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
.Handlers
20 using Castle
.MonoRail
.Framework
.Configuration
;
21 using Castle
.MonoRail
.Framework
.Providers
;
22 using Castle
.MonoRail
.Framework
.Routing
;
23 using Castle
.MonoRail
.Framework
.Services
;
26 using NUnit
.Framework
;
30 public class MRHandlerFactoryTestCase
32 private readonly MockRepository mockRepository
= new MockRepository();
33 private MonoRailHttpHandlerFactory handlerFactory
;
34 private IServiceProviderLocator serviceProviderLocatorMock
;
35 private IMonoRailContainer container
;
36 private IControllerFactory controllerFactoryMock
;
37 private IAsyncController controllerMock
;
38 private IControllerDescriptorProvider controllerDescriptorProviderMock
;
39 private IControllerContextFactory controllerContextFactoryMock
;
44 container
= mockRepository
.CreateMock
<IMonoRailContainer
>();
45 serviceProviderLocatorMock
= mockRepository
.CreateMock
<IServiceProviderLocator
>();
46 controllerFactoryMock
= mockRepository
.CreateMock
<IControllerFactory
>();
47 controllerMock
= mockRepository
.CreateMock
<IAsyncController
>();
48 controllerDescriptorProviderMock
= mockRepository
.CreateMock
<IControllerDescriptorProvider
>();
49 controllerContextFactoryMock
= mockRepository
.CreateMock
<IControllerContextFactory
>();
51 SetupResult
.For(container
.UrlTokenizer
).Return(new DefaultUrlTokenizer());
52 SetupResult
.For(container
.UrlBuilder
).Return(new DefaultUrlBuilder());
53 SetupResult
.For(container
.EngineContextFactory
).Return(new DefaultEngineContextFactory());
54 SetupResult
.For(container
.ControllerFactory
).Return(controllerFactoryMock
);
55 SetupResult
.For(container
.ControllerContextFactory
).Return(controllerContextFactoryMock
);
56 SetupResult
.For(container
.ControllerDescriptorProvider
).Return(controllerDescriptorProviderMock
);
57 SetupResult
.For(container
.StaticResourceRegistry
).Return(new DefaultStaticResourceRegistry());
59 handlerFactory
= new MonoRailHttpHandlerFactory(serviceProviderLocatorMock
);
60 handlerFactory
.ResetState();
61 handlerFactory
.Configuration
= new MonoRailConfiguration();
62 handlerFactory
.Container
= container
;
66 public void Request_CreatesSessionfulHandler()
68 StringWriter writer
= new StringWriter();
70 HttpResponse res
= new HttpResponse(writer
);
71 HttpRequest req
= new HttpRequest(Path
.Combine(
72 AppDomain
.CurrentDomain
.BaseDirectory
, "Handlers/Files/simplerequest.txt"),
73 "http://localhost:1333/home/something", "");
74 RouteMatch routeMatch
= new RouteMatch();
75 HttpContext httpCtx
= new HttpContext(req
, res
);
76 httpCtx
.Items
[RouteMatch
.RouteMatchKey
] = routeMatch
;
78 using(mockRepository
.Record())
80 ControllerMetaDescriptor controllerDesc
= new ControllerMetaDescriptor();
81 controllerDesc
.ControllerDescriptor
= new ControllerDescriptor(typeof(Controller
), "home", "", false);
83 Expect
.Call(controllerFactoryMock
.CreateController("", "home")).IgnoreArguments().Return(controllerMock
);
84 Expect
.Call(controllerDescriptorProviderMock
.BuildDescriptor(controllerMock
)).Return(controllerDesc
);
85 ControllerContext controllerContext
= new ControllerContext();
86 controllerContext
.ControllerDescriptor
= new ControllerMetaDescriptor();
87 Expect
.Call(controllerContextFactoryMock
.Create("", "home", "something", controllerDesc
, routeMatch
)).
88 Return(controllerContext
);
91 using(mockRepository
.Playback())
93 IHttpHandler handler
= handlerFactory
.GetHandler(httpCtx
, "GET", "", "");
95 Assert
.IsNotNull(handler
);
96 Assert
.IsInstanceOfType(typeof(MonoRailHttpHandler
), handler
);
101 public void Request_CreatesSessionfulHandler_Async()
103 StringWriter writer
= new StringWriter();
105 HttpResponse res
= new HttpResponse(writer
);
106 HttpRequest req
= new HttpRequest(Path
.Combine(
107 AppDomain
.CurrentDomain
.BaseDirectory
, "Handlers/Files/simplerequest.txt"),
108 "http://localhost:1333/home/something", "");
109 RouteMatch routeMatch
= new RouteMatch();
110 HttpContext httpCtx
= new HttpContext(req
, res
);
111 httpCtx
.Items
[RouteMatch
.RouteMatchKey
] = routeMatch
;
113 using (mockRepository
.Record())
115 ControllerMetaDescriptor controllerDesc
= new ControllerMetaDescriptor();
116 controllerDesc
.ControllerDescriptor
= new ControllerDescriptor(typeof(Controller
), "home", "", false);
118 Expect
.Call(controllerFactoryMock
.CreateController("", "home")).IgnoreArguments().Return(controllerMock
);
119 Expect
.Call(controllerDescriptorProviderMock
.BuildDescriptor(controllerMock
)).Return(controllerDesc
);
120 ControllerContext controllerContext
= new ControllerContext();
121 controllerContext
.ControllerDescriptor
= new ControllerMetaDescriptor();
122 controllerContext
.Action
= "something";
123 controllerContext
.ControllerDescriptor
.Actions
["something"] = new AsyncActionPair(null, null, null);
124 Expect
.Call(controllerContextFactoryMock
.Create("", "home", "something", controllerDesc
, routeMatch
)).
125 Return(controllerContext
);
128 using (mockRepository
.Playback())
130 IHttpHandler handler
= handlerFactory
.GetHandler(httpCtx
, "GET", "", "");
132 Assert
.IsNotNull(handler
);
133 Assert
.IsInstanceOfType(typeof(AsyncMonoRailHttpHandler
), handler
);
138 public void Request_CreatesSessionlessHandler()
140 StringWriter writer
= new StringWriter();
142 HttpResponse res
= new HttpResponse(writer
);
143 HttpRequest req
= new HttpRequest(Path
.Combine(
144 AppDomain
.CurrentDomain
.BaseDirectory
, "Handlers/Files/simplerequest.txt"),
145 "http://localhost:1333/home/something", "");
146 RouteMatch routeMatch
= new RouteMatch();
147 HttpContext httpCtx
= new HttpContext(req
, res
);
148 httpCtx
.Items
[RouteMatch
.RouteMatchKey
] = routeMatch
;
150 using(mockRepository
.Record())
152 ControllerMetaDescriptor controllerDesc
= new ControllerMetaDescriptor();
153 controllerDesc
.ControllerDescriptor
= new ControllerDescriptor(typeof(Controller
), "home", "", true);
155 Expect
.Call(controllerFactoryMock
.CreateController("", "home")).IgnoreArguments().Return(controllerMock
);
156 Expect
.Call(controllerDescriptorProviderMock
.BuildDescriptor(controllerMock
)).Return(controllerDesc
);
157 ControllerContext controllerContext
= new ControllerContext();
158 controllerContext
.ControllerDescriptor
= new ControllerMetaDescriptor();
159 Expect
.Call(controllerContextFactoryMock
.Create("", "home", "something", controllerDesc
, routeMatch
)).
160 Return(controllerContext
);
163 using(mockRepository
.Playback())
165 IHttpHandler handler
= handlerFactory
.GetHandler(httpCtx
, "GET", "", "");
167 Assert
.IsNotNull(handler
);
168 Assert
.IsInstanceOfType(typeof(SessionlessMonoRailHttpHandler
), handler
);
173 public void Request_CreatesSessionlessHandler_Async()
175 StringWriter writer
= new StringWriter();
177 HttpResponse res
= new HttpResponse(writer
);
178 HttpRequest req
= new HttpRequest(Path
.Combine(
179 AppDomain
.CurrentDomain
.BaseDirectory
, "Handlers/Files/simplerequest.txt"),
180 "http://localhost:1333/home/something", "");
181 RouteMatch routeMatch
= new RouteMatch();
182 HttpContext httpCtx
= new HttpContext(req
, res
);
183 httpCtx
.Items
[RouteMatch
.RouteMatchKey
] = routeMatch
;
185 using (mockRepository
.Record())
187 ControllerMetaDescriptor controllerDesc
= new ControllerMetaDescriptor();
188 controllerDesc
.ControllerDescriptor
= new ControllerDescriptor(typeof(Controller
), "home", "", true);
190 Expect
.Call(controllerFactoryMock
.CreateController("", "home")).IgnoreArguments().Return(controllerMock
);
191 Expect
.Call(controllerDescriptorProviderMock
.BuildDescriptor(controllerMock
)).Return(controllerDesc
);
192 ControllerContext controllerContext
= new ControllerContext();
193 controllerContext
.Action
= "something";
194 controllerContext
.ControllerDescriptor
= new ControllerMetaDescriptor();
195 controllerContext
.ControllerDescriptor
.Actions
["something"] = new AsyncActionPair(null,null,null);
196 Expect
.Call(controllerContextFactoryMock
.Create("", "home", "something", controllerDesc
, routeMatch
)).
197 Return(controllerContext
);
200 using (mockRepository
.Playback())
202 IHttpHandler handler
= handlerFactory
.GetHandler(httpCtx
, "GET", "", "");
204 Assert
.IsNotNull(handler
);
205 Assert
.IsInstanceOfType(typeof(AsyncSessionlessMonoRailHttpHandler
), handler
);