1
// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Tests
.Controllers
18 using Castle
.MonoRail
.Framework
.Services
;
21 using NUnit
.Framework
;
25 public class ControllerTestCase
27 private MockEngineContext engineContext
;
28 private ViewEngineManagerStub viewEngStub
;
29 private MockServices services
;
30 private MockResponse response
;
35 MockRequest request
= new MockRequest();
36 response
= new MockResponse();
37 services
= new MockServices();
38 viewEngStub
= new ViewEngineManagerStub();
39 services
.ViewEngineManager
= viewEngStub
;
40 engineContext
= new MockEngineContext(request
, response
, services
, null);
44 public void ControllerCallsInitialize()
46 ControllerWithInitialize controller
= new ControllerWithInitialize();
48 ControllerContext context
= new ControllerContext("controller", "", "action1", new ControllerMetaDescriptor());
50 controller
.Process(engineContext
, context
);
52 Assert
.IsTrue(controller
.Initialized
);
55 [Test
, ExpectedException(typeof(MonoRailException
), "Could not find action named NonExistentAction on controller \\home")]
56 public void InvokingNonExistingActionResultsIn404()
58 ControllerAndViews controller
= new ControllerAndViews();
60 IControllerContext context
= new DefaultControllerContextFactory().
61 Create("", "home", "NonExistentAction", new ControllerMetaDescriptor());
65 controller
.Process(engineContext
, context
);
67 catch(MonoRailException
)
69 Assert
.AreEqual(404, response
.StatusCode
);
76 public void RendersViewByDefault()
78 ControllerAndViews controller
= new ControllerAndViews();
80 IControllerContext context
= services
.ControllerContextFactory
.
81 Create("", "home", "EmptyAction", new ControllerMetaDescriptor());
83 controller
.Process(engineContext
, context
);
85 Assert
.AreEqual(200, response
.StatusCode
);
86 Assert
.AreEqual("OK", response
.StatusDescription
);
87 Assert
.AreEqual("home\\EmptyAction", viewEngStub
.TemplateRendered
);
91 public void ControllerCanOverrideView()
93 ControllerAndViews controller
= new ControllerAndViews();
95 IControllerContext context
= services
.ControllerContextFactory
.
96 Create("", "home", "ActionWithViewOverride", new ControllerMetaDescriptor());
98 controller
.Process(engineContext
, context
);
100 Assert
.AreEqual(200, response
.StatusCode
);
101 Assert
.AreEqual("OK", response
.StatusDescription
);
102 Assert
.AreEqual("home\\SomethingElse", viewEngStub
.TemplateRendered
);
106 public void ControllerCanCancelView()
108 ControllerAndViews controller
= new ControllerAndViews();
110 IControllerContext context
= services
.ControllerContextFactory
.
111 Create("", "home", "CancelsTheView", new ControllerMetaDescriptor());
113 controller
.Process(engineContext
, context
);
115 Assert
.AreEqual(200, response
.StatusCode
);
116 Assert
.AreEqual("OK", response
.StatusDescription
);
117 Assert
.IsNull(viewEngStub
.TemplateRendered
);
121 public void DefaultActionIsRun_AttributeOnMethod()
123 ControllerWithDefMethodOnAction controller
= new ControllerWithDefMethodOnAction();
125 IControllerContext context
= services
.ControllerContextFactory
.
126 Create("", "home", "index", services
.ControllerDescriptorProvider
.BuildDescriptor(controller
));
128 controller
.Process(engineContext
, context
);
130 Assert
.IsTrue(controller
.DefExecuted
);
131 Assert
.AreEqual("home\\index", viewEngStub
.TemplateRendered
);
132 Assert
.AreEqual(200, response
.StatusCode
);
133 Assert
.AreEqual("OK", response
.StatusDescription
);
137 public void DefaultActionIsRun_AttributeOnClass()
139 ControllerWithDefaultActionAttribute controller
= new ControllerWithDefaultActionAttribute();
141 IControllerContext context
= services
.ControllerContextFactory
.
142 Create("", "home", "index", services
.ControllerDescriptorProvider
.BuildDescriptor(controller
));
144 controller
.Process(engineContext
, context
);
146 Assert
.IsTrue(controller
.DefExecuted
);
147 Assert
.AreEqual("home\\index", viewEngStub
.TemplateRendered
);
148 Assert
.AreEqual(200, response
.StatusCode
);
149 Assert
.AreEqual("OK", response
.StatusDescription
);
153 public void AllServiceInterfacesAreInvokedForAHelperSoItIsContextualized()
155 ControllerWithCustomHelper controller
= new ControllerWithCustomHelper();
157 IControllerContext context
= services
.ControllerContextFactory
.
158 Create("", "home", "index", services
.ControllerDescriptorProvider
.BuildDescriptor(controller
));
160 engineContext
.CurrentController
= controller
;
161 engineContext
.CurrentControllerContext
= context
;
163 controller
.Process(engineContext
, context
);
165 MyCustomHelper helper
= (MyCustomHelper
) context
.Helpers
["MyCustomHelper"];
166 Assert
.IsTrue(helper
.Service1Invoked
);
167 Assert
.IsTrue(helper
.Service2Invoked
);
168 Assert
.IsTrue(helper
.SetContextInvoked
);
169 Assert
.IsTrue(helper
.SetControllerInvoked
);
174 private class ControllerWithInitialize
: Controller
176 private bool initialized
;
178 protected override void Initialize()
183 public bool Initialized
185 get { return initialized; }
188 public void Action1()
193 private class ControllerAndViews
: Controller
195 public void EmptyAction()
199 public void ActionWithViewOverride()
201 RenderView("SomethingElse");
204 public void CancelsTheView()
210 private class ControllerWithDefMethodOnAction
: Controller
212 private bool defExecuted
;
214 public void EmptyAction()
219 public void Default()
224 public bool DefExecuted
226 get { return defExecuted; }
230 [DefaultAction("Default")]
231 class ControllerWithDefaultActionAttribute
: Controller
233 private bool defExecuted
;
235 public void Default()
240 public bool DefExecuted
242 get { return defExecuted; }
246 [Helper(typeof(MyCustomHelper
))]
247 class ControllerWithCustomHelper
: Controller
254 class MyCustomHelper
: IContextAware
, IControllerAware
, IServiceEnabledComponent
, IMRServiceEnabled
256 private bool setContextInvoked
;
257 private bool setControllerInvoked
;
258 private bool service1Invoked
;
259 private bool service2Invoked
;
261 public bool SetContextInvoked
263 get { return setContextInvoked; }
266 public bool SetControllerInvoked
268 get { return setControllerInvoked; }
271 public bool Service1Invoked
273 get { return service1Invoked; }
276 public bool Service2Invoked
278 get { return service2Invoked; }
281 public void SetContext(IEngineContext context
)
283 setContextInvoked
= true;
284 Assert
.IsNotNull(context
);
287 public void SetController(IController controller
, IControllerContext context
)
289 setControllerInvoked
= true;
290 Assert
.IsNotNull(controller
);
291 Assert
.IsNotNull(context
);
294 public void Service(IServiceProvider provider
)
296 service1Invoked
= true;
297 Assert
.IsNotNull(provider
);
300 public void Service(IMonoRailServices serviceProvider
)
302 service2Invoked
= true;
303 Assert
.IsNotNull(serviceProvider
);