-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
317 lines (275 loc) · 7.1 KB
/
main_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestGetPort(t *testing.T) {
// Save value so we can set it back
orig, isSet := os.LookupEnv("PORT")
err := os.Unsetenv("PORT")
if err != nil {
t.Fatal(err)
}
expected := ":8080"
if getPort() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
getPort(), expected)
}
err = os.Setenv("PORT", "3000")
if err != nil {
t.Fatal(err)
}
expected = ":3000"
if getPort() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
getPort(), expected)
}
if isSet {
err = os.Setenv("PORT", orig)
if err != nil {
t.Fatal(err)
}
} else {
err = os.Unsetenv("PORT")
if err != nil {
t.Fatal(err)
}
}
}
func TestHomeHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(homeHandler)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
// expected := `{"alive": true}`
// if rr.Body.String() != expected {
// t.Errorf("handler returned unexpected body: got %v want %v",
// rr.Body.String(), expected)
// }
}
func TestFireHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req1, err := http.NewRequest("GET", "/fire", nil)
if err != nil {
t.Fatal(err)
}
req2, err := http.NewRequest("POST", "/fire", nil)
if err != nil {
t.Fatal(err)
}
reqs := []*http.Request{req1, req2}
for _, req := range reqs {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fireHandler)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusSeeOther {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusSeeOther)
}
// Check the response body is what we expect.
// expected := `{"alive": true}`
// if rr.Body.String() != expected {
// t.Errorf("handler returned unexpected body: got %v want %v",
// rr.Body.String(), expected)
// }
}
}
func TestTokenHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req1, err := http.NewRequest("GET", "/token", nil)
if err != nil {
t.Fatal(err)
}
req2, err := http.NewRequest("POST", "/token", nil)
if err != nil {
t.Fatal(err)
}
reqs := []*http.Request{req1, req2}
for _, req := range reqs {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(activateHandler)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusSeeOther {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusSeeOther)
}
// Check the response body is what we expect.
// expected := `{"alive": true}`
// if rr.Body.String() != expected {
// t.Errorf("handler returned unexpected body: got %v want %v",
// rr.Body.String(), expected)
// }
}
}
func TestRandomPageHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req1, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
req2, err := http.NewRequest("GET", "/fire", nil)
if err != nil {
t.Fatal(err)
}
req3, err := http.NewRequest("GET", "/token", nil)
if err != nil {
t.Fatal(err)
}
req4, err := http.NewRequest("GET", "/testindex.html", nil)
if err != nil {
t.Fatal(err)
}
req5, err := http.NewRequest("GET", "/Thisshouldnotbeaurl", nil)
if err != nil {
t.Fatal(err)
}
req6, err := http.NewRequest("GET", "/This should be & a ) bad / request", nil)
if err != nil {
t.Fatal(err)
}
reqs := []*http.Request{req1, req2, req3, req4, req5, req6}
for _, req := range reqs {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(randomPageHandler)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the response body is what we expect.
// expected := `{"alive": true}`
// if rr.Body.String() != expected {
// t.Errorf("handler returned unexpected body: got %v want %v",
// rr.Body.String(), expected)
// }
}
}
func TestFire(t *testing.T) {
fire()
}
func TestRandomValue(t *testing.T) {
sum := 0
//TODO: This should be a map so it can be broken out into a helper function
hasBeen5 := false
hasBeen6 := false
hasBeen7 := false
hasBeen8 := false
hasBeen9 := false
hasBeen10 := false
hasBeen11 := false
hasBeen12 := false
hasBeen13 := false
hasBeen14 := false
hasBeen15 := false
allNumHit := false
for i := 0; i < 10000; i++ {
num := randomValue(5, 15)
switch num {
case 5:
if !hasBeen5 {
fmt.Println("5")
hasBeen5 = true
}
case 6:
if !hasBeen6 {
fmt.Println("6")
hasBeen6 = true
}
case 7:
if !hasBeen7 {
fmt.Println("7")
hasBeen7 = true
}
case 8:
if !hasBeen8 {
fmt.Println("8")
hasBeen8 = true
}
case 9:
if !hasBeen9 {
fmt.Println("9")
hasBeen9 = true
}
case 10:
if !hasBeen10 {
fmt.Println("10")
hasBeen10 = true
}
case 11:
if !hasBeen11 {
fmt.Println("11")
hasBeen11 = true
}
case 12:
if !hasBeen12 {
fmt.Println("12")
hasBeen12 = true
}
case 13:
if !hasBeen13 {
fmt.Println("13")
hasBeen13 = true
}
case 14:
if !hasBeen14 {
fmt.Println("14")
hasBeen14 = true
}
case 15:
if !hasBeen15 {
fmt.Println("15")
hasBeen15 = true
}
default:
break
}
if hasBeen5 &&
hasBeen6 &&
hasBeen7 &&
hasBeen8 &&
hasBeen9 &&
hasBeen10 &&
hasBeen11 &&
hasBeen12 &&
hasBeen13 &&
hasBeen14 &&
hasBeen15 {
allNumHit = true
break
}
sum += i
}
if !allNumHit {
t.Errorf("not all numbers hit in test")
}
}