-
Notifications
You must be signed in to change notification settings - Fork 4
/
router_test.go
100 lines (77 loc) · 1.68 KB
/
router_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
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestGopherGet(t *testing.T) {
rt, err := NewRouter(api)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/api/gophers/1", nil)
if err != nil {
t.Fatal(err)
}
rt.ServeHTTP(w, req)
// Test if returned some error
errorTest(w, t)
// Try to get the gopher from the response
var resp GopherResp
err = json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(resp.Gopher, api.Gophers[0]) {
t.Fatal("The service returned the gopher wrong!")
}
}
func TestGophersInit(t *testing.T) {
rt, err := NewRouter(api)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/api/gophers", nil)
if err != nil {
t.Fatal(err)
}
rt.ServeHTTP(w, req)
// Test if returned some error
errorTest(w, t)
// Try to get the gopher from the response
var resp GophersResp
err = json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatal(err)
}
if len(resp.Gophers) < 4 {
t.Fatal("The service not all gophers wrong!")
}
}
func TestGetAction(t *testing.T) {
rt, err := NewRouter(api)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/api/gophers/2/message", nil)
if err != nil {
t.Fatal(err)
}
rt.ServeHTTP(w, req)
// Test if returned some error
errorTest(w, t)
// Try to get the string returned
var resp StringResp
err = json.Unmarshal(w.Body.Bytes(), &resp)
if err != nil {
t.Fatal(err)
}
if resp.String != "I still love programming" {
t.Fatal("The service returned something wrong!")
}
}