Skip to content

Commit e5a511b

Browse files
committed
Add tests
1 parent 7c6055d commit e5a511b

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed

server/handler_connect_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHandleConnect(t *testing.T) {
8+
s := Server{}
9+
h := NewConnectHandler(s)
10+
m := Message{}
11+
r := &TestResponse{}
12+
13+
h.handle(r, m)
14+
data := r._data.(map[string]string)
15+
16+
if data["msg"] != "connected" {
17+
t.Error("msg field should be 'connected'")
18+
}
19+
20+
if len(data["session"]) != 17 {
21+
t.Error("session field should be have 17 characters")
22+
}
23+
}

server/handler_method_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUnavailableMethod(t *testing.T) {
8+
s := Server{}
9+
h := NewMethodHandler(s)
10+
m := Message{Method: "test"}
11+
r := &TestResponse{}
12+
13+
if err := h.handle(r, m); err == nil {
14+
t.Error("an error must be returned if method is not available")
15+
}
16+
}
17+
18+
func TestAvailableMethod(t *testing.T) {
19+
s := Server{methods: make(map[string]MethodFn)}
20+
h := NewMethodHandler(s)
21+
m := Message{Method: "test"}
22+
r := &TestResponse{}
23+
b := false
24+
25+
s.methods["test"] = func(ctx MethodContext) {
26+
b = true
27+
}
28+
29+
if err := h.handle(r, m); err != nil {
30+
t.Error("an error must not be returned if method is available")
31+
}
32+
33+
if !b {
34+
t.Error("method handler must be called")
35+
}
36+
}

server/handler_ping_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package server
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestHandlePingWithoutID(t *testing.T) {
9+
s := Server{}
10+
h := NewPingHandler(s)
11+
m := Message{}
12+
r := &TestResponse{}
13+
h.handle(r, m)
14+
15+
expected := map[string]string{
16+
"msg": "pong",
17+
}
18+
19+
if !reflect.DeepEqual(r._data, expected) {
20+
t.Error("message should only have msg field")
21+
}
22+
}
23+
24+
func TestHandlePingWithID(t *testing.T) {
25+
s := Server{}
26+
h := NewPingHandler(s)
27+
m := Message{ID: "test-id"}
28+
r := &TestResponse{}
29+
h.handle(r, m)
30+
31+
expected := map[string]string{
32+
"msg": "pong",
33+
"id": "test-id",
34+
}
35+
36+
if !reflect.DeepEqual(r._data, expected) {
37+
t.Error("message should have msg and ID fields")
38+
}
39+
}

server/method_context_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package server
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSendResult(t *testing.T) {
9+
r := &TestResponse{}
10+
ctx := MethodContext{ID: "test-id", Res: r}
11+
err := ctx.SendResult(100)
12+
13+
expected := map[string]interface{}{
14+
"msg": "result",
15+
"id": "test-id",
16+
"result": 100,
17+
}
18+
19+
if err != nil {
20+
t.Error("result should be sent successfully")
21+
}
22+
23+
if !ctx.Done {
24+
t.Error("context must set that a result is sent")
25+
}
26+
27+
if !reflect.DeepEqual(r._data, expected) {
28+
t.Error("invalid response for method result")
29+
}
30+
}
31+
32+
func TestSendResultWhenDone(t *testing.T) {
33+
r := &TestResponse{}
34+
ctx := MethodContext{ID: "test-id", Res: r, Done: true}
35+
err := ctx.SendResult(100)
36+
37+
if err == nil {
38+
t.Error("result should be sent only once")
39+
}
40+
41+
if r._data != nil {
42+
t.Error("result should be sent only once")
43+
}
44+
}
45+
46+
func TestSendError(t *testing.T) {
47+
r := &TestResponse{}
48+
ctx := MethodContext{ID: "test-id", Res: r}
49+
err := ctx.SendError("test-error")
50+
51+
expected := map[string]interface{}{
52+
"msg": "result",
53+
"id": "test-id",
54+
"error": map[string]string{
55+
"error": "test-error",
56+
},
57+
}
58+
59+
if err != nil {
60+
t.Error("error should be sent successfully")
61+
}
62+
63+
if !ctx.Done {
64+
t.Error("context must set that a result is sent")
65+
}
66+
67+
if !reflect.DeepEqual(r._data, expected) {
68+
t.Error("invalid response for method error")
69+
}
70+
}
71+
72+
func TestSendErrorWhenDone(t *testing.T) {
73+
r := &TestResponse{}
74+
ctx := MethodContext{ID: "test-id", Res: r, Done: true}
75+
err := ctx.SendError("test-error")
76+
77+
if err == nil {
78+
t.Error("error should be sent only once")
79+
}
80+
81+
if r._data != nil {
82+
t.Error("error should be sent only once")
83+
}
84+
}
85+
86+
func TestSendUpdated(t *testing.T) {
87+
r := &TestResponse{}
88+
ctx := MethodContext{ID: "test-id", Res: r}
89+
err := ctx.SendUpdated()
90+
91+
expected := map[string]interface{}{
92+
"msg": "updated",
93+
"methods": []string{"test-id"},
94+
}
95+
96+
if err != nil {
97+
t.Error("updated should be sent successfully")
98+
}
99+
100+
if !ctx.Updated {
101+
t.Error("context must set that updated is sent")
102+
}
103+
104+
if !reflect.DeepEqual(r._data, expected) {
105+
t.Error("invalid response for method updated")
106+
}
107+
}
108+
109+
func TestSendUpdatedWhenDone(t *testing.T) {
110+
r := &TestResponse{}
111+
ctx := MethodContext{ID: "test-id", Res: r, Updated: true}
112+
err := ctx.SendUpdated()
113+
114+
if err == nil {
115+
t.Error("updated message should be sent only once")
116+
}
117+
118+
if r._data != nil {
119+
t.Error("updated message should be sent only once")
120+
}
121+
}

server/server_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package server
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestAddMethod(t *testing.T) {
9+
s := New()
10+
s.Method("testfn", func(MethodContext) {})
11+
if _, ok := s.methods["testfn"]; !ok {
12+
t.Error("method functionm ust be stored under methods")
13+
}
14+
}
15+
16+
func TestReadMessageReadError(t *testing.T) {
17+
req := TestRequest{Error: errors.New("test-error")}
18+
if _, err := readMessage(&req); err == nil {
19+
t.Error("an error must be returned if reading from Request fails")
20+
}
21+
}
22+
23+
func TestReadMessageBinaryMessage(t *testing.T) {
24+
req := TestRequest{Type: 2}
25+
if _, err := readMessage(&req); err == nil {
26+
t.Error("an error must be returned if type is binary")
27+
}
28+
}
29+
30+
func TestReadMessageInvalidMessage(t *testing.T) {
31+
str := []byte("invalid-json")
32+
req := TestRequest{Type: 1, Message: str}
33+
if _, err := readMessage(&req); err == nil {
34+
t.Error("an error must be returned if message is not json")
35+
}
36+
}
37+
38+
func TestReadMessageValidMessage(t *testing.T) {
39+
str := []byte(`{"msg": "ping"}`)
40+
req := TestRequest{Type: 1, Message: str}
41+
msg, err := readMessage(&req)
42+
43+
if err != nil {
44+
t.Error("message must be read successfully")
45+
}
46+
47+
if msg.Msg != "ping" {
48+
t.Error("message must have correct message type")
49+
}
50+
}

server/types_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package server
2+
3+
type TestResponse struct {
4+
_data interface{}
5+
}
6+
7+
func (t *TestResponse) WriteJSON(d interface{}) error {
8+
t._data = d
9+
return nil
10+
}
11+
12+
type TestRequest struct {
13+
Type int
14+
Message []byte
15+
Error error
16+
}
17+
18+
func (t *TestRequest) ReadMessage() (int, []byte, error) {
19+
return t.Type, t.Message, t.Error
20+
}

0 commit comments

Comments
 (0)