2017-12-16 05:38:28 +01:00
|
|
|
package mvc_test
|
2017-11-24 11:32:35 +01:00
|
|
|
|
2017-11-24 14:10:30 +01:00
|
|
|
// black-box
|
2017-11-24 11:32:35 +01:00
|
|
|
|
2017-11-24 14:10:30 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-12-10 06:00:51 +01:00
|
|
|
"reflect"
|
2017-11-24 14:10:30 +01:00
|
|
|
"testing"
|
2017-11-24 11:32:35 +01:00
|
|
|
|
2017-11-24 14:10:30 +01:00
|
|
|
"github.com/kataras/iris"
|
|
|
|
"github.com/kataras/iris/httptest"
|
2017-12-16 05:38:28 +01:00
|
|
|
|
|
|
|
. "github.com/kataras/iris/mvc"
|
2017-11-24 14:10:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// dynamic func
|
|
|
|
type testUserStruct struct {
|
|
|
|
ID int64
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBinderFunc(ctx iris.Context) testUserStruct {
|
|
|
|
id, _ := ctx.Params().GetInt64("id")
|
|
|
|
username := ctx.Params().Get("username")
|
|
|
|
return testUserStruct{
|
|
|
|
ID: id,
|
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// service
|
|
|
|
type (
|
2017-11-27 20:39:57 +01:00
|
|
|
// these TestService and TestServiceImpl could be in lowercase, unexported
|
|
|
|
// but the `Say` method should be exported however we have those exported
|
|
|
|
// because of the controller handler test.
|
|
|
|
TestService interface {
|
2017-11-24 14:10:30 +01:00
|
|
|
Say(string) string
|
|
|
|
}
|
2017-11-27 20:39:57 +01:00
|
|
|
TestServiceImpl struct {
|
2017-11-24 14:10:30 +01:00
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-27 20:39:57 +01:00
|
|
|
func (s *TestServiceImpl) Say(message string) string {
|
2017-11-24 14:10:30 +01:00
|
|
|
return s.prefix + " " + message
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:34:35 +01:00
|
|
|
var (
|
|
|
|
// binders, as user-defined
|
|
|
|
testBinderFuncUserStruct = testBinderFunc
|
2017-11-27 20:39:57 +01:00
|
|
|
testBinderService = &TestServiceImpl{prefix: "say"}
|
2017-11-24 16:34:35 +01:00
|
|
|
testBinderFuncParam = func(ctx iris.Context) string {
|
|
|
|
return ctx.Params().Get("param")
|
|
|
|
}
|
|
|
|
|
|
|
|
// consumers
|
|
|
|
// a context as first input arg, which is not needed to be binded manually,
|
|
|
|
// and a user struct which is binded to the input arg by the #1 func(ctx) any binder.
|
|
|
|
testConsumeUserHandler = func(ctx iris.Context, user testUserStruct) {
|
|
|
|
ctx.JSON(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// just one input arg, the service which is binded by the #2 service binder.
|
2017-11-27 20:39:57 +01:00
|
|
|
testConsumeServiceHandler = func(service TestService) string {
|
2017-11-24 16:34:35 +01:00
|
|
|
return service.Say("something")
|
|
|
|
}
|
|
|
|
// just one input arg, a standar string which is binded by the #3 func(ctx) any binder.
|
|
|
|
testConsumeParamHandler = func(myParam string) string {
|
|
|
|
return "param is: " + myParam
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-24 14:10:30 +01:00
|
|
|
func TestMakeHandler(t *testing.T) {
|
|
|
|
var (
|
2017-12-10 06:00:51 +01:00
|
|
|
h1 = MustMakeHandler(testConsumeUserHandler, reflect.ValueOf(testBinderFuncUserStruct))
|
|
|
|
h2 = MustMakeHandler(testConsumeServiceHandler, reflect.ValueOf(testBinderService))
|
|
|
|
h3 = MustMakeHandler(testConsumeParamHandler, reflect.ValueOf(testBinderFuncParam))
|
2017-11-24 14:10:30 +01:00
|
|
|
)
|
|
|
|
|
2017-11-24 16:34:35 +01:00
|
|
|
testAppWithMvcHandlers(t, h1, h2, h3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAppWithMvcHandlers(t *testing.T, h1, h2, h3 iris.Handler) {
|
2017-11-24 14:10:30 +01:00
|
|
|
app := iris.New()
|
|
|
|
app.Get("/{id:long}/{username:string}", h1)
|
|
|
|
app.Get("/service", h2)
|
|
|
|
app.Get("/param/{param:string}", h3)
|
|
|
|
|
|
|
|
expectedUser := testUserStruct{
|
|
|
|
ID: 42,
|
|
|
|
Username: "kataras",
|
|
|
|
}
|
|
|
|
|
|
|
|
e := httptest.New(t, app)
|
|
|
|
// 1
|
|
|
|
e.GET(fmt.Sprintf("/%d/%s", expectedUser.ID, expectedUser.Username)).Expect().Status(httptest.StatusOK).
|
|
|
|
JSON().Equal(expectedUser)
|
|
|
|
// 2
|
|
|
|
e.GET("/service").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal("say something")
|
|
|
|
// 3
|
|
|
|
e.GET("/param/the_param_value").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal("param is: the_param_value")
|
|
|
|
}
|
2017-12-14 02:30:39 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|