add test for binding a whole function as an input argument on the handler's function - worked

Former-commit-id: 410ffdf44057ce57d5d280aa80ef0c9884f275b2
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-12-14 03:30:39 +02:00
parent 43c330f338
commit d72c649441

View File

@ -101,3 +101,29 @@ func testAppWithMvcHandlers(t *testing.T, h1, h2, h3 iris.Handler) {
e.GET("/param/the_param_value").Expect().Status(httptest.StatusOK).
Body().Equal("param is: the_param_value")
}
// TestBindFunctionAsFunctionInputArgument tests to bind
// a whole dynamic function based on the current context
// as an input argument in the mvc-like handler's function.
func TestBindFunctionAsFunctionInputArgument(t *testing.T) {
app := iris.New()
postsBinder := func(ctx iris.Context) func(string) string {
return ctx.PostValue // or FormValue, the same here.
}
h := MustMakeHandler(func(get func(string) string) string {
// send the `ctx.PostValue/FormValue("username")` value
// to the client.
return get("username")
},
// bind the function binder.
reflect.ValueOf(postsBinder))
app.Post("/", h)
e := httptest.New(t, app)
expectedUsername := "kataras"
e.POST("/").WithFormField("username", expectedUsername).
Expect().Status(iris.StatusOK).Body().Equal(expectedUsername)
}