-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux_test.go
98 lines (93 loc) · 2.55 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
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
package multiplexer
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestHttpNotFound(t *testing.T) {
mux := New(http.NewServeMux(), "/")
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("custom not found handler"))
})
ts := httptest.NewServer(mux)
defer ts.Close()
resp, err := http.Get(ts.URL + "/api/blah-blah")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "custom not found handler" {
t.Fatalf("expected: custom not found handler, got %s", string(body))
}
}
func TestHttpMethodNotAllowed(t *testing.T) {
mux := New(http.NewServeMux(), "/api")
mux.MethodNotAllowed = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("custom method not allowed handler"))
})
mux.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello"))
})
ts := httptest.NewServer(mux)
defer ts.Close()
resp, err := http.Post(ts.URL+"/api/hello", "application/json", nil)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) == "hello" {
t.Fatalf("expected: custom method not allowed handler, got %s", string(body))
}
if resp.StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("expected: %d, got %d", http.StatusMethodNotAllowed, resp.StatusCode)
}
}
func TestHttpPathWithSlash(t *testing.T) {
mux := New(http.NewServeMux(), "/api")
mux.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
ts := httptest.NewServer(mux)
defer ts.Close()
resp, err := http.Get(ts.URL + "/api/hello/")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
t.Fatal("expected with / 200 but got 404")
}
}
func TestHttpGroupAPI(t *testing.T) {
mux := New(http.NewServeMux(), "/api")
usersGroup := mux.Group("/users")
usersGroup.HandleFunc("/fetch", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("user is here"))
})
ts := httptest.NewServer(mux)
defer ts.Close()
resp, err := http.Get(ts.URL + "/api/users/fetch")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "user is here" {
t.Fatalf("expected: user is here, got %s", string(body))
}
}