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

Function TestPayloadBinding

hero/handler_test.go:129–173  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

127}
128
129func TestPayloadBinding(t *testing.T) {
130 h := New()
131
132 ptrHandler := h.Handler(func(input *testUserStruct /* ptr */) string {
133 return input.Username
134 })
135
136 valHandler := h.Handler(func(input testUserStruct) string {
137 return input.Username
138 })
139
140 h.GetErrorHandler = func(iris.Context) ErrorHandler {
141 return ErrorHandlerFunc(func(ctx iris.Context, err error) {
142 if iris.IsErrPath(err) {
143 return // continue.
144 }
145
146 ctx.StopWithError(iris.StatusBadRequest, err)
147 })
148 }
149
150 app := iris.New()
151 app.Get("/", ptrHandler)
152 app.Post("/", ptrHandler)
153 app.Post("/2", valHandler)
154
155 e := httptest.New(t, app)
156
157 // JSON
158 e.POST("/").WithJSON(iris.Map{"username": "makis"}).Expect().Status(httptest.StatusOK).Body().IsEqual("makis")
159 e.POST("/2").WithJSON(iris.Map{"username": "kataras"}).Expect().Status(httptest.StatusOK).Body().IsEqual("kataras")
160
161 // FORM (url-encoded)
162 e.POST("/").WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().IsEqual("makis")
163 // FORM (multipart)
164 e.POST("/").WithMultipart().WithFormField("username", "makis").Expect().Status(httptest.StatusOK).Body().IsEqual("makis")
165 // FORM: test ErrorHandler skip the ErrPath.
166 e.POST("/").WithMultipart().WithFormField("username", "makis").WithFormField("unknown", "continue").
167 Expect().Status(httptest.StatusOK).Body().IsEqual("makis")
168
169 // POST URL query.
170 e.POST("/").WithQuery("username", "makis").Expect().Status(httptest.StatusOK).Body().IsEqual("makis")
171 // GET URL query.
172 e.GET("/").WithQuery("username", "makis").Expect().Status(httptest.StatusOK).Body().IsEqual("makis")
173}
174
175/* Author's notes:
176If aksed or required by my company, make the following test to pass but think downsides of code complexity and performance-cost

Callers

nothing calls this directly

Calls 10

NewFunction · 0.92
StopWithErrorMethod · 0.80
NewMethod · 0.80
StatusMethod · 0.80
NewFunction · 0.70
ErrorHandlerFuncFuncType · 0.70
GetMethod · 0.65
PostMethod · 0.65
BodyMethod · 0.65
HandlerMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…