-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.article_test.go
111 lines (86 loc) · 3.04 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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
_ "net/http/httptest"
"strings"
"testing"
)
func TestArticleUnauthenticated(t *testing.T) {
r := GetRouter(true)
r.GET("/article/view/:article_id", getArticle)
req, _ := http.NewRequest("GET", "/article/view/1", nil)
testHttpResponse(t, r, req, func(w *httptest.ResponseRecorder) bool {
statusOK := w.Code == http.StatusOK
theArticle, _ := getArticleByID(1)
title := theArticle.Title
p, err := ioutil.ReadAll(w.Body)
pageOK := err == nil && strings.Index(string(p), fmt.Sprintf("<title>%s</title>", title)) > 0
return statusOK && pageOK
})
}
// Test that GET request to the Home page returns home page with 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"
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 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
})
}