-
Notifications
You must be signed in to change notification settings - Fork 1
/
mux_test.go
67 lines (54 loc) · 1.14 KB
/
mux_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
package mux
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func setup() *httptest.Server {
m := NewMux()
m.Get("/foo", foo)
m.Get("/bar", bar)
m.Get("/baz", baz)
return httptest.NewServer(m)
}
func teardown(ts *httptest.Server) {
ts.Close()
}
func TestMux(t *testing.T) {
ts := setup()
defer teardown(ts)
cases := []struct {
Path string
Status int
Body string
}{
{"/foo", 200, "foo"},
{"/bar", 200, "bar"},
{"/baz", 200, "baz"},
{"/hoge", 404, "404 page not found\n"},
}
for _, tc := range cases {
res, err := http.Get(ts.URL + tc.Path)
if err != nil {
t.Fatalf("something went to wrong: %s", err)
}
if got, want := res.StatusCode, tc.Status; got != want {
t.Fatalf("StatusCode=%d, want=%d", got, want)
}
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
if got, want := buf.String(), tc.Body; got != want {
t.Fatalf("Body=%q, want=%q", got, want)
}
}
}
func foo(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("foo"))
}
func bar(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("bar"))
}
func baz(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("baz"))
}