forked from taiyoh/graphqlws-subscription-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_test.go
119 lines (111 loc) · 3.51 KB
/
notifier_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gss
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
)
func notifyRequestTest(method, url, content, contentType string) (int, []byte) {
req, _ := http.NewRequest(method, url, strings.NewReader(content))
ctx := context.Background()
req = req.WithContext(ctx)
req.Header.Set("Content-Type", contentType)
client := &http.Client{}
resp, _ := client.Do(req)
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
defer resp.Body.Close()
return resp.StatusCode, buf.Bytes()
}
type notifyTestCase struct {
Label string
Method string
Body string
ContentType string
StatusCode int
ResponseSuccess bool
ResponseError string
}
func TestNotificationHandler(t *testing.T) {
mux := http.NewServeMux()
ch := make(chan *RequestData, 10)
mux.Handle("/notify", NewNotifyHandler(ch))
ts := httptest.NewServer(mux)
defer ts.Close()
for _, testCase := range []notifyTestCase{
notifyTestCase{
Label: "invalid method",
Method: "GET",
Body: "foobar",
ContentType: "application/json",
StatusCode: http.StatusMethodNotAllowed,
ResponseSuccess: false,
ResponseError: "not allowed method",
},
notifyTestCase{
Label: "invalid content-type",
Method: "POST",
Body: "foobar",
ContentType: "application/x-www-form-urlencoded",
StatusCode: http.StatusBadRequest,
ResponseSuccess: false,
ResponseError: "Content-Type requires application/json",
},
notifyTestCase{
Label: "wrong body",
Method: "POST",
Body: "foobar",
ContentType: "application/json",
StatusCode: http.StatusBadRequest,
ResponseSuccess: false,
ResponseError: "cannot parse invalid JSON request data",
},
} {
t.Run(testCase.Label, func(t *testing.T) {
statusCode, bufBytes := notifyRequestTest(testCase.Method, ts.URL+"/notify", testCase.Body, testCase.ContentType)
if statusCode != testCase.StatusCode {
t.Error(testCase.Label + ": /notify invalid statusCode: " + strconv.Itoa(statusCode))
}
resData := &NotificationResponse{}
json.Unmarshal(bufBytes, resData)
if resData.Success != testCase.ResponseSuccess {
t.Error(testCase.Label + ": response.Success is wrong")
}
if len(resData.Errors) != 1 {
t.Error(testCase.Label + ": response.Errors count should be 1")
}
if !resData.Success && resData.Errors[0] != testCase.ResponseError {
t.Error(testCase.Label + ": unexpected error message")
}
})
}
for _, testCase := range []notifyTestCase{
notifyTestCase{
Label: "success",
Method: "POST",
Body: `{"channel":"foo","users":["hoge","fuga"],"field":"foo","payload":{"aaa":"iii","uu":{"ee":"oo"}}}`,
ContentType: "application/json",
StatusCode: http.StatusOK,
ResponseSuccess: true,
},
} {
t.Run(testCase.Label, func(t *testing.T) {
statusCode, bufBytes := notifyRequestTest(testCase.Method, ts.URL+"/notify", testCase.Body, testCase.ContentType)
if statusCode != testCase.StatusCode {
t.Error(testCase.Label + ": /notify invalid statusCode: " + strconv.Itoa(statusCode))
}
resData := &NotificationResponse{}
json.Unmarshal(bufBytes, resData)
if resData.Success != testCase.ResponseSuccess {
t.Error(testCase.Label + ": response.Success is wrong. " + resData.Errors[0])
}
if len(resData.Errors) != 0 {
t.Error(testCase.Label + ": response.Errors count should be 0")
}
})
}
}