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 NUnit
.Framework
;
22 public class ControllerWithAccessibleThroughTestCase
24 private MockEngineContext engineContext
;
25 private ViewEngineManagerStub viewEngStub
;
26 private MockServices services
;
27 private MockRequest request
;
28 private MockResponse response
;
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);
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
);
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
));
70 controller
.Process(engineContext
, context
);
74 Assert
.AreEqual(403, response
.StatusCode
);
75 Assert
.AreEqual("Forbidden", response
.StatusDescription
);
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
));
95 controller
.Process(engineContext
, context
);
99 Assert
.AreEqual(403, response
.StatusCode
);
100 Assert
.AreEqual("Forbidden", response
.StatusDescription
);
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
);
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()