-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
www_test.go
59 lines (48 loc) · 1.38 KB
/
www_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
package main
import (
"net/http"
"net/http/httptest"
"regexp"
"testing"
)
func TestCreateSSL(t *testing.T) {
certPEMBlock, keyPEMBlock, err := createSSL()
if certPEMBlock == nil {
t.Errorf("Error when create ssl %v", err)
}
if keyPEMBlock == nil {
t.Errorf("Error when create ssl %v", err)
}
if err != nil {
t.Errorf("Error when create ssl %v", err)
}
}
func TestMain(t *testing.T) {
// Create a request to pass to www handler.
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
r := "." // current dir
q := false
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(www(r, q))
// 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 := "www.go"
re := regexp.MustCompile("www.go")
isContains := re.MatchString(rr.Body.String())
if !isContains {
t.Errorf("handler returned unexpected body: got %v but not contains %v",
rr.Body.String(), expected)
}
}