forked from gorilla/mux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailgun_patch_test.go
More file actions
71 lines (62 loc) · 2.07 KB
/
mailgun_patch_test.go
File metadata and controls
71 lines (62 loc) · 2.07 KB
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
package mux
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestRouteUpsert(t *testing.T) {
router := NewRouter()
router.HandleFunc("/foo", func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("GET /foo v1"))
}).Methods("GET")
router.HandleFunc("/bar", func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("GET /bar v1"))
}).Methods("GET")
// This route is masked because v1 was added to the router fist.
router.HandleFunc("/foo", func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("GET /foo v2"))
}).Methods("GET")
// This route overrides v1 because it is added using UpsertRoute.
r := router.NewUnattachedRoute().Path("/bar").Methods("GET").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("GET /bar v2"))
})
router.UpsertRoute(r)
r = router.NewUnattachedRoute().Path("/zoom").Methods("GET").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
_, _ = rw.Write([]byte("GET /zoom v1"))
})
router.UpsertRoute(r)
assertResponseBody := func(t *testing.T, s *httptest.Server, method, path, wantBody string) {
rq, _ := http.NewRequest(method, s.URL+path, nil)
resp, err := s.Client().Do(rq)
if err != nil {
t.Fatalf("unexpected error getting from server: %v", err)
}
if resp.StatusCode != 200 {
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected error reading body: %v", err)
}
if !bytes.Equal(body, []byte(wantBody)) {
t.Fatalf("response should be hello world, was: %q", string(body))
}
}
t.Run("/foo", func(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
assertResponseBody(t, s, "GET", "/foo", "GET /foo v1")
})
t.Run("/bar", func(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
assertResponseBody(t, s, "GET", "/bar", "GET /bar v2")
})
t.Run("/zoom", func(t *testing.T) {
s := httptest.NewServer(router)
defer s.Close()
assertResponseBody(t, s, "GET", "/zoom", "GET /zoom v1")
})
}