1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

don't create new controller instance when all fields are set-ed by the end-dev - but we keep show those fields' values as Dependencies on the BeforeActivate in order to the future custom controllers authors to be able to check if something is added as dependecy or even manually set-ed before bind their own dependecies, otherwise they could override the manually set-ing

Former-commit-id: 72d3a0f1299781ee9a5e3e35e4a543354f8cd63d
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-17 06:34:16 +02:00
parent 6120e755e8
commit 40b40fa7d3
5 changed files with 79 additions and 40 deletions

View File

@@ -433,7 +433,7 @@ func TestControllerActivateListener(t *testing.T) {
app := iris.New()
NewEngine().Controller(app, new(testControllerActivateListener))
m := NewEngine()
m.Dependencies.Add(&testBindType{ // will bind to all controllers under this .New() MVC Engine.
m.Dependencies.Add(&testBindType{
title: "my title",
})
m.Controller(app.Party("/manual"), new(testControllerActivateListener))
@@ -452,3 +452,38 @@ func TestControllerActivateListener(t *testing.T) {
e.GET("/manual2").Expect().Status(iris.StatusOK).
Body().Equal("my title")
}
type testControllerNotCreateNewDueManuallySettingAllFields struct {
TitlePointer *testBindType
}
func (c *testControllerNotCreateNewDueManuallySettingAllFields) Get() string {
return c.TitlePointer.title
}
func TestControllerNotCreateNewDueManuallySettingAllFields(t *testing.T) {
app := iris.New()
NewEngine().Controller(app, &testControllerNotCreateNewDueManuallySettingAllFields{
TitlePointer: &testBindType{
title: "my title",
},
}, func(ca *ControllerActivator) {
if n := len(ca.Dependencies.Values); n != 1 {
t.Fatalf(`expecting 1 dependency, the 'TitlePointer' which we manually insert
and the fields length is 1 so it will not create a new controller on each request
however the dependencies are available here
although the struct injector is being ignored when
creating the controller's handlers because we set it to invalidate state at "newControllerActivator"
-- got dependencies length: %d`, n)
}
if ca.IsRequestScoped() {
t.Fatalf(`this controller shouldn't be tagged used as request scoped(create new instances on each request),
it doesn't contain any dynamic value or dependencies that should be binded via the iris mvc engine`)
}
})
e := httptest.New(t, app)
e.GET("/").Expect().Status(iris.StatusOK).
Body().Equal("my title")
}