-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
43 lines (38 loc) · 892 Bytes
/
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
package main
import (
"net/http"
"net/http/httptest"
"os"
"testing"
)
var url string
func TestMain(m *testing.M) {
srv := httptest.NewServer(handler())
url = srv.URL
os.Exit(m.Run())
}
// TestRouting tests whether the patterns return the correct status codes.
func TestRouting(t *testing.T) {
tt := []struct {
name string
pattern string
statusCode int
}{
{"empty", "", http.StatusOK},
{"forwardSlash", "/", http.StatusOK},
// Do not register this pattern.
// All unregistered patterns should return StatusNotFound.
{"abcdefg1234567", "/abcdefg1234567", http.StatusNotFound},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
res, err := http.Get(url + tc.pattern)
if err != nil {
t.Fatalf("%v", err)
}
if res.StatusCode != tc.statusCode {
t.Fatalf("expected %v; got %v", tc.statusCode, res.StatusCode)
}
})
}
}