Codebase list golang-github-nlopes-slack / 63c3772
refactor reactions tests to get things out of the global space Ryan Carver 8 years ago
1 changed file(s) with 49 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
66 "testing"
77 )
88
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) {
2322 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)
3437 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))
5539 }
5640
5741 func TestSlack_AddReaction(t *testing.T) {
8569 },
8670 },
8771 }
72 var rh *reactionsHandler
73 http.HandleFunc("/reactions.add", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
8874 for i, test := range tests {
89 gotParams = map[string]string{}
75 rh = newReactionsHandler()
9076 err := api.AddReaction(test.params)
9177 if err != nil {
9278 t.Fatalf("%d: Unexpected error: %s", i, err)
9379 }
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)
9682 }
9783 }
9884 }
128114 },
129115 },
130116 }
117 var rh *reactionsHandler
118 http.HandleFunc("/reactions.remove", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
131119 for i, test := range tests {
132 gotParams = map[string]string{}
120 rh = newReactionsHandler()
133121 err := api.RemoveReaction(test.params)
134122 if err != nil {
135123 t.Fatalf("%d: Unexpected error: %s", i, err)
136124 }
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)
139127 }
140128 }
141129 }
241229 },
242230 },
243231 }
232 var rh *reactionsHandler
233 http.HandleFunc("/reactions.get", func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) })
244234 for i, test := range tests {
245 gotParams = map[string]string{}
246 getReactionRes = test.json
235 rh = newReactionsHandler()
236 rh.response = test.json
247237 got, err := api.GetReactions(test.params)
248238 if err != nil {
249239 t.Fatalf("%d: Unexpected error: %s", i, err)
251241 if !reflect.DeepEqual(got, test.wantReactions) {
252242 t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions)
253243 }
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)
256246 }
257247 }
258248 }
261251 once.Do(startServer)
262252 SLACK_API = "http://" + serverAddr + "/"
263253 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,
265257 "items": [
266258 {
267259 "type": "message",
347339 "page": "2",
348340 "full": "true",
349341 }
350 gotParams = map[string]string{}
351342 params := NewListReactionsParameters("UserID")
352343 params.Count = 200
353344 params.Page = 2
366357 fmt.Printf("Reactions %#v\n", item.Reactions)
367358 }
368359 }
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)
371362 }
372363 if reflect.DeepEqual(paging, Paging{}) {
373364 t.Errorf("Want paging data, got empty struct")