MCPcopy Create free account
hub / github.com/kataras/iris / TestOnAnyErrorCode

Function TestOnAnyErrorCode

core/router/status_test.go:20–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18}
19
20func TestOnAnyErrorCode(t *testing.T) {
21 app := iris.New()
22 app.Configure(iris.WithFireMethodNotAllowed)
23
24 buff := &bytes.Buffer{}
25 expectedPrintBeforeExecuteErr := "printed before error"
26
27 // with a middleware
28 app.OnAnyErrorCode(func(ctx *context.Context) {
29 buff.WriteString(expectedPrintBeforeExecuteErr)
30 ctx.Next()
31 }, defaultErrHandler)
32
33 expectedFoundResponse := "found"
34 app.Get("/found", func(ctx *context.Context) {
35 ctx.WriteString(expectedFoundResponse)
36 })
37
38 expected407 := "this should be sent, we manage the response response by ourselves"
39 app.Get("/407", func(ctx *context.Context) {
40 ctx.Record()
41 ctx.WriteString(expected407)
42 ctx.StatusCode(iris.StatusProxyAuthRequired)
43 })
44
45 e := httptest.New(t, app)
46
47 e.GET("/found").Expect().Status(iris.StatusOK).
48 Body().IsEqual(expectedFoundResponse)
49
50 e.GET("/notfound").Expect().Status(iris.StatusNotFound).
51 Body().IsEqual(http.StatusText(iris.StatusNotFound))
52
53 checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
54
55 e.POST("/found").Expect().Status(iris.StatusMethodNotAllowed).
56 Body().IsEqual(http.StatusText(iris.StatusMethodNotAllowed))
57
58 checkAndClearBuf(t, buff, expectedPrintBeforeExecuteErr)
59
60 e.GET("/407").Expect().Status(iris.StatusProxyAuthRequired).
61 Body().IsEqual(expected407)
62
63 // Test Configuration.ResetOnFireErrorCode.
64 app2 := iris.New()
65 app2.Configure(iris.WithResetOnFireErrorCode)
66
67 app2.OnAnyErrorCode(func(ctx *context.Context) {
68 buff.WriteString(expectedPrintBeforeExecuteErr)
69 ctx.Next()
70 }, defaultErrHandler)
71
72 app2.Get("/406", func(ctx *context.Context) {
73 ctx.Record()
74 ctx.WriteString("this should not be sent, only status text will be sent")
75 ctx.WriteString("the handler can handle 'rollback' of the text when error code fired because of the recorder")
76 ctx.StatusCode(iris.StatusNotAcceptable)
77 })

Callers

nothing calls this directly

Calls 12

NewFunction · 0.92
checkAndClearBufFunction · 0.85
NewMethod · 0.80
WriteStringMethod · 0.80
NextMethod · 0.80
RecordMethod · 0.80
StatusMethod · 0.80
ConfigureMethod · 0.65
OnAnyErrorCodeMethod · 0.65
GetMethod · 0.65
StatusCodeMethod · 0.65
BodyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…