-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_test.go
177 lines (143 loc) · 4.97 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
165
166
167
168
169
170
171
172
173
174
175
176
177
package router
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yashpokar/router/tests"
)
func ProductDetailsHandler(w http.ResponseWriter, r *http.Request) {
vars := Vars(r)
productID, ok := vars["product_id"]
if !ok {
_, err := w.Write([]byte("'product_id' not found"))
if err != nil {
log.Fatal(err)
}
}
_, err := w.Write([]byte(productID))
if err != nil {
log.Fatal(err)
}
}
func TestRouter(t *testing.T) {
r := New().(*router)
server := &http.Server{Handler: r, Addr: ":8787"}
go func() {
err := server.ListenAndServe()
assert.NoError(t, err)
}()
t.Run("registers GET method", func(t *testing.T) {
r.GET("/", tests.Handler)
route, err := r.resolve("GET", "")
assert.NoError(t, err)
assert.Equal(t, route.path, "")
})
t.Run("registers POST method", func(t *testing.T) {
r.POST("/", tests.Handler)
route, err := r.resolve("POST", "")
assert.NoError(t, err)
assert.Equal(t, route.path, "")
})
t.Run("registers PUT method", func(t *testing.T) {
r.PUT("/", tests.Handler)
route, err := r.resolve("PUT", "")
assert.NoError(t, err)
assert.Equal(t, route.path, "")
})
t.Run("registers PATCH method", func(t *testing.T) {
r.PATCH("/", tests.Handler)
route, err := r.resolve("PATCH", "")
assert.NoError(t, err)
assert.Equal(t, route.path, "")
})
t.Run("registers DELETE method", func(t *testing.T) {
r.DELETE("/", tests.Handler)
route, err := r.resolve("DELETE", "")
assert.NoError(t, err)
assert.Equal(t, route.path, "")
})
t.Run("handles general error", func(t *testing.T) {
r.handleError(tests.NewMockResponseWriter(), &http.Request{}, errors.New("internal server error"))
})
t.Run("resolves the route", func(t *testing.T) {
r.GET("/path/nested", tests.Handler)
route, err := r.resolve("GET", "path/nested")
assert.NoError(t, err)
assert.Equal(t, "path/nested", route.path)
})
t.Run("returns the path variables map", func(t *testing.T) {
r.GET("/products/:product_id", tests.Handler)
route, err := r.resolve("GET", "products/8298ae5f-9cbf-4751-a411-560419b0b5d7")
assert.NoError(t, err)
assert.Equal(t, route.getPathVariablesMap(), map[string]string{"product_id": "8298ae5f-9cbf-4751-a411-560419b0b5d7"})
})
t.Run("returns nil when there are no path variables", func(t *testing.T) {
r.GET("/products", tests.Handler)
route, err := r.resolve("GET", "products")
assert.NoError(t, err)
assert.Nil(t, route.getPathVariablesMap())
})
t.Run("returns error when route not found", func(t *testing.T) {
route, err := r.resolve("GET", "unknown/path")
assert.EqualError(t, err, "route 'unknown/path' not found")
assert.Nil(t, route)
})
t.Run("returns the product id using path variable", func(t *testing.T) {
r.GET("/products/:product_id", ProductDetailsHandler)
response, err := http.Get("http://localhost:8787/products/8298ae5f-9cbf-4751-a411-560419b0b5d7")
assert.NoError(t, err)
bytes, err := io.ReadAll(response.Body)
assert.NoError(t, err)
assert.NoError(t, err)
assert.Equal(t, []byte("8298ae5f-9cbf-4751-a411-560419b0b5d7"), bytes)
defer response.Body.Close()
})
t.Run("can not return the product id when it is not there", func(t *testing.T) {
r.GET("/products", ProductDetailsHandler)
response, err := http.Get("http://localhost:8787/products")
assert.NoError(t, err)
bytes, err := io.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, []byte("'product_id' not found"), bytes)
defer response.Body.Close()
})
t.Run("recovers from the panic", func(t *testing.T) {
r.GET("/panic-maker", tests.PanicMaker)
r.OnPanic(func(w http.ResponseWriter, _ *http.Request, r any) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(r.(string)))
})
response, err := http.Get("http://localhost:8787/panic-maker")
assert.NoError(t, err)
bytes, err := io.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
assert.Equal(t, []byte("to test panic recovery"), bytes)
defer response.Body.Close()
})
t.Run("handles the not found route", func(t *testing.T) {
r.GET("/panic-maker", tests.PanicMaker)
r.OnRouteNotFound(func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte(fmt.Sprintf("route '%s' not found.", request.RequestURI)))
})
response, err := http.Get("http://localhost:8787/route/not/found")
assert.NoError(t, err)
bytes, err := io.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusNotFound, response.StatusCode)
assert.Equal(t, []byte("route '/route/not/found' not found."), bytes)
defer response.Body.Close()
})
t.Run("handles the default error when panic handler is provided", func(t *testing.T) {
r.OnPanic(func(_ http.ResponseWriter, _ *http.Request, err any) {
e, ok := err.(error)
assert.True(t, ok)
assert.EqualError(t, e, tests.ErrUnknown.Error())
})
r.handleError(tests.NewMockResponseWriter(), &http.Request{}, tests.ErrUnknown)
})
}