forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_test.go
165 lines (135 loc) · 6.36 KB
/
status_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
package iris_test
import (
"testing"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/gorillamux"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/httptest"
)
func testStatusErrors(routerPolicy iris.Policy, t *testing.T) {
var (
notFoundMessage = "Iris custom message for 404 not found"
internalServerMessage = "Iris custom message for 500 internal server error"
testRoutesCustomErrors = []testRoute{
// NOT FOUND CUSTOM ERRORS - not registered
{"GET", "/test_get_nofound_custom", "/test_get_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"POST", "/test_post_nofound_custom", "/test_post_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"PUT", "/test_put_nofound_custom", "/test_put_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"DELETE", "/test_delete_nofound_custom", "/test_delete_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"HEAD", "/test_head_nofound_custom", "/test_head_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"OPTIONS", "/test_options_nofound_custom", "/test_options_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"CONNECT", "/test_connect_nofound_custom", "/test_connect_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"PATCH", "/test_patch_nofound_custom", "/test_patch_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
{"TRACE", "/test_trace_nofound_custom", "/test_trace_nofound_custom", "", notFoundMessage, 404, false, nil, nil},
// SERVER INTERNAL ERROR 500 PANIC CUSTOM ERRORS - registered
{"GET", "/test_get_panic_custom", "/test_get_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"POST", "/test_post_panic_custom", "/test_post_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"PUT", "/test_put_panic_custom", "/test_put_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"DELETE", "/test_delete_panic_custom", "/test_delete_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"HEAD", "/test_head_panic_custom", "/test_head_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"OPTIONS", "/test_options_panic_custom", "/test_options_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"CONNECT", "/test_connect_panic_custom", "/test_connect_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"PATCH", "/test_patch_panic_custom", "/test_patch_panic_custom", "", internalServerMessage, 500, true, nil, nil},
{"TRACE", "/test_trace_panic_custom", "/test_trace_panic_custom", "", internalServerMessage, 500, true, nil, nil},
}
)
app := iris.New()
app.Adapt(routerPolicy)
// first register the testRoutes needed
for _, r := range testRoutesCustomErrors {
if r.Register {
app.HandleFunc(r.Method, r.Path, func(ctx *iris.Context) {
ctx.EmitError(r.Status)
})
}
}
// register the custom errors
app.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
ctx.Writef("%s", notFoundMessage)
})
app.OnError(iris.StatusInternalServerError, func(ctx *iris.Context) {
ctx.Writef("%s", internalServerMessage)
})
// create httpexpect instance that will call fasthtpp.RequestHandler directly
e := httptest.New(app, t)
// run the tests
for _, r := range testRoutesCustomErrors {
e.Request(r.Method, r.RequestPath).
Expect().
Status(r.Status).Body().Equal(r.Body)
}
}
// Author's note:
// these are easy tests and can be done without special path parameters,
// so we have them here for all possible router policies.
func TestStatusErrors(t *testing.T) {
testStatusErrors(httprouter.New(), t)
testStatusErrors(gorillamux.New(), t)
}
func testStatusMethodNotAllowed(routerPolicy iris.Policy, t *testing.T) {
app := iris.New()
app.Adapt(routerPolicy)
app.Config.FireMethodNotAllowed = true
h := func(ctx *iris.Context) {
ctx.WriteString(ctx.Method())
}
app.OnError(iris.StatusMethodNotAllowed, func(ctx *iris.Context) {
ctx.WriteString("Hello from my custom 405 page")
})
app.Get("/mypath", h)
app.Put("/mypath", h)
e := httptest.New(app, t)
e.GET("/mypath").Expect().Status(iris.StatusOK).Body().Equal("GET")
e.PUT("/mypath").Expect().Status(iris.StatusOK).Body().Equal("PUT")
// this should fail with 405 and catch by the custom http error
e.POST("/mypath").Expect().Status(iris.StatusMethodNotAllowed).Body().Equal("Hello from my custom 405 page")
}
func TestStatusMethodNotAllowed(t *testing.T) {
testStatusMethodNotAllowed(httprouter.New(), t)
testStatusMethodNotAllowed(gorillamux.New(), t)
}
func testRegisterRegex(routerPolicy iris.Policy, t *testing.T) {
app := iris.New()
app.Adapt(routerPolicy)
h := func(ctx *iris.Context) {
ctx.WriteString(ctx.Method())
}
app.OnError(iris.StatusNotFound, func(ctx *iris.Context) {
ctx.WriteString("root 404")
})
staticP := app.Party("/static")
{
// or app.Errors... same thing
staticP.Errors.RegisterRegex(iris.StatusNotFound, iris.HandlerFunc(func(ctx *iris.Context) {
ctx.WriteString("/static 404")
}), "/static/[0-9]+")
// simulate a StaticHandler or StaticWeb simple behavior
// in order to get a not found error from a wildcard path registered on the root path of the "/static".
// Note:
// RouteWildcardPath when you want to work on all router adaptors:httprouter=> *file, gorillamux=> {file:.*}
staticP.Get(app.RouteWildcardPath("/", "file"), func(ctx *iris.Context) {
i, err := ctx.ParamIntWildcard("file")
if i > 0 || err == nil {
// this handler supposed to accept only strings, for the sake of the test.
ctx.EmitError(iris.StatusNotFound)
return
}
ctx.SetStatusCode(iris.StatusOK)
})
}
app.Get("/mypath", h)
e := httptest.New(app, t)
// print("-------------------TESTING ")
// println(app.Config.Other[iris.RouterNameConfigKey].(string) + "-------------------")
e.GET("/mypath").Expect().Status(iris.StatusOK).Body().Equal("GET")
e.GET("/rootnotfound").Expect().Status(iris.StatusNotFound).Body().Equal("root 404")
// test found on party
e.GET("/static/itshouldbeok").Expect().Status(iris.StatusOK)
// test no found on party ( by putting at least one integer after /static)
e.GET("/static/42").Expect().Status(iris.StatusNotFound).Body().Equal("/static 404")
// println("-------------------------------------------------------")
}
func TestRegisterRegex(t *testing.T) {
testRegisterRegex(httprouter.New(), t)
testRegisterRegex(gorillamux.New(), t)
}