-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjson_test.go
233 lines (196 loc) · 4.95 KB
/
json_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
package httptest
import (
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
type jBody struct {
Method string `json:"method"`
Name string `json:"name"`
Message string `json:"message"`
Username string `json:"username"`
Password string `json:"password"`
}
func JSONApp() http.Handler {
p := &mux{}
p.Handle("GET", "/get", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(201)
json.NewEncoder(res).Encode(jBody{
Method: req.Method,
Message: "Hello from Get!",
})
})
p.Handle("HEAD", "/head", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(418)
json.NewEncoder(res).Encode(jBody{
Method: req.Method,
Message: "Hello from Head!",
})
})
p.Handle("DELETE", "/delete", func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(201)
json.NewEncoder(res).Encode(jBody{
Method: req.Method,
Message: "Goodbye",
})
})
p.Handle("POST", "/post", func(res http.ResponseWriter, req *http.Request) {
jb := jBody{}
if u, p, ok := req.BasicAuth(); ok {
jb.Username = u
jb.Password = p
}
json.NewDecoder(req.Body).Decode(&jb)
jb.Method = req.Method
json.NewEncoder(res).Encode(jb)
})
p.Handle("PUT", "/put", func(res http.ResponseWriter, req *http.Request) {
jb := jBody{}
json.NewDecoder(req.Body).Decode(&jb)
jb.Method = req.Method
json.NewEncoder(res).Encode(jb)
})
p.Handle("PATCH", "/patch", func(res http.ResponseWriter, req *http.Request) {
jb := jBody{}
json.NewDecoder(req.Body).Decode(&jb)
jb.Method = req.Method
json.NewEncoder(res).Encode(jb)
})
p.Handle("POST", "/sessions/set", func(res http.ResponseWriter, req *http.Request) {
sess, _ := Store.Get(req, "my-session")
sess.Values["name"] = req.PostFormValue("name")
sess.Save(req, res)
})
p.Handle("GET", "/sessions/get", func(res http.ResponseWriter, req *http.Request) {
sess, _ := Store.Get(req, "my-session")
if sess.Values["name"] != nil {
json.NewEncoder(res).Encode(jBody{
Method: req.Method,
Name: sess.Values["name"].(string),
})
}
})
return p
}
func Test_JSON_Headers_Dont_Overwrite_App_Headers(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
w.Headers["foo"] = "bar"
req := w.JSON("/")
req.Headers["foo"] = "baz"
r.Equal("baz", req.Headers["foo"])
r.Equal("bar", w.Headers["foo"])
}
func Test_JSON_Get(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/get")
r.Equal("/get", req.URL)
res := req.Get()
r.Equal(201, res.Code)
jb := &jBody{}
res.Bind(jb)
r.Equal("GET", jb.Method)
r.Equal("Hello from Get!", jb.Message)
}
func Test_JSON_Head(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
c := w.JSON("/head")
r.Equal("/head", c.URL)
res, err := c.Do("HEAD", nil)
r.NoError(err)
r.Equal(418, res.Code)
jb := &jBody{}
res.Bind(jb)
r.Equal("HEAD", jb.Method)
r.Equal("Hello from Head!", jb.Message)
}
func Test_JSON_Delete(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/delete")
r.Equal("/delete", req.URL)
res := req.Delete()
jb := &jBody{}
res.Bind(jb)
r.Equal("DELETE", jb.Method)
r.Equal("Goodbye", jb.Message)
}
func Test_JSON_Post_Struct(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/post")
res := req.Post(User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("POST", jb.Method)
r.Equal("Mark", jb.Name)
}
func Test_JSON_Post_Struct_Pointer(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/post")
res := req.Post(&User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("POST", jb.Method)
r.Equal("Mark", jb.Name)
r.Equal("", jb.Username)
r.Equal("", jb.Password)
}
func Test_JSON_Post_Set_Basic_Auth(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/post")
req.Username = "httptest_username"
req.Password = "httptest_password"
res := req.Post(&User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("POST", jb.Method)
r.Equal("Mark", jb.Name)
r.Equal("httptest_username", jb.Username)
r.Equal("httptest_password", jb.Password)
}
func Test_JSON_Put(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/put")
res := req.Put(User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("PUT", jb.Method)
r.Equal("Mark", jb.Name)
}
func Test_JSON_Put_Struct_Pointer(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/put")
res := req.Put(&User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("PUT", jb.Method)
r.Equal("Mark", jb.Name)
}
func Test_JSON_Patch(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/patch")
res := req.Patch(User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("PATCH", jb.Method)
r.Equal("Mark", jb.Name)
}
func Test_JSON_Patch_Struct_Pointer(t *testing.T) {
r := require.New(t)
w := New(JSONApp())
req := w.JSON("/patch")
res := req.Patch(&User{Name: "Mark"})
jb := &jBody{}
res.Bind(jb)
r.Equal("PATCH", jb.Method)
r.Equal("Mark", jb.Name)
}