refactor reactions tests to get things out of the global space
Ryan Carver
8 years ago
6 | 6 | "testing" |
7 | 7 | ) |
8 | 8 | |
9 | func init() { | |
10 | http.HandleFunc("/reactions.add", reactionHandler) | |
11 | http.HandleFunc("/reactions.remove", reactionHandler) | |
12 | http.HandleFunc("/reactions.get", getReactionHandler) | |
13 | http.HandleFunc("/reactions.list", listReactionHandler) | |
14 | } | |
15 | ||
16 | var ( | |
17 | gotParams map[string]string | |
18 | getReactionRes string | |
19 | listReactionRes string | |
20 | ) | |
21 | ||
22 | func accumulateFormValue(k string, r *http.Request) { | |
9 | type reactionsHandler struct { | |
10 | gotParams map[string]string | |
11 | response string | |
12 | } | |
13 | ||
14 | func newReactionsHandler() *reactionsHandler { | |
15 | return &reactionsHandler{ | |
16 | gotParams: make(map[string]string), | |
17 | response: `{ "ok": true }`, | |
18 | } | |
19 | } | |
20 | ||
21 | func (rh *reactionsHandler) accumulateFormValue(k string, r *http.Request) { | |
23 | 22 | if v := r.FormValue(k); v != "" { |
24 | gotParams[k] = v | |
25 | } | |
26 | } | |
27 | ||
28 | func reactionHandler(w http.ResponseWriter, r *http.Request) { | |
29 | accumulateFormValue("name", r) | |
30 | accumulateFormValue("channel", r) | |
31 | accumulateFormValue("timestamp", r) | |
32 | accumulateFormValue("file", r) | |
33 | accumulateFormValue("file_comment", r) | |
23 | rh.gotParams[k] = v | |
24 | } | |
25 | } | |
26 | ||
27 | func (rh *reactionsHandler) handler(w http.ResponseWriter, r *http.Request) { | |
28 | rh.accumulateFormValue("channel", r) | |
29 | rh.accumulateFormValue("count", r) | |
30 | rh.accumulateFormValue("file", r) | |
31 | rh.accumulateFormValue("file_comment", r) | |
32 | rh.accumulateFormValue("full", r) | |
33 | rh.accumulateFormValue("name", r) | |
34 | rh.accumulateFormValue("page", r) | |
35 | rh.accumulateFormValue("timestamp", r) | |
36 | rh.accumulateFormValue("user", r) | |
34 | 37 | w.Header().Set("Content-Type", "application/json") |
35 | w.Write([]byte(`{ "ok": true }`)) | |
36 | } | |
37 | ||
38 | func getReactionHandler(w http.ResponseWriter, r *http.Request) { | |
39 | accumulateFormValue("channel", r) | |
40 | accumulateFormValue("timestamp", r) | |
41 | accumulateFormValue("file", r) | |
42 | accumulateFormValue("file_comment", r) | |
43 | accumulateFormValue("full", r) | |
44 | w.Header().Set("Content-Type", "application/json") | |
45 | w.Write([]byte(getReactionRes)) | |
46 | } | |
47 | ||
48 | func listReactionHandler(w http.ResponseWriter, r *http.Request) { | |
49 | accumulateFormValue("user", r) | |
50 | accumulateFormValue("count", r) | |
51 | accumulateFormValue("full", r) | |
52 | accumulateFormValue("page", r) | |
53 | w.Header().Set("Content-Type", "application/json") | |
54 | w.Write([]byte(listReactionRes)) | |
38 | w.Write([]byte(rh.response)) | |
55 | 39 | } |
56 | 40 | |
57 | 41 | func TestSlack_AddReaction(t *testing.T) { |
85 | 69 | }, |
86 | 70 | }, |
87 | 71 | } |
72 | var rh *reactionsHandler | |
73 | http.HandleFunc("/reactions.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) | |
88 | 74 | for i, test := range tests { |
89 | gotParams = map[string]string{} | |
75 | rh = newReactionsHandler() | |
90 | 76 | err := api.AddReaction(test.params) |
91 | 77 | if err != nil { |
92 | 78 | t.Fatalf("%d: Unexpected error: %s", i, err) |
93 | 79 | } |
94 | if !reflect.DeepEqual(gotParams, test.wantParams) { | |
95 | t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams) | |
80 | if !reflect.DeepEqual(rh.gotParams, test.wantParams) { | |
81 | t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams) | |
96 | 82 | } |
97 | 83 | } |
98 | 84 | } |
128 | 114 | }, |
129 | 115 | }, |
130 | 116 | } |
117 | var rh *reactionsHandler | |
118 | http.HandleFunc("/reactions.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) | |
131 | 119 | for i, test := range tests { |
132 | gotParams = map[string]string{} | |
120 | rh = newReactionsHandler() | |
133 | 121 | err := api.RemoveReaction(test.params) |
134 | 122 | if err != nil { |
135 | 123 | t.Fatalf("%d: Unexpected error: %s", i, err) |
136 | 124 | } |
137 | if !reflect.DeepEqual(gotParams, test.wantParams) { | |
138 | t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams) | |
125 | if !reflect.DeepEqual(rh.gotParams, test.wantParams) { | |
126 | t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams) | |
139 | 127 | } |
140 | 128 | } |
141 | 129 | } |
241 | 229 | }, |
242 | 230 | }, |
243 | 231 | } |
232 | var rh *reactionsHandler | |
233 | http.HandleFunc("/reactions.get", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) | |
244 | 234 | for i, test := range tests { |
245 | gotParams = map[string]string{} | |
246 | getReactionRes = test.json | |
235 | rh = newReactionsHandler() | |
236 | rh.response = test.json | |
247 | 237 | got, err := api.GetReactions(test.params) |
248 | 238 | if err != nil { |
249 | 239 | t.Fatalf("%d: Unexpected error: %s", i, err) |
251 | 241 | if !reflect.DeepEqual(got, test.wantReactions) { |
252 | 242 | t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions) |
253 | 243 | } |
254 | if !reflect.DeepEqual(gotParams, test.wantParams) { | |
255 | t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams) | |
244 | if !reflect.DeepEqual(rh.gotParams, test.wantParams) { | |
245 | t.Errorf("%d: Got params %#v, want %#v", i, rh.gotParams, test.wantParams) | |
256 | 246 | } |
257 | 247 | } |
258 | 248 | } |
261 | 251 | once.Do(startServer) |
262 | 252 | SLACK_API = "http://" + serverAddr + "/" |
263 | 253 | api := New("testing-token") |
264 | listReactionRes = `{"ok": true, | |
254 | rh := newReactionsHandler() | |
255 | http.HandleFunc("/reactions.list", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }) | |
256 | rh.response = `{"ok": true, | |
265 | 257 | "items": [ |
266 | 258 | { |
267 | 259 | "type": "message", |
347 | 339 | "page": "2", |
348 | 340 | "full": "true", |
349 | 341 | } |
350 | gotParams = map[string]string{} | |
351 | 342 | params := NewListReactionsParameters("UserID") |
352 | 343 | params.Count = 200 |
353 | 344 | params.Page = 2 |
366 | 357 | fmt.Printf("Reactions %#v\n", item.Reactions) |
367 | 358 | } |
368 | 359 | } |
369 | if !reflect.DeepEqual(gotParams, wantParams) { | |
370 | t.Errorf("Got params %#v, want %#v", gotParams, wantParams) | |
360 | if !reflect.DeepEqual(rh.gotParams, wantParams) { | |
361 | t.Errorf("Got params %#v, want %#v", rh.gotParams, wantParams) | |
371 | 362 | } |
372 | 363 | if reflect.DeepEqual(paging, Paging{}) { |
373 | 364 | t.Errorf("Want paging data, got empty struct") |