2017-12-16 05:38:28 +01:00
|
|
|
package mvc_test
|
2017-11-27 20:39:57 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/kataras/iris"
|
2017-12-16 05:38:28 +01:00
|
|
|
"github.com/kataras/iris/context"
|
2017-11-27 20:39:57 +01:00
|
|
|
"github.com/kataras/iris/httptest"
|
2017-12-16 05:38:28 +01:00
|
|
|
|
|
|
|
. "github.com/kataras/iris/mvc"
|
2017-11-27 20:39:57 +01:00
|
|
|
)
|
|
|
|
|
2017-12-25 19:57:04 +01:00
|
|
|
// service
|
|
|
|
type (
|
|
|
|
// 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 {
|
|
|
|
Say(string) string
|
|
|
|
}
|
|
|
|
TestServiceImpl struct {
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *TestServiceImpl) Say(message string) string {
|
|
|
|
return s.prefix + " " + message
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
type testControllerHandle struct {
|
2017-12-16 05:38:28 +01:00
|
|
|
Ctx context.Context
|
2017-12-04 04:06:03 +01:00
|
|
|
Service TestService
|
2017-11-27 20:39:57 +01:00
|
|
|
|
|
|
|
reqField string
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:04:26 +01:00
|
|
|
func (c *testControllerHandle) BeforeActivation(b BeforeActivation) {
|
2017-12-17 23:16:10 +01:00
|
|
|
b.Handle("GET", "/histatic", "HiStatic")
|
|
|
|
b.Handle("GET", "/hiservice", "HiService")
|
|
|
|
b.Handle("GET", "/hiparam/{ps:string}", "HiParamBy")
|
|
|
|
b.Handle("GET", "/hiparamempyinput/{ps:string}", "HiParamEmptyInputBy")
|
2017-11-27 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:04:26 +01:00
|
|
|
// test `GetRoute` for custom routes.
|
|
|
|
func (c *testControllerHandle) AfterActivation(a AfterActivation) {
|
|
|
|
// change automatic parser's route change name.
|
|
|
|
rget := a.GetRoute("Get")
|
|
|
|
if rget == nil {
|
|
|
|
panic("route from function name: 'Get' doesn't exist on `AfterActivation`")
|
|
|
|
}
|
|
|
|
rget.Name = "index_route"
|
|
|
|
|
|
|
|
// change a custom route's name.
|
|
|
|
r := a.GetRoute("HiStatic")
|
|
|
|
if r == nil {
|
|
|
|
panic("route from function name: HiStatic doesn't exist on `AfterActivation`")
|
|
|
|
}
|
|
|
|
// change the name here, and test if name changed in the handler.
|
|
|
|
r.Name = "hi_static_route"
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *testControllerHandle) BeginRequest(ctx iris.Context) {
|
2017-11-27 20:39:57 +01:00
|
|
|
c.reqField = ctx.URLParam("reqfield")
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:38:28 +01:00
|
|
|
func (c *testControllerHandle) EndRequest(ctx iris.Context) {}
|
|
|
|
|
|
|
|
func (c *testControllerHandle) Get() string {
|
2017-12-30 21:04:26 +01:00
|
|
|
if c.Ctx.GetCurrentRoute().Name() != "index_route" {
|
|
|
|
return "Get's route's name didn't change on AfterActivation"
|
|
|
|
}
|
2017-12-16 05:38:28 +01:00
|
|
|
return "index"
|
2017-11-27 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *testControllerHandle) HiStatic() string {
|
2017-12-30 21:04:26 +01:00
|
|
|
if c.Ctx.GetCurrentRoute().Name() != "hi_static_route" {
|
|
|
|
return "HiStatic's route's name didn't change on AfterActivation"
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:39:57 +01:00
|
|
|
return c.reqField
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *testControllerHandle) HiService() string {
|
2017-11-27 20:39:57 +01:00
|
|
|
return c.Service.Say("hi")
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *testControllerHandle) HiParamBy(v string) string {
|
2017-11-27 20:39:57 +01:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (c *testControllerHandle) HiParamEmptyInputBy() string {
|
2017-11-27 20:39:57 +01:00
|
|
|
return "empty in but served with ctx.Params.Get('ps')=" + c.Ctx.Params().Get("ps")
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func TestControllerHandle(t *testing.T) {
|
2017-11-27 20:39:57 +01:00
|
|
|
app := iris.New()
|
2017-12-10 06:00:51 +01:00
|
|
|
|
2017-12-25 19:57:04 +01:00
|
|
|
m := New(app)
|
2017-12-27 03:15:41 +01:00
|
|
|
m.Register(&TestServiceImpl{prefix: "service:"})
|
|
|
|
m.Handle(new(testControllerHandle))
|
2017-12-15 19:28:06 +01:00
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
e := httptest.New(t, app)
|
2017-11-27 20:39:57 +01:00
|
|
|
|
|
|
|
// test the index, is not part of the current package's implementation but do it.
|
|
|
|
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal("index")
|
|
|
|
|
|
|
|
// the important things now.
|
|
|
|
|
|
|
|
// this test ensures that the BeginRequest of the controller will be
|
|
|
|
// called correctly and also the controller is binded to the first input argument
|
|
|
|
// (which is the function's receiver, if any, in this case the *testController in go).
|
|
|
|
expectedReqField := "this is a request field filled by this url param"
|
|
|
|
e.GET("/histatic").WithQuery("reqfield", expectedReqField).Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal(expectedReqField)
|
|
|
|
// this test makes sure that the binded values of the controller is handled correctly
|
|
|
|
// and can be used in a user-defined, dynamic "mvc handler".
|
|
|
|
e.GET("/hiservice").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal("service: hi")
|
|
|
|
|
|
|
|
// this worked with a temporary variadic on the resolvemethodfunc which is not
|
|
|
|
// correct design, I should split the path and params with the rest of implementation
|
|
|
|
// in order a simple template.Src can be given.
|
|
|
|
e.GET("/hiparam/value").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal("value")
|
|
|
|
e.GET("/hiparamempyinput/value").Expect().Status(httptest.StatusOK).
|
|
|
|
Body().Equal("empty in but served with ctx.Params.Get('ps')=value")
|
|
|
|
}
|