|
| 1 | +package kid |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func registerHandlers(g Group) { |
| 13 | + g.Get("/path", func(c *Context) error { |
| 14 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 15 | + }) |
| 16 | + |
| 17 | + g.Post("/path", func(c *Context) error { |
| 18 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 19 | + }) |
| 20 | + |
| 21 | + g.Patch("/path", func(c *Context) error { |
| 22 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 23 | + }) |
| 24 | + |
| 25 | + g.Put("/path", func(c *Context) error { |
| 26 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 27 | + }) |
| 28 | + |
| 29 | + g.Delete("/path", func(c *Context) error { |
| 30 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 31 | + }) |
| 32 | + |
| 33 | + g.Connect("/path", func(c *Context) error { |
| 34 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 35 | + }) |
| 36 | + |
| 37 | + g.Trace("/path", func(c *Context) error { |
| 38 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 39 | + }) |
| 40 | + |
| 41 | + g.Options("/path", func(c *Context) error { |
| 42 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 43 | + }) |
| 44 | + |
| 45 | + g.Head("/path", func(c *Context) error { |
| 46 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 47 | + }) |
| 48 | + |
| 49 | + g.Any("/any", func(c *Context) error { |
| 50 | + return c.JSON(http.StatusOK, Map{"method": c.Request().Method}) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestNewGroup(t *testing.T) { |
| 55 | + k := New() |
| 56 | + |
| 57 | + g := newGroup(k, "/v1") |
| 58 | + assert.Equal(t, k, g.kid) |
| 59 | + assert.Equal(t, "/v1", g.prefix) |
| 60 | + assert.Nil(t, g.middlewares) |
| 61 | + |
| 62 | + g = newGroup(k, "/v1", nil) |
| 63 | + assert.NotNil(t, g.middlewares) |
| 64 | + assert.Len(t, g.middlewares, 1) |
| 65 | +} |
| 66 | + |
| 67 | +func TestGroup_combineMiddlewares(t *testing.T) { |
| 68 | + k := New() |
| 69 | + |
| 70 | + g := newGroup(k, "/v1") |
| 71 | + |
| 72 | + middlewares := g.combineMiddlewares(nil) |
| 73 | + assert.Nil(t, middlewares) |
| 74 | + assert.Len(t, middlewares, 0) |
| 75 | + |
| 76 | + middlewares = g.combineMiddlewares([]MiddlewareFunc{nil}) |
| 77 | + assert.NotNil(t, middlewares) |
| 78 | + assert.Len(t, middlewares, 1) |
| 79 | + |
| 80 | + g.middlewares = []MiddlewareFunc{nil, nil} |
| 81 | + |
| 82 | + middlewares = g.combineMiddlewares(nil) |
| 83 | + assert.NotNil(t, middlewares) |
| 84 | + assert.Len(t, middlewares, 2) |
| 85 | + |
| 86 | + middlewares = g.combineMiddlewares([]MiddlewareFunc{nil}) |
| 87 | + assert.NotNil(t, middlewares) |
| 88 | + assert.Len(t, middlewares, 3) |
| 89 | +} |
| 90 | + |
| 91 | +func TestGroup_Add(t *testing.T) { |
| 92 | + k := New() |
| 93 | + g := newGroup(k, "/v1") |
| 94 | + |
| 95 | + assert.PanicsWithValue(t, "handler cannot be nil", func() { |
| 96 | + g.Add("/", nil, []string{http.MethodGet, http.MethodPost}) |
| 97 | + }) |
| 98 | + |
| 99 | + g.Add("/test", func(c *Context) error { |
| 100 | + return c.JSON(http.StatusCreated, Map{"message": c.Request().Method}) |
| 101 | + }, []string{http.MethodGet, http.MethodPost}) |
| 102 | + |
| 103 | + assert.Equal(t, 1, len(k.router.routes)) |
| 104 | + assert.Equal(t, 2, len(k.router.routes[0].methods)) |
| 105 | + assert.Equal(t, 0, len(k.router.routes[0].middlewares)) |
| 106 | + assert.Equal(t, []string{http.MethodGet, http.MethodPost}, k.router.routes[0].methods) |
| 107 | + |
| 108 | + testCases := []struct { |
| 109 | + req *http.Request |
| 110 | + res *httptest.ResponseRecorder |
| 111 | + expectedMethod string |
| 112 | + }{ |
| 113 | + {req: httptest.NewRequest(http.MethodPost, "/v1/test", nil), res: httptest.NewRecorder(), expectedMethod: http.MethodPost}, |
| 114 | + {req: httptest.NewRequest(http.MethodGet, "/v1/test", nil), res: httptest.NewRecorder(), expectedMethod: http.MethodGet}, |
| 115 | + } |
| 116 | + |
| 117 | + for _, testCase := range testCases { |
| 118 | + t.Run(testCase.expectedMethod, func(t *testing.T) { |
| 119 | + k.ServeHTTP(testCase.res, testCase.req) |
| 120 | + |
| 121 | + assert.Equal(t, http.StatusCreated, testCase.res.Code) |
| 122 | + assert.Equal(t, "application/json", testCase.res.Header().Get("Content-Type")) |
| 123 | + assert.Equal(t, fmt.Sprintf("{\"message\":%q}\n", testCase.expectedMethod), testCase.res.Body.String()) |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestGroup_Methods(t *testing.T) { |
| 129 | + k := New() |
| 130 | + g := newGroup(k, "/v1") |
| 131 | + |
| 132 | + registerHandlers(g) |
| 133 | + |
| 134 | + target := "/v1/path" |
| 135 | + any := "/v1/any" |
| 136 | + |
| 137 | + testCases := []struct { |
| 138 | + req *http.Request |
| 139 | + res *httptest.ResponseRecorder |
| 140 | + expectedMethod string |
| 141 | + }{ |
| 142 | + {req: httptest.NewRequest(http.MethodGet, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodGet}, |
| 143 | + {req: httptest.NewRequest(http.MethodPost, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodPost}, |
| 144 | + {req: httptest.NewRequest(http.MethodPut, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodPut}, |
| 145 | + {req: httptest.NewRequest(http.MethodPatch, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodPatch}, |
| 146 | + {req: httptest.NewRequest(http.MethodDelete, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodDelete}, |
| 147 | + {req: httptest.NewRequest(http.MethodOptions, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodOptions}, |
| 148 | + {req: httptest.NewRequest(http.MethodConnect, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodConnect}, |
| 149 | + {req: httptest.NewRequest(http.MethodTrace, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodTrace}, |
| 150 | + {req: httptest.NewRequest(http.MethodHead, target, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodHead}, |
| 151 | + |
| 152 | + {req: httptest.NewRequest(http.MethodGet, any, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodGet}, |
| 153 | + {req: httptest.NewRequest(http.MethodDelete, any, nil), res: httptest.NewRecorder(), expectedMethod: http.MethodDelete}, |
| 154 | + } |
| 155 | + |
| 156 | + for _, testCase := range testCases { |
| 157 | + t.Run(testCase.expectedMethod, func(t *testing.T) { |
| 158 | + k.ServeHTTP(testCase.res, testCase.req) |
| 159 | + |
| 160 | + assert.Equal(t, http.StatusOK, testCase.res.Code) |
| 161 | + assert.Equal(t, "application/json", testCase.res.Header().Get("Content-Type")) |
| 162 | + assert.Equal(t, fmt.Sprintf("{\"method\":%q}\n", testCase.expectedMethod), testCase.res.Body.String()) |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestGroup_Group(t *testing.T) { |
| 168 | + k := New() |
| 169 | + |
| 170 | + g := newGroup(k, "/v1") |
| 171 | + |
| 172 | + nestedG := g.Group("/api") |
| 173 | + assert.Equal(t, k, nestedG.kid) |
| 174 | + assert.Equal(t, "/v1/api", nestedG.prefix) |
| 175 | + assert.Nil(t, nestedG.middlewares) |
| 176 | + |
| 177 | + nestedG = g.Group("/api", nil) |
| 178 | + assert.NotNil(t, nestedG.middlewares) |
| 179 | + assert.Len(t, nestedG.middlewares, 1) |
| 180 | +} |
| 181 | + |
| 182 | +func TestGroup_Add_NestedGroups(t *testing.T) { |
| 183 | + k := New() |
| 184 | + g := newGroup(k, "/v1") |
| 185 | + nestedG := g.Group("/api") |
| 186 | + |
| 187 | + g.Add("/test", func(c *Context) error { |
| 188 | + return c.JSON(http.StatusCreated, Map{"message": c.Request().Method}) |
| 189 | + }, []string{http.MethodPost}) |
| 190 | + |
| 191 | + nestedG.Add("/{var}", func(c *Context) error { |
| 192 | + return c.JSON(http.StatusCreated, Map{"message": c.Param("var")}) |
| 193 | + }, []string{http.MethodPost}) |
| 194 | + |
| 195 | + testCases := []struct { |
| 196 | + req *http.Request |
| 197 | + res *httptest.ResponseRecorder |
| 198 | + name, expectedMessage string |
| 199 | + }{ |
| 200 | + { |
| 201 | + name: "group", |
| 202 | + req: httptest.NewRequest(http.MethodPost, "/v1/test", nil), |
| 203 | + res: httptest.NewRecorder(), |
| 204 | + expectedMessage: "{\"message\":\"POST\"}\n", |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "nested_group", |
| 208 | + req: httptest.NewRequest(http.MethodPost, "/v1/api/test", nil), |
| 209 | + res: httptest.NewRecorder(), |
| 210 | + expectedMessage: "{\"message\":\"test\"}\n", |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + for _, testCase := range testCases { |
| 215 | + t.Run(testCase.name, func(t *testing.T) { |
| 216 | + k.ServeHTTP(testCase.res, testCase.req) |
| 217 | + |
| 218 | + assert.Equal(t, http.StatusCreated, testCase.res.Code) |
| 219 | + assert.Equal(t, "application/json", testCase.res.Header().Get("Content-Type")) |
| 220 | + assert.Equal(t, testCase.expectedMessage, testCase.res.Body.String()) |
| 221 | + }) |
| 222 | + } |
| 223 | +} |
0 commit comments