Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Controllers / ControllerWithAccessibleThroughTestCase.cs
blob1abd55e61fbd3496da36d02f71b3c0b8bbde4d98
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using NUnit.Framework;
19 using Test;
21 [TestFixture]
22 public class ControllerWithAccessibleThroughTestCase
24 private MockEngineContext engineContext;
25 private ViewEngineManagerStub viewEngStub;
26 private MockServices services;
27 private MockRequest request;
28 private MockResponse response;
30 [SetUp]
31 public void Init()
33 request = new MockRequest();
34 response = new MockResponse();
35 services = new MockServices();
36 viewEngStub = new ViewEngineManagerStub();
37 services.ViewEngineManager = viewEngStub;
38 engineContext = new MockEngineContext(request, response, services, null);
41 [Test]
42 public void ActionWithGetOnlyAttributeCanBeInvokedWithGET()
44 AccThrController controller = new AccThrController();
46 request.HttpMethod = "GET";
48 IControllerContext context = services.ControllerContextFactory.
49 Create("", "home", "GetOnly", services.ControllerDescriptorProvider.BuildDescriptor(controller));
51 controller.Process(engineContext, context);
53 Assert.AreEqual("home\\GetOnly", viewEngStub.TemplateRendered);
56 [Test,
57 ExpectedException(typeof(MonoRailException),
58 "Access to the action [GetOnly] on controller [home] is not allowed to the http verb [POST].")]
59 public void ActionWithGetOnlyAttributeCannotBeInvokedWithPOST()
61 AccThrController controller = new AccThrController();
63 request.HttpMethod = "POST";
65 IControllerContext context = services.ControllerContextFactory.
66 Create("", "home", "GetOnly", services.ControllerDescriptorProvider.BuildDescriptor(controller));
68 try
70 controller.Process(engineContext, context);
72 catch(Exception)
74 Assert.AreEqual(403, response.StatusCode);
75 Assert.AreEqual("Forbidden", response.StatusDescription);
77 throw;
81 [Test,
82 ExpectedException(typeof(MonoRailException),
83 "Access to the action [PostOnly] on controller [home] is not allowed to the http verb [GET].")]
84 public void ActionWithPostOnlyAttributeCannotBeInvokedWithGET()
86 AccThrController controller = new AccThrController();
88 request.HttpMethod = "GET";
90 IControllerContext context = services.ControllerContextFactory.
91 Create("", "home", "PostOnly", services.ControllerDescriptorProvider.BuildDescriptor(controller));
93 try
95 controller.Process(engineContext, context);
97 catch(Exception)
99 Assert.AreEqual(403, response.StatusCode);
100 Assert.AreEqual("Forbidden", response.StatusDescription);
102 throw;
106 [Test]
107 public void ActionWithGetOrPostOnlyAttributeCanBeInvokedWithGETAndPOST()
109 AccThrController controller = new AccThrController();
111 IControllerContext context = services.ControllerContextFactory.
112 Create("", "home", "GetAndPost", services.ControllerDescriptorProvider.BuildDescriptor(controller));
114 request.HttpMethod = "GET";
115 controller.Process(engineContext, context);
116 Assert.AreEqual("home\\GetAndPost", viewEngStub.TemplateRendered);
118 request.HttpMethod = "POST";
119 controller.Process(engineContext, context);
120 Assert.AreEqual("home\\GetAndPost", viewEngStub.TemplateRendered);
123 #region Controllers
125 private class AccThrController : Controller
127 [AccessibleThrough(Verb.Get)]
128 public void GetOnly()
132 [AccessibleThrough(Verb.Post)]
133 public void PostOnly()
137 [AccessibleThrough(Verb.Get | Verb.Post)]
138 public void GetAndPost()
143 #endregion