-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
164 lines (138 loc) · 3.83 KB
/
router_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package router_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/gostalt/router"
"github.com/stretchr/testify/assert"
)
// helloHandler is a simple handler used in tests.
func helloHandler() string { return "Hello" }
func TestResponseCodes(t *testing.T) {
type testcase struct {
expected int
setup func() *httptest.Server
client func() *http.Client
}
cases := map[string]testcase{
"unregistered routes return 404": {
expected: http.StatusNotFound,
setup: func() *httptest.Server {
return httptest.NewServer(router.New())
},
client: func() *http.Client {
return &http.Client{}
},
},
// "unavailable method returns 405": {
// expected: http.StatusMethodNotAllowed,
// setup: func() *httptest.Server {
// r := router.New()
// r.Post("/", helloHandler)
// return httptest.NewServer(r)
// },
// client: func() *http.Client {
// return &http.Client{}
// },
// },
"redirect routes return 308": {
expected: http.StatusPermanentRedirect,
setup: func() *httptest.Server {
r := router.New()
r.Redirect("/", "/new")
return httptest.NewServer(r)
},
client: func() *http.Client {
return &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
server := tc.setup()
defer server.Close()
resp, _ := tc.client().Get(server.URL)
assert.Equal(t, tc.expected, resp.StatusCode)
})
}
}
func TestEmptyRouteIsNotCatchall(t *testing.T) {
router := router.New()
router.Get("/", func() string {
return "hello"
})
server := httptest.NewServer(router)
defer server.Close()
resp, _ := http.Get(server.URL + "/nonexistant")
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func TestRouteDispatching(t *testing.T) {
router := router.New()
server := httptest.NewServer(router)
defer server.Close()
t.Run("test basic get", func(t *testing.T) {
router.Get("user/profile", func() string {
return "Hello"
})
assert.Equal(t, "Hello", get(server.URL+"/user/profile"))
})
t.Run("test basic post", func(t *testing.T) {
router.Post("users", func() string {
return "Hello post"
})
assert.Equal(t, "Hello post", post(server.URL+"/users"))
})
t.Run("test parameterised route", func(t *testing.T) {
router.Get("users/{userId}/posts/{postId}", func(req *http.Request) string {
return fmt.Sprintf("Hello %s on post %s!", req.Form.Get("userId"), req.Form.Get("postId"))
})
assert.Equal(t, "Hello 30 on post 28!", get(server.URL+"/users/30/posts/28"))
})
t.Run("test parameterised route with patterns", func(t *testing.T) {
router.Get("posts/{postId:[0-9]+}", func(req *http.Request) string {
return fmt.Sprintf("Hello post %s!", req.Form.Get("postId"))
})
assert.Equal(t, "Hello post 28!", get(server.URL+"/posts/28"))
})
t.Run("duplicate records uses last registered", func(t *testing.T) {
router.Get("duplicate", func() string {
return "first"
})
router.Get("duplicate", func() string {
return "second"
})
assert.Equal(t, "second", get(server.URL+"/duplicate"))
})
}
// get is a convenience method that fires off a GET request and assumes a positive
// response with no errors. If errors occur, a panic is thrown.
func get(uri string) string {
resp, err := http.Get(uri)
if err != nil {
panic(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return string(body)
}
// post is a convenience method that fires off a POST request and assumes a positive
// response with no errors. If errors occur, a panic is thrown.
func post(uri string) string {
resp, err := http.Post(uri, "text/plain", nil)
if err != nil {
panic(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return string(body)
}