-
Notifications
You must be signed in to change notification settings - Fork 0
/
rum_test.go
371 lines (352 loc) · 9.69 KB
/
rum_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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (c) 2020 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rum
import (
"crypto/tls"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
)
func testHTTP(method, url string, status int, result string, t *testing.T) {
var req *http.Request
req, _ = http.NewRequest(method, url, nil)
client := &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 1,
DisableKeepAlives: true,
},
}
if resp, err := client.Do(req); err != nil {
t.Error(err)
} else if resp.StatusCode != status {
t.Error(resp.StatusCode)
} else if body, err := ioutil.ReadAll(resp.Body); err != nil {
t.Error(err)
} else if string(body) != result {
t.Error(string(body))
}
}
func testHTTPError(method, url string, t *testing.T) {
var req *http.Request
req, _ = http.NewRequest(method, url, nil)
client := &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 1,
DisableKeepAlives: true,
},
}
if _, err := client.Do(req); err == nil {
t.Error()
}
}
func testHTTPTLS(method, url string, status int, result string, t *testing.T) {
var req *http.Request
req, _ = http.NewRequest(method, url, nil)
client := &http.Client{
Transport: &http.Transport{
MaxConnsPerHost: 1,
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
if resp, err := client.Do(req); err != nil {
t.Error(err)
} else if resp.StatusCode != status {
t.Error(resp.StatusCode)
} else if body, err := ioutil.ReadAll(resp.Body); err != nil {
t.Error(err)
} else if string(body) != result {
t.Error(string(body))
}
}
func TestRum(t *testing.T) {
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.Run(addr)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTP("GET", "http://"+addr+"/", http.StatusOK, "Hello World", t)
m.Close()
<-done
}
func TestRumTLS(t *testing.T) {
certFile := "server.crt"
keyFile := "server.key"
defer os.Remove(certFile)
defer os.Remove(keyFile)
cf, err := os.Create(certFile)
if err != nil {
t.Error()
}
cf.Write(testCertPEM)
cf.Close()
kf, err := os.Create(keyFile)
if err != nil {
t.Error()
}
kf.Write(testKeyPEM)
kf.Close()
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.RunTLS(addr, certFile, keyFile)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTPTLS("GET", "https://"+addr+"/", http.StatusOK, "Hello World", t)
m.Close()
<-done
}
func TestFastRum(t *testing.T) {
addr := ":8080"
m := New()
m.SetFast(true)
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.Run(addr)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTP("GET", "http://"+addr+"/", http.StatusOK, "Hello World", t)
m.Close()
<-done
}
func TestRumPoll(t *testing.T) {
addr := ":8080"
m := New()
m.SetPoll(true)
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.Run(addr)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTP("GET", "http://"+addr+"/", http.StatusOK, "Hello World", t)
m.Close()
<-done
}
func TestFastRumPollTLS(t *testing.T) {
certFile := "server.crt"
keyFile := "server.key"
defer os.Remove(certFile)
defer os.Remove(keyFile)
cf, err := os.Create(certFile)
if err != nil {
t.Error()
}
cf.Write(testCertPEM)
cf.Close()
kf, err := os.Create(keyFile)
if err != nil {
t.Error()
}
kf.Write(testKeyPEM)
kf.Close()
addr := ":8080"
m := New()
m.SetFast(true)
m.SetPoll(true)
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.RunTLS(addr, certFile, keyFile)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTPTLS("GET", "https://"+addr+"/", http.StatusOK, "Hello World", t)
testHTTPError("GET", "http://"+addr+"/", t)
m.Close()
<-done
}
func TestListenAndServe(t *testing.T) {
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
ListenAndServe(addr, m)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTP("GET", "http://"+addr+"/", http.StatusOK, "Hello World", t)
DefaultServer.Close()
<-done
}
func TestListenAndServeTLS(t *testing.T) {
certFile := "server.crt"
keyFile := "server.key"
defer os.Remove(certFile)
defer os.Remove(keyFile)
cf, err := os.Create(certFile)
if err != nil {
t.Error()
}
cf.Write(testCertPEM)
cf.Close()
kf, err := os.Create(keyFile)
if err != nil {
t.Error()
}
kf.Write(testKeyPEM)
kf.Close()
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
ListenAndServeTLS(addr, certFile, keyFile, m)
close(done)
}()
time.Sleep(time.Millisecond * 10)
testHTTPTLS("GET", "https://"+addr+"/", http.StatusOK, "Hello World", t)
DefaultServer.Close()
<-done
}
func TestRun(t *testing.T) {
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.Run(addr)
close(done)
}()
time.Sleep(time.Millisecond * 10)
if err := m.Run(addr); err == nil {
t.Error(err)
}
m.Close()
<-done
}
func TestRunTLS(t *testing.T) {
certFile := "server.crt"
keyFile := "server.key"
defer os.Remove(certFile)
defer os.Remove(keyFile)
cf, err := os.Create(certFile)
if err != nil {
t.Error()
}
cf.Write(testCertPEM)
cf.Close()
kf, err := os.Create(keyFile)
if err != nil {
t.Error()
}
kf.Write(testKeyPEM)
kf.Close()
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
done := make(chan struct{})
go func() {
m.RunTLS(addr, certFile, keyFile)
close(done)
}()
time.Sleep(time.Millisecond * 10)
if err := m.RunTLS(addr, certFile, keyFile); err == nil {
t.Error(err)
}
m.Close()
<-done
}
func TestRunTLSMore(t *testing.T) {
addr := ":8080"
m := New()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
if err := m.RunTLS(addr, "", ""); err == nil {
t.Error(err)
}
m.Close()
}
func TestStrSliceContains(t *testing.T) {
if !strSliceContains([]string{"http/1.1"}, "http/1.1") {
t.Error()
}
if strSliceContains([]string{"http/1.0"}, "http/1.1") {
t.Error()
}
}
var testKeyPEM = []byte(`-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQClne2gm0eXs/fV
Ffk/pPncBqlVvSy4XXu+n7VC7uAVjWd5Qv2aID/ECJSAzoeHHrzKhwE35yVnUZYl
i0M+KFen6cZ1l/bJLZ2stTKjnVRLIWTEU0ySBWdbq5noKCqgu95/UEWYYspfuBVU
aPoFi9QJhGMy1Bqay5jK85O+k+6/ZDa80uJBU4x1Ii5um72Cno+nLoAHqZ21hW58
pHQSdjLwcitclB0I11vk5ukVaQ0Ex+khyTWOQUPVmlBlHt/MxppqnPw0VCEm/8ID
WbN0GknBJ6BMm72OI9rv7CXVKCB3p9bwTHWs+ZAc1i0FIbd/QRAUZ3R18xRy5vhp
5x+eCxkFAgMBAAECggEAcVTsGn6H9FDsJD4ALURI0v8f+5MTuioGlwCVOnNaloUI
LNvhzQmKeSxh6u7poVYwXOsbs1SSrOwQsiplXnCe40G/VBMkhrCXeGsadwofd7Yn
TY2vFTFrdDiufkaWPR2yeS7GISxhTjUbK4MWrJruA4E7nLm2Zv0x1pfuH3j95Kul
xzn+x5f39IlTtUMcPtlxmyIQJhP8IZCNMH1czMviEsVhse1HjlP6RPtcyG3vSUTl
sT+SFV+qVEQM8i/PsopEXmSp2c57wLZuPAfyZE2v52ZLayrvozWEwCzkPhXwBh8Z
YL9mWpFWMi21xm/6yGAsemt0wQSamVMud1dCJkt3lQKBgQDbSVuUfT0Y9yEIbsYa
xykm2b9h9EYRTrW1LTmFV7olSc2y4ODX749M2aVViS0sptSFH+16RrBRqx4znTWE
Zw4Ie00NYdudP+e50KQttbWlCWLLPW//6A0bh5osMtwVH/DeWh9D8Dv5p8z/sM04
ni9WnBEgZv9nCDBcHZr/kdGPVwKBgQDBWEjPRb9VbHkChk5P16F/Z28zlXOfLznQ
YJFno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