-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeprecated_test.go
More file actions
85 lines (73 loc) · 1.99 KB
/
deprecated_test.go
File metadata and controls
85 lines (73 loc) · 1.99 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
package jsendx
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/require"
)
func TestNewRouter(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
path string
setupRouter func(*httprouter.Router)
wantStatus int
}{
{
name: "should handle 404",
method: http.MethodGet,
path: "/not/found",
wantStatus: http.StatusNotFound,
},
{
name: "should handle 405",
method: http.MethodPost,
setupRouter: func(r *httprouter.Router) {
fn := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, http.StatusText(http.StatusOK), http.StatusOK)
})
r.Handler(http.MethodGet, "/not/allowed", fn)
},
path: "/not/allowed",
wantStatus: http.StatusMethodNotAllowed,
},
{
name: "should handle panic in handler",
method: http.MethodGet,
setupRouter: func(r *httprouter.Router) {
fn := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
panic("panicking!")
})
r.Handler(http.MethodGet, "/panic", fn)
},
path: "/panic",
wantStatus: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defaultInstrumentHandler := func(_ string, handler http.HandlerFunc) http.Handler { return handler }
params := &AppInfo{
ProgramName: "test",
ProgramVersion: "1.2.3",
ProgramRelease: "12345",
}
r := NewRouter(params, defaultInstrumentHandler)
if tt.setupRouter != nil {
tt.setupRouter(r)
}
rr := httptest.NewRecorder()
r.ServeHTTP(rr, httptest.NewRequest(tt.method, tt.path, nil))
resp := rr.Result()
require.NotNil(t, resp)
defer func() {
err := resp.Body.Close()
require.NoError(t, err, "error closing resp.Body")
}()
require.Equal(t, tt.wantStatus, resp.StatusCode, "status code got = %d, want = %d", resp.StatusCode, tt.wantStatus)
})
}
}