-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.article_test.go
119 lines (93 loc) · 3.41 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
// handlers.article_test.go
package main
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// 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 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 unauthenticated user
func TestCustom404(t *testing.T) {
r := getRouter(true)
r.GET("/missing", custom404)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/missing", nil)
testHTTPResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
// Test that the http status code is 200
status := w.Code == http.StatusNotFound
// Test that the page title is "Page Not Found"
// 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>Page Not Found</title>") > 0
return status && pageOK
})
}