-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor handlers #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,3 @@ | ||
# Go DDP | ||
|
||
DDP server and client implemented with go. | ||
|
||
## Server Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/meteorhacks/goddp" | ||
) | ||
|
||
func main() { | ||
server := goddp.NewServer() | ||
server.Method("hello", methodHandler) | ||
server.Listen(":1337") | ||
} | ||
|
||
func methodHandler(p []interface{}) (interface{}, error) { | ||
return "result", nil | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package integration | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/meteorhacks/goddp/server" | ||
) | ||
|
||
var ( | ||
URL = "http://localhost:1337/websocket" | ||
ORIGIN = "http://localhost:1337" | ||
ADDR = "localhost:1337" | ||
s server.Server | ||
) | ||
|
||
type MethodError struct { | ||
Error string `json:"error"` | ||
} | ||
|
||
type Message struct { | ||
Msg string `json:"msg"` | ||
Session string `json:"session"` | ||
ID string `json:"id"` | ||
Result float64 `json:"result"` | ||
Error MethodError `json:"error"` | ||
} | ||
|
||
func TestStartServer(t *testing.T) { | ||
s = server.New() | ||
|
||
s.Method("double", func(ctx server.MethodContext) { | ||
n, ok := ctx.Args[0].(float64) | ||
|
||
if !ok { | ||
ctx.SendError("invalid parameters") | ||
} else { | ||
ctx.SendResult(n * 2) | ||
} | ||
|
||
ctx.SendUpdated() | ||
}) | ||
|
||
go s.Listen(":1337") | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
|
||
func TestConnect(t *testing.T) { | ||
ws, err := newClient() | ||
if err != nil { | ||
t.Error("websocket connection failed") | ||
} | ||
|
||
defer ws.Close() | ||
|
||
writeMessage(ws, `{"msg": "connect", "version": "1", "support": ["1"]}`, t) | ||
msg := readMessage(ws, t) | ||
|
||
if msg.Msg != "connected" { | ||
t.Error("inconnect DDP message type") | ||
} | ||
|
||
if len(msg.Session) != 17 { | ||
t.Error("session field should be have 17 characters") | ||
} | ||
} | ||
|
||
func TestPingWithoutId(t *testing.T) { | ||
ws, err := newClient() | ||
if err != nil { | ||
t.Error("websocket connection failed") | ||
} | ||
|
||
defer ws.Close() | ||
|
||
writeMessage(ws, `{"msg": "connect", "version": "1", "support": ["1"]}`, t) | ||
_ = readMessage(ws, t) // ignore "connected" message | ||
|
||
writeMessage(ws, `{"msg": "ping"}`, t) | ||
msg := readMessage(ws, t) | ||
|
||
if msg.Msg != "pong" { | ||
t.Error("inconnect DDP message type") | ||
} | ||
} | ||
|
||
func TestPingWithId(t *testing.T) { | ||
ws, err := newClient() | ||
if err != nil { | ||
t.Error("websocket connection failed") | ||
} | ||
|
||
defer ws.Close() | ||
|
||
writeMessage(ws, `{"msg": "connect", "version": "1", "support": ["1"]}`, t) | ||
_ = readMessage(ws, t) // ignore "connected" message | ||
|
||
writeMessage(ws, `{"msg": "ping", "id": "test-id"}`, t) | ||
msg := readMessage(ws, t) | ||
|
||
if msg.Msg != "pong" { | ||
t.Error("inconnect DDP message type") | ||
} | ||
|
||
if msg.ID != "test-id" { | ||
t.Error("inconnect random id") | ||
} | ||
} | ||
|
||
func TestMethodResult(t *testing.T) { | ||
ws, err := newClient() | ||
if err != nil { | ||
t.Error("websocket connection failed") | ||
} | ||
|
||
defer ws.Close() | ||
|
||
writeMessage(ws, `{"msg": "connect", "version": "1", "support": ["1"]}`, t) | ||
_ = readMessage(ws, t) // ignore "connected" message | ||
|
||
writeMessage(ws, `{"msg": "method", "id": "test-id", "method": "double", "params": [2]}`, t) | ||
msg := readMessage(ws, t) | ||
|
||
if msg.Msg != "result" { | ||
t.Error("inconnect DDP message type") | ||
} | ||
|
||
if msg.ID != "test-id" { | ||
t.Error("inconnect random id") | ||
} | ||
|
||
if msg.Result != 4 { | ||
t.Error("inconnect method result") | ||
} | ||
} | ||
|
||
func TestMethodError(t *testing.T) { | ||
ws, err := newClient() | ||
if err != nil { | ||
t.Error("websocket connection failed") | ||
} | ||
|
||
defer ws.Close() | ||
|
||
writeMessage(ws, `{"msg": "connect", "version": "1", "support": ["1"]}`, t) | ||
_ = readMessage(ws, t) // ignore "connected" message | ||
|
||
writeMessage(ws, `{"msg": "method", "id": "test-id", "method": "double", "params": ["two"]}`, t) | ||
msg := readMessage(ws, t) | ||
|
||
if msg.Msg != "result" { | ||
t.Error("inconnect DDP message type") | ||
} | ||
|
||
if msg.ID != "test-id" { | ||
t.Error("inconnect random id") | ||
} | ||
|
||
if msg.Error.Error == "" { | ||
t.Error("method error should be set") | ||
} | ||
} | ||
|
||
func newClient() (*websocket.Conn, error) { | ||
u, _ := url.Parse(URL) | ||
conn, err := net.Dial("tcp", ADDR) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
header := http.Header{"Origin": {ORIGIN}} | ||
ws, _, err := websocket.NewClient(conn, u, header, 1024, 1024) | ||
return ws, err | ||
} | ||
|
||
func writeMessage(c *websocket.Conn, str string, t *testing.T) { | ||
w, err := c.NextWriter(websocket.TextMessage) | ||
|
||
if err != nil { | ||
t.Error("cannot create websocket write") | ||
} | ||
|
||
io.WriteString(w, str) | ||
w.Close() | ||
} | ||
|
||
func readMessage(c *websocket.Conn, t *testing.T) Message { | ||
op, r, err := c.NextReader() | ||
|
||
if op != websocket.TextMessage { | ||
t.Error("expecting a text message") | ||
} | ||
|
||
if err != nil { | ||
t.Error("cannot create reader") | ||
} | ||
|
||
str, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
t.Error("websocket read error") | ||
} | ||
|
||
msg := Message{} | ||
if err := json.Unmarshal(str, &msg); err != nil { | ||
t.Error("cannot parse websocket response") | ||
} | ||
|
||
return msg | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
type MethodContext struct { | ||
ID string | ||
Args []interface{} | ||
Res Response | ||
Done bool | ||
Updated bool | ||
} | ||
|
||
func NewMethodContext(m Message, res Response) MethodContext { | ||
ctx := MethodContext{} | ||
ctx.ID = m.ID | ||
ctx.Args = m.Params | ||
ctx.Res = res | ||
return ctx | ||
} | ||
|
||
func (ctx *MethodContext) SendResult(r interface{}) error { | ||
if ctx.Done { | ||
err := errors.New("already sent results for method") | ||
return err | ||
} | ||
|
||
ctx.Done = true | ||
return ctx.Res.WriteJSON(map[string]interface{}{ | ||
"msg": "result", | ||
"id": ctx.ID, | ||
"result": r, | ||
}) | ||
} | ||
|
||
func (ctx *MethodContext) SendError(e string) error { | ||
if ctx.Done { | ||
err := errors.New("already sent results for method") | ||
return err | ||
} | ||
|
||
ctx.Done = true | ||
return ctx.Res.WriteJSON(map[string]interface{}{ | ||
"msg": "result", | ||
"id": ctx.ID, | ||
"error": map[string]string{ | ||
"error": e, | ||
}, | ||
}) | ||
} | ||
|
||
func (ctx *MethodContext) SendUpdated() error { | ||
if ctx.Updated { | ||
err := errors.New("already sent updated for method") | ||
return err | ||
} | ||
|
||
ctx.Updated = true | ||
return ctx.Res.WriteJSON(map[string]interface{}{ | ||
"msg": "updated", | ||
"methods": []string{ctx.ID}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate.