From d72c649441ea241a9e3c2bf5ac5180a1e0f52008 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 14 Dec 2017 03:30:39 +0200 Subject: [PATCH] add test for binding a whole function as an input argument on the handler's function - worked Former-commit-id: 410ffdf44057ce57d5d280aa80ef0c9884f275b2 --- mvc2/handler_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mvc2/handler_test.go b/mvc2/handler_test.go index ee9962fd..205b1602 100644 --- a/mvc2/handler_test.go +++ b/mvc2/handler_test.go @@ -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) +}