-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler_test.go
More file actions
552 lines (457 loc) · 13.9 KB
/
Copy pathhandler_test.go
File metadata and controls
552 lines (457 loc) · 13.9 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright 2015 Eryx <evorui at gmail dot com>, All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpsrv
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
func TestHandlerInfo(t *testing.T) {
tests := []struct {
name string
handler *regHandler
expected string
}{
{
name: "handler with method and pattern",
handler: ®Handler{
method: "GET",
pattern: "/test",
},
expected: "GET /test",
},
{
name: "handler with pattern only",
handler: ®Handler{
pattern: "/test",
},
expected: "/test",
},
{
name: "handler with func",
handler: ®Handler{
pattern: "/test",
handlerFunc: func(w http.ResponseWriter, r *http.Request) {},
},
expected: "/test func",
},
{
name: "handler with controller",
handler: ®Handler{
pattern: "/test",
handlerController: &handlerController{
Name: "App",
ActionName: "Index",
},
},
expected: "/test ctrl App/Index",
},
{
name: "handler with file server",
handler: ®Handler{
pattern: "/static",
handlerFileServer: &handlerFileServer{
filepath: "/path/to/static",
},
},
expected: "/static fs (/path/to/static)",
},
{
name: "handler with bin fs",
handler: ®Handler{
pattern: "/static",
handlerFileServer: &handlerFileServer{
binFs: http.Dir("/"),
},
},
expected: "/static fs (bin)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.handler.info()
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestHandlerHandleFunc(t *testing.T) {
srv := NewService()
called := false
srv.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
called = true
w.Write([]byte("test response"))
})
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
srv.router.add("/test", srv.handlers[len(srv.handlers)-1])
h, _, _ := srv.router.find(req)
h.handle(rec, req, "/test", "/test", time.Now())
if !called {
t.Error("handler function was not called")
}
}
func TestHandlerHandleNotFound(t *testing.T) {
srv := NewService()
req := httptest.NewRequest("GET", "/notfound", nil)
rec := httptest.NewRecorder()
h, _, _ := srv.router.find(req)
h.handle(rec, req, "/notfound", "/notfound", time.Now())
// Default handler returns 200 with "page not found"
if rec.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", rec.Code)
}
}
func TestHandlerModulerFind(t *testing.T) {
moduler := &handlerModuler{
actions: map[string]*handlerController{
"/hello/world": {
Name: "Hello",
ActionName: "World",
},
},
}
// Create a request with path values set
req := httptest.NewRequest("GET", "/hello/world", nil)
req.SetPathValue("controller", "hello")
req.SetPathValue("action", "world")
hc := moduler.find(req)
if hc == nil {
t.Fatal("handler controller should be found")
}
if hc.Name != "Hello" {
t.Errorf("expected name Hello, got %s", hc.Name)
}
if hc.ActionName != "World" {
t.Errorf("expected action World, got %s", hc.ActionName)
}
}
func TestHandlerModulerFindNotFound(t *testing.T) {
moduler := &handlerModuler{
actions: map[string]*handlerController{},
}
req := httptest.NewRequest("GET", "/hello/world", nil)
req.SetPathValue("controller", "hello")
req.SetPathValue("action", "world")
hc := moduler.find(req)
if hc != nil {
t.Error("handler controller should be nil for unknown action")
}
}
func TestHandlerModulerNilActions(t *testing.T) {
moduler := &handlerModuler{}
req := httptest.NewRequest("GET", "/test", nil)
req.SetPathValue("controller", "test")
req.SetPathValue("action", "index")
hc := moduler.find(req)
if hc != nil {
t.Error("handler controller should be nil when actions is nil")
}
}
func TestCompressResponseGzip(t *testing.T) {
srv := NewService()
srv.Config.CompressResponse = true
srv.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test response for compression"))
})
req := httptest.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Encoding", "gzip")
rec := httptest.NewRecorder()
srv.router.add("/test", srv.handlers[len(srv.handlers)-1])
h, _, _ := srv.router.find(req)
h.handle(rec, req, "/test", "/test", time.Now())
// Check if gzip encoding is set
if rec.Header().Get("Content-Encoding") != "gzip" {
t.Error("expected gzip content encoding")
}
}
func TestCompressResponseBrotli(t *testing.T) {
srv := NewService()
srv.Config.CompressResponse = true
srv.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test response for compression"))
})
req := httptest.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Encoding", "br")
rec := httptest.NewRecorder()
srv.router.add("/test", srv.handlers[len(srv.handlers)-1])
h, _, _ := srv.router.find(req)
h.handle(rec, req, "/test", "/test", time.Now())
// Check if brotli encoding is set
if rec.Header().Get("Content-Encoding") != "br" {
t.Error("expected brotli content encoding")
}
}
func TestCompressResponseNoEncoding(t *testing.T) {
srv := NewService()
srv.Config.CompressResponse = true
body := "test response without compression"
srv.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(body))
})
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
srv.router.add("/test", srv.handlers[len(srv.handlers)-1])
h, _, _ := srv.router.find(req)
h.handle(rec, req, "/test", "/test", time.Now())
// No compression encoding should be set
if rec.Header().Get("Content-Encoding") != "" {
t.Error("expected no content encoding")
}
}
func TestCompressResponseDisabled(t *testing.T) {
srv := NewService()
srv.Config.CompressResponse = false
body := "test response"
srv.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(body))
})
req := httptest.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Encoding", "gzip")
rec := httptest.NewRecorder()
srv.router.add("/test", srv.handlers[len(srv.handlers)-1])
h, _, _ := srv.router.find(req)
h.handle(rec, req, "/test", "/test", time.Now())
// No compression should be applied when disabled
if rec.Header().Get("Content-Encoding") != "" {
t.Error("expected no content encoding when compression is disabled")
}
if !bytes.Contains(rec.Body.Bytes(), []byte(body)) {
t.Error("response body should contain uncompressed content")
}
}
// -- ActionFunc tests --
func sampleActionOk(ctx Ctx) error {
return ctx.Send([]byte("hello from action"))
}
func sampleActionError(ctx Ctx) error {
return fmt.Errorf("something went wrong")
}
func sampleActionJson(ctx Ctx) error {
return ctx.JSON(map[string]string{"status": "ok"})
}
func TestActionFuncDispatch(t *testing.T) {
srv := NewService()
srv.regHandler(®Handler{
pattern: "/test/action-ok",
handlerAction: &handlerAction{
name: "ActionOk",
fn: sampleActionOk,
},
})
lastHandler := srv.handlers[len(srv.handlers)-1]
srv.router.add(lastHandler.pattern, lastHandler)
req := httptest.NewRequest("GET", "/test/action-ok/", nil)
rec := httptest.NewRecorder()
h, urlPath, _ := srv.router.find(req)
h.handle(rec, req, urlPath, urlPath, time.Now())
if !bytes.Contains(rec.Body.Bytes(), []byte("hello from action")) {
t.Errorf("expected body to contain 'hello from action', got %q", rec.Body.String())
}
}
func TestActionFuncDispatchError(t *testing.T) {
srv := NewService()
srv.regHandler(®Handler{
pattern: "/test/action-err",
handlerAction: &handlerAction{
name: "ActionError",
fn: sampleActionError,
},
})
lastHandler := srv.handlers[len(srv.handlers)-1]
srv.router.add(lastHandler.pattern, lastHandler)
req := httptest.NewRequest("GET", "/test/action-err/", nil)
rec := httptest.NewRecorder()
h, urlPath, _ := srv.router.find(req)
h.handle(rec, req, urlPath, urlPath, time.Now())
if rec.Code != http.StatusInternalServerError {
t.Errorf("expected status 500, got %d", rec.Code)
}
if !bytes.Contains(rec.Body.Bytes(), []byte("something went wrong")) {
t.Errorf("expected body to contain error message, got %q", rec.Body.String())
}
}
func TestActionFuncDispatchJson(t *testing.T) {
srv := NewService()
srv.regHandler(®Handler{
pattern: "/test/action-json",
handlerAction: &handlerAction{
name: "ActionJson",
fn: sampleActionJson,
},
})
lastHandler := srv.handlers[len(srv.handlers)-1]
srv.router.add(lastHandler.pattern, lastHandler)
req := httptest.NewRequest("GET", "/test/action-json/", nil)
rec := httptest.NewRecorder()
h, urlPath, _ := srv.router.find(req)
h.handle(rec, req, urlPath, urlPath, time.Now())
ct := rec.Header().Get("Content-Type")
if ct != "application/json" {
t.Errorf("expected application/json, got %q", ct)
}
if !bytes.Contains(rec.Body.Bytes(), []byte(`"status":"ok"`)) {
t.Errorf("expected json body, got %q", rec.Body.String())
}
}
func TestActionFuncFiltersRun(t *testing.T) {
srv := NewService()
filterCalled := false
srv.Filters = append(srv.Filters, func(c *Controller) {
filterCalled = true
})
srv.regHandler(®Handler{
pattern: "/test/filter",
handlerAction: &handlerAction{
name: "Filter",
fn: func(ctx Ctx) error {
return ctx.Send([]byte("ok"))
},
},
})
lastHandler := srv.handlers[len(srv.handlers)-1]
srv.router.add(lastHandler.pattern, lastHandler)
req := httptest.NewRequest("GET", "/test/filter/", nil)
rec := httptest.NewRecorder()
h, urlPath, _ := srv.router.find(req)
h.handle(rec, req, urlPath, urlPath, time.Now())
if !filterCalled {
t.Error("expected filter to be called for ActionFunc")
}
}
func TestActionFuncViaModule(t *testing.T) {
mod := NewModule()
mod.RegisterAction("/api/action-ok", sampleActionOk)
mod.RegisterAction("/api/action-json", sampleActionJson)
srv := NewService()
srv.HandleModule("/v1", mod)
for _, h := range srv.handlers {
srv.router.add(h.pattern, h)
}
tests := []struct {
path string
wantBody string
}{
{"/v1/api/action-ok/", "hello from action"},
{"/v1/api/action-json/", `"status":"ok"`},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
req := httptest.NewRequest("GET", tt.path, nil)
rec := httptest.NewRecorder()
h, urlPath, _ := srv.router.find(req)
h.handle(rec, req, urlPath, urlPath, time.Now())
if !bytes.Contains(rec.Body.Bytes(), []byte(tt.wantBody)) {
t.Errorf("expected body to contain %q, got %q", tt.wantBody, rec.Body.String())
}
})
}
}
// TestControllerUrlBase verifies that UrlBase produces correct paths
// without double slashes, including when UrlBasePath is set.
func TestControllerUrlBase(t *testing.T) {
srv := NewService()
// httptest.NewRequest uses "example.com" as the default Host
const host = "http://example.com"
tests := []struct {
name string
basePath string
path string
wantSuffix string
}{
{"no_basepath", "", "/path", host + "/path"},
{"with_basepath", "app", "/path", host + "/app/path"},
{"basepath_with_slash", "/app", "/path", host + "/app/path"},
{"empty_path", "app", "", host + "/app"},
{"path_no_leading_slash", "app", "sub", host + "/app/sub"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv.Config.UrlBasePath = tt.basePath
req := httptest.NewRequest("GET", "/", nil)
c := newController(srv, newRequest(req), newResponse(httptest.NewRecorder()))
got := c.UrlBase(tt.path)
if got != tt.wantSuffix {
t.Errorf("got %q, want %q", got, tt.wantSuffix)
}
})
}
}
// TestControllerRedirectNoDoubleSlash verifies Redirect does not produce
// URLs with double slashes when UrlBasePath is set.
func TestControllerRedirectNoDoubleSlash(t *testing.T) {
srv := NewService()
srv.Config.UrlBasePath = "app"
req := httptest.NewRequest("GET", "/", nil)
rec := httptest.NewRecorder()
c := newController(srv, newRequest(req), newResponse(rec))
c.Redirect("login")
loc := rec.Header().Get("Location")
if loc != "/app/login" {
t.Errorf("expected /app/login, got %q", loc)
}
}
// VoidInitController verifies that a controller whose Init() method
// returns no values does not cause a panic.
type VoidInitController struct {
*Controller
}
func (c *VoidInitController) Init() {
}
func (c *VoidInitController) IndexAction() {
c.RenderString("inited")
}
func TestControllerVoidInit(t *testing.T) {
srv := NewService()
hc := &handlerController{
Name: "VoidInit",
ActionName: "Index",
ctrlType: reflect.TypeOf(&VoidInitController{}).Elem(),
ctrlIndexes: findControllers(reflect.TypeOf(&VoidInitController{}).Elem()),
}
h := ®Handler{
pattern: "/void-init/index/",
handlerController: hc,
service: srv,
}
srv.router.add(h.pattern, h)
req := httptest.NewRequest("GET", "/void-init/index/", nil)
rec := httptest.NewRecorder()
foundHandler, urlPath, _ := srv.router.find(req)
if foundHandler == nil {
t.Fatal("handler not found")
}
foundHandler.handle(rec, req, urlPath, urlPath, time.Now())
if !bytes.Contains(rec.Body.Bytes(), []byte("inited")) {
t.Errorf("expected body to contain 'inited', got %q", rec.Body.String())
}
}
func TestHandlerInfoAction(t *testing.T) {
h := ®Handler{
pattern: "/test",
handlerAction: &handlerAction{
name: "ListUsers",
fn: func(ctx Ctx) error { return nil },
},
}
expected := "/test action ListUsers"
if got := h.info(); got != expected {
t.Errorf("expected %q, got %q", expected, got)
}
}