forked from gitpod-io/go-gin-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.article_test.go
318 lines (246 loc) · 9.75 KB
/
handlers.article_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// handlers.article_test.go
package main
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
)
// Test that a GET request to the home page returns the home page with
// the HTTP code 200 for an unauthenticated user
func TestShowIndexPageUnauthenticated(t *testing.T) {
r := getRouter(true)
r.GET("/", showIndexPage)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/", nil)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
statusOK := w.Code == http.StatusOK
// Test that the page title is "Home Page"
// You can carry out a lot more detailed tests using libraries that can
// parse and process HTML pages
p, err := ioutil.ReadAll(w.Body)
pageOK := err == nil && strings.Index(string(p), "<title>Home Page</title>") > 0
return statusOK && pageOK
})
}
// Test that a GET request to the home page returns the home page with
// the HTTP code 200 for an authenticated user
func TestShowIndexPageAuthenticated(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter(true)
// Set the token cookie to simulate an authenticated user
http.SetCookie(w, &http.Cookie{Name: "token", Value: "123"})
// Define the route similar to its definition in the routes file
r.GET("/", showIndexPage)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/", nil)
req.Header = http.Header{"Cookie": w.HeaderMap["Set-Cookie"]}
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page title is "Home Page"
// You can carry out a lot more detailed tests using libraries that can
// parse and process HTML pages
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "<title>Home Page</title>") < 0 {
t.Fail()
}
}
// Test that a GET request to an article page returns the article page with
// the HTTP code 200 for an unauthenticated user
func TestArticleUnauthenticated(t *testing.T) {
r := getRouter(true)
// Define the route similar to its definition in the routes file
r.GET("/article/view/:article_id", getArticle)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/article/view/1", nil)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
statusOK := w.Code == http.StatusOK
// Test that the page title is "Article 1"
// You can carry out a lot more detailed tests using libraries that can
// parse and process HTML pages
p, err := ioutil.ReadAll(w.Body)
pageOK := err == nil && strings.Index(string(p), "<title>Article 1</title>") > 0
return statusOK && pageOK
})
}
// Test that a GET request to an article page returns the article page with
// the HTTP code 200 for an authenticated user
func TestArticleAuthenticated(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter(true)
// Set the token cookie to simulate an authenticated user
http.SetCookie(w, &http.Cookie{Name: "token", Value: "123"})
// Define the route similar to its definition in the routes file
r.GET("/article/view/:article_id", getArticle)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/article/view/1", nil)
req.Header = http.Header{"Cookie": w.HeaderMap["Set-Cookie"]}
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page title is "Article 1"
// You can carry out a lot more detailed tests using libraries that can
// parse and process HTML pages
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "<title>Article 1</title>") < 0 {
t.Fail()
}
}
// Test that a GET request to the home page returns the list of articles
// in JSON format when the Accept header is set to application/json
func TestArticleListJSON(t *testing.T) {
r := getRouter(true)
// Define the route similar to its definition in the routes file
r.GET("/", showIndexPage)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("Accept", "application/json")
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
statusOK := w.Code == http.StatusOK
// Test that the response is JSON which can be converted to
// an array of Article structs
p, err := ioutil.ReadAll(w.Body)
if err != nil {
return false
}
var articles []article
err = json.Unmarshal(p, &articles)
return err == nil && len(articles) >= 2 && statusOK
})
}
// Test that a GET request to an article page returns the article in XML
// format when the Accept header is set to application/xml
func TestArticleXML(t *testing.T) {
r := getRouter(true)
// Define the route similar to its definition in the routes file
r.GET("/article/view/:article_id", getArticle)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/article/view/1", nil)
req.Header.Add("Accept", "application/xml")
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
statusOK := w.Code == http.StatusOK
// Test that the response is JSON which can be converted to
// an array of Article structs
p, err := ioutil.ReadAll(w.Body)
if err != nil {
return false
}
var a article
err = xml.Unmarshal(p, &a)
return err == nil && a.ID == 1 && len(a.Title) >= 0 && statusOK
})
}
// Test that a GET request to the article creation page returns the
// article creation page with the HTTP code 200 for an authenticated user
func TestArticleCreationPageAuthenticated(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter(true)
// Set the token cookie to simulate an authenticated user
http.SetCookie(w, &http.Cookie{Name: "token", Value: "123"})
// Define the route similar to its definition in the routes file
r.GET("/article/create", ensureLoggedIn(), showArticleCreationPage)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/article/create", nil)
req.Header = http.Header{"Cookie": w.HeaderMap["Set-Cookie"]}
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page title is "Create New Article"
// You can carry out a lot more detailed tests using libraries that can
// parse and process HTML pages
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "<title>Create New Article</title>") < 0 {
t.Fail()
}
}
// Test that a GET request to the article creation page returns
// an HTTP 401 error for an unauthorized user
func TestArticleCreationPageUnauthenticated(t *testing.T) {
r := getRouter(true)
// Define the route similar to its definition in the routes file
r.GET("/article/create", ensureLoggedIn(), showArticleCreationPage)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/article/create", nil)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 401
return w.Code == http.StatusUnauthorized
})
}
// Test that a POST request to create an article returns
// an HTTP 200 code along with a success message for an authenticated user
func TestArticleCreationAuthenticated(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter(true)
// Set the token cookie to simulate an authenticated user
http.SetCookie(w, &http.Cookie{Name: "token", Value: "123"})
// Define the route similar to its definition in the routes file
r.POST("/article/create", ensureLoggedIn(), createArticle)
// Create a request to send to the above route
articlePayload := getArticlePOSTPayload()
req, _ := http.NewRequest("POST", "/article/create", strings.NewReader(articlePayload))
req.Header = http.Header{"Cookie": w.HeaderMap["Set-Cookie"]}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(articlePayload)))
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page title is "Submission Successful"
// You can carry out a lot more detailed tests using libraries that can
// parse and process HTML pages
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "<title>Submission Successful</title>") < 0 {
t.Fail()
}
}
// Test that a POST request to create an article returns
// an HTTP 401 error for an unauthorized user
func TestArticleCreationUnauthenticated(t *testing.T) {
r := getRouter(true)
// Define the route similar to its definition in the routes file
r.POST("/article/create", ensureLoggedIn(), createArticle)
// Create a request to send to the above route
articlePayload := getArticlePOSTPayload()
req, _ := http.NewRequest("POST", "/article/create", strings.NewReader(articlePayload))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(articlePayload)))
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 401
return w.Code == http.StatusUnauthorized
})
}
func getArticlePOSTPayload() string {
params := url.Values{}
params.Add("title", "Test Article Title")
params.Add("content", "Test Article Content")
return params.Encode()
}