-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
257 lines (223 loc) · 5.6 KB
/
server_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package chatgo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/labstack/echo"
)
type Comparison func(*httptest.ResponseRecorder) string
type T struct {
handlerFunc echo.HandlerFunc
t *testing.T
req *http.Request
rec *httptest.ResponseRecorder
path string
comparisons map[string]Comparison
}
func testHandler(t T) {
e := echo.New()
c := e.NewContext(t.req, t.rec)
c.SetPath(t.path)
if err := t.handlerFunc(c); err != nil {
t.t.Errorf("got error while calling handlerFunc: %v\n", err)
}
for name, comparison := range t.comparisons {
t.t.Run(
fmt.Sprintf("%s_%s", t.t.Name(), name),
func(test *testing.T) {
if err := comparison(t.rec); err != "" {
test.Error(err)
}
},
)
}
}
func indentedJSON(s string) (string, error) {
var output bytes.Buffer
err := json.Indent(&output, []byte(s), "", "\t")
if err != nil {
return "", err
}
return output.String(), nil
}
func compareCode(code int) Comparison {
return func(rec *httptest.ResponseRecorder) string {
if rec.Code != code {
return fmt.Sprintf("expected %d but got %d", code, rec.Code)
}
return ""
}
}
func compareJSON(expected string) Comparison {
return func(rec *httptest.ResponseRecorder) string {
if rec.Body.String() != expected {
expected, err := indentedJSON(expected)
if err != nil {
return fmt.Sprintf("got error while indenting JSON: %s", err)
}
got, err := indentedJSON(rec.Body.String())
if err != nil {
return fmt.Sprintf("got error while indenting JSON: %s", err)
}
return fmt.Sprintf("expected:\n%s\ngot:\n%s", expected, got)
}
return ""
}
}
func TestGetKeyboard(t *testing.T) {
var getKeyboardScenario CondScenario
getKeyboardScenario.Add(
func(o Object) bool {
return true
},
func(o Object) (Scenario, Object) {
return nil, Keyboard{[]string{"hello", "seeeturtle"}}
},
)
chat := NewChat()
chat.Set("get_keyboard", getKeyboardScenario)
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Content-Type", "application/json")
expectedJSON := `{"type":"buttons","buttons":["hello","seeeturtle"]}`
testHandler(
T{
handlerFunc: chat.getKeyboard,
t: t,
req: req,
rec: httptest.NewRecorder(),
path: "/keyboard",
comparisons: map[string]Comparison{
"Code": compareCode(200),
"JSON": compareJSON(expectedJSON),
},
},
)
}
func TestPostMessage(t *testing.T) {
var postMessageScenario CondScenario
postMessageScenario.Add(
func(o Object) bool { return true },
func(o Object) (Scenario, Object) {
switch v := o.(type) {
case Text:
return nil, Message{Text: string(v)}
default:
return nil, nil
}
},
)
chat := NewChat()
chat.Set("reply_message", postMessageScenario)
req := httptest.NewRequest(
"POST",
"/",
strings.NewReader(`{"user_key":"encryptedUserKey","type":"text","content":"Hello World"}`),
)
req.Header.Set("Content-Type", "application/json")
expectedJSON := `{"message":{"text":"Hello World"}}`
testHandler(
T{
handlerFunc: chat.postMessage,
t: t,
req: req,
rec: httptest.NewRecorder(),
path: "/message",
comparisons: map[string]Comparison{
"Code": compareCode(200),
"JSON": compareJSON(expectedJSON),
},
},
)
}
func TestPostFriend(t *testing.T) {
var postFriendScenario CondScenario
postFriendScenario.Add(
func(o Object) bool { return true },
func(Object) (Scenario, Object) {
return nil, nil
},
)
chat := NewChat()
chat.Set("add_user", postFriendScenario)
req := httptest.NewRequest("POST",
"/",
strings.NewReader(`{"user_key":"user_name"}`),
)
req.Header.Set("Content-Type", "application/json")
expectedJSON := `{"code":0,"message":"SUCCESS","comment":"정상 응답"}`
testHandler(
T{
handlerFunc: chat.postFriend,
t: t,
req: req,
rec: httptest.NewRecorder(),
path: "/friend",
comparisons: map[string]Comparison{
"Code": compareCode(200),
"JSON": compareJSON(expectedJSON),
},
},
)
}
func TestDeleteFriend(t *testing.T) {
var deleteFriendScenario CondScenario
deleteFriendScenario.Add(
func(o Object) bool { return true },
func(o Object) (Scenario, Object) { return nil, nil },
)
chat := NewChat()
chat.Set("delete_user", deleteFriendScenario)
req := httptest.NewRequest(
"DELETE",
"/",
strings.NewReader(`{"user_key":"user_name"}`),
)
req.Header.Set("Content-Type", "application/json")
expectedJSON := `{"code":0,"message":"SUCCESS","comment":"정상 응답"}`
testHandler(
T{
handlerFunc: chat.deleteFriend,
t: t,
req: req,
rec: httptest.NewRecorder(),
path: "/friend/:user_key",
comparisons: map[string]Comparison{
"Code": compareCode(200),
"JSON": compareJSON(expectedJSON),
},
},
)
}
func TestDeleteRoom(t *testing.T) {
var deleteRoomScenario CondScenario
deleteRoomScenario.Add(
func(o Object) bool { return true },
func(o Object) (Scenario, Object) { return nil, nil },
)
chat := NewChat()
chat.Set("delete_room", deleteRoomScenario)
req := httptest.NewRequest(
"DELETE",
"/",
strings.NewReader(`{"user_key":"user_name"}`),
)
req.Header.Set("Content-Type", "application/json")
expectedJSON := `{"code":0,"message":"SUCCESS","comment":"정상 응답"}`
testHandler(
T{
handlerFunc: chat.deleteRoom,
t: t,
req: req,
rec: httptest.NewRecorder(),
path: "/room/:user_key",
comparisons: map[string]Comparison{
"Code": compareCode(200),
"JSON": compareJSON(expectedJSON),
},
},
)
}