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

Function TestPartyOnErrorCode

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

Source from the content-addressed store, hash-verified

93}
94
95func TestPartyOnErrorCode(t *testing.T) {
96 app := iris.New()
97 app.Configure(iris.WithFireMethodNotAllowed)
98
99 globalNotFoundResponse := "custom not found"
100 app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
101 ctx.WriteString(globalNotFoundResponse)
102 })
103
104 globalMethodNotAllowedResponse := "global: method not allowed"
105 app.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) {
106 ctx.WriteString(globalMethodNotAllowedResponse)
107 })
108
109 app.Get("/path", h)
110
111 usersResponse := "users: method allowed"
112 users := app.Party("/users")
113 users.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) {
114 ctx.WriteString(usersResponse)
115 })
116 users.Get("/", h)
117 write400 := func(ctx iris.Context) { ctx.StatusCode(iris.StatusBadRequest) }
118 // test setting the error code from a handler.
119 users.Get("/badrequest", write400)
120
121 usersuserResponse := "users:user: method allowed"
122 user := users.Party("/{id:int}")
123 user.OnErrorCode(iris.StatusMethodNotAllowed, func(ctx iris.Context) {
124 ctx.WriteString(usersuserResponse)
125 })
126 usersuserNotFoundResponse := "users:user: not found"
127 user.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
128 ctx.WriteString(usersuserNotFoundResponse)
129 })
130 user.Get("/", h)
131 user.Get("/ab/badrequest", write400)
132
133 friends := users.Party("/friends")
134 friends.Get("/{id:int}", h)
135
136 e := httptest.New(t, app)
137
138 e.GET("/notfound").Expect().Status(iris.StatusNotFound).Body().IsEqual(globalNotFoundResponse)
139 e.POST("/path").Expect().Status(iris.StatusMethodNotAllowed).Body().IsEqual(globalMethodNotAllowedResponse)
140 e.GET("/path").Expect().Status(iris.StatusOK).Body().IsEqual("/path")
141
142 e.POST("/users").Expect().Status(iris.StatusMethodNotAllowed).
143 Body().IsEqual(usersResponse)
144
145 e.POST("/users/42").Expect().Status(iris.StatusMethodNotAllowed).
146 Body().IsEqual(usersuserResponse)
147
148 e.GET("/users/42").Expect().Status(iris.StatusOK).
149 Body().IsEqual("/users/42")
150 e.GET("/users/ab").Expect().Status(iris.StatusNotFound).Body().IsEqual(usersuserNotFoundResponse)
151 // inherit the parent.
152 e.GET("/users/42/friends/dsa").Expect().Status(iris.StatusNotFound).Body().IsEqual(usersuserNotFoundResponse)

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
NewMethod · 0.80
WriteStringMethod · 0.80
StatusMethod · 0.80
ConfigureMethod · 0.65
OnErrorCodeMethod · 0.65
GetMethod · 0.65
PartyMethod · 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…