(t *testing.T)
| 127 | } |
| 128 | |
| 129 | func 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: |
| 176 | If aksed or required by my company, make the following test to pass but think downsides of code complexity and performance-cost |
nothing calls this directly
no test coverage detected
searching dependent graphs…