-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter_test.go
88 lines (74 loc) · 2.26 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
// SPDX-FileCopyrightText: 2018-2024 caixw
//
// SPDX-License-Identifier: MIT
package web
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/issue9/assert/v4"
)
func TestRouters(t *testing.T) {
a := assert.New(t, false)
s := newTestServer(a)
rs := s.Routers()
a.NotNil(rs)
r1 := rs.New("r1", nil)
a.NotNil(r1)
a.Equal(r1, rs.Get("r1")).
Length(rs.Routers(), 1)
rs.Remove("r1")
a.Nil(rs.Get("r1")).
Length(rs.Routers(), 0)
}
func TestRouters_Handle(t *testing.T) {
a := assert.New(t, false)
s := newTestServer(a)
rs := s.Routers()
a.NotNil(rs)
router := rs.New("def", nil, WithRecovery(http.StatusInternalServerError, s.Logs().ERROR()))
a.NotNil(router)
router.Get("/get1", func(ctx *Context) Responser {
return OK("ok")
})
router.Get("/panic-http-error", func(ctx *Context) Responser {
panic(NewError(http.StatusConflict, errors.New("panic")))
})
router.Get("/panic-error", func(ctx *Context) Responser {
panic(errors.New("panic"))
})
router.Get("/panic-string", func(ctx *Context) Responser {
panic("panic")
})
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/get1", nil)
router.ServeHTTP(w, r)
a.Equal(w.Result().StatusCode, http.StatusOK)
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/not-exists", nil)
router.ServeHTTP(w, r)
a.Equal(w.Result().StatusCode, http.StatusNotFound)
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodPatch, "/get1", nil)
router.ServeHTTP(w, r)
a.Equal(w.Result().StatusCode, http.StatusMethodNotAllowed)
s.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/panic-http-error", nil)
router.ServeHTTP(w, r)
a.Equal(w.Result().StatusCode, http.StatusConflict).
Contains(s.logBuf.String(), "router_test.go:44")
s.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/panic-error", nil)
router.ServeHTTP(w, r)
a.Equal(w.Result().StatusCode, http.StatusInternalServerError).
Contains(s.logBuf.String(), "router_test.go:47")
s.logBuf.Reset()
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/panic-string", nil)
router.ServeHTTP(w, r)
a.Equal(w.Result().StatusCode, http.StatusInternalServerError).
Contains(s.logBuf.String(), "router_test.go:50")
}