-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong_test.go
158 lines (128 loc) · 3.5 KB
/
pong_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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
type PongTestCase struct {
desc string
method string
query string
body string
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err.Error())
}
}
func assertResponseStatus(t *testing.T, expected int, got int) {
t.Helper()
if expected != got {
t.Errorf("Expected response status to be %d, but got %d", expected, got)
}
}
func assertResponseContentType(t *testing.T, expected string, got string) {
t.Helper()
if expected != got {
t.Errorf("Expected response headers to have Content-Type %s, but got %s", expected, got)
}
}
func assertResponseMethod(t *testing.T, expected string, response string) {
t.Helper()
if expected != response {
t.Errorf("Expected %s, but got %s", expected, response)
}
}
func assertKeyValues(t *testing.T, key string, expectedValue string, gotValue string) {
t.Helper()
if expectedValue != gotValue {
t.Errorf("Expected key %s to have value %v, but got %v", key, expectedValue, gotValue)
}
}
func deepCompare(t *testing.T, sent string, response interface{}) {
t.Helper()
expected, err := url.ParseQuery(sent)
assertNoError(t, err)
got := response.(map[string]interface{})
for key, expectedValue := range expected {
assertKeyValues(t, key, fmt.Sprintf("%v", expectedValue), fmt.Sprintf("%v", got[key]))
}
for key, gotValue := range got {
assertKeyValues(t, key, fmt.Sprintf("%v", expected[key]), fmt.Sprintf("%v", gotValue))
}
}
func doRequest(t *testing.T, testCase PongTestCase) *http.Request {
t.Helper()
requestURL := "/ping"
if testCase.query != "" {
requestURL = requestURL + "?" + testCase.query
}
var requestBody io.Reader
if testCase.body != "" {
requestBody = strings.NewReader(testCase.body)
}
request, err := http.NewRequest(testCase.method, requestURL, requestBody)
assertNoError(t, err)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return request
}
func TestPongHandler(t *testing.T) {
testCases := []PongTestCase{
{
desc: "GET with no query string nor body",
method: http.MethodGet,
},
{
desc: "GET with a query string",
method: http.MethodGet,
query: "key1=value1&key2=value2",
},
{
desc: "POST with no query string nor body",
method: http.MethodPost,
},
{
desc: "POST with a query string but no body",
method: http.MethodPost,
query: "key1=value1&key2=value2",
},
{
desc: "POST with a query string and a body",
method: http.MethodPost,
query: "key1=value1&key2=value2",
body: "key3=value3&key4=value4",
},
}
for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
request := doRequest(t, testCase)
response := httptest.NewRecorder()
if testCase.body != "" {
defer request.Body.Close()
}
PongHandler(response, request)
responseBody, err := ioutil.ReadAll(response.Body)
assertNoError(t, err)
var pongResponse PongResponse
if err := json.Unmarshal(responseBody, &pongResponse); err != nil {
t.Fatalf(err.Error())
}
assertResponseStatus(t, http.StatusOK, response.Code)
assertResponseContentType(t, "application/json", response.HeaderMap["Content-Type"][0])
assertResponseMethod(t, testCase.method, pongResponse.Method)
if testCase.query != "" {
deepCompare(t, testCase.query, pongResponse.QueryParams)
}
if testCase.body != "" {
deepCompare(t, testCase.body, pongResponse.Body)
}
})
}
}