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
.Routing
;
22 using Castle
.MonoRail
.Framework
.Services
;
25 using NUnit
.Framework
;
29 public class MRHandlerFactoryTestCase
31 private MockRepository mockRepository
= new MockRepository();
32 private MonoRailHttpHandlerFactory handlerFactory
;
33 private IServiceProviderLocator serviceProviderLocatorMock
;
34 private IMonoRailContainer container
;
35 private IControllerFactory controllerFactoryMock
;
36 private IController controllerMock
;
37 private IControllerDescriptorProvider controllerDescriptorProviderMock
;
38 private IControllerContextFactory controllerContextFactoryMock
;
43 container
= mockRepository
.CreateMock
<IMonoRailContainer
>();
44 serviceProviderLocatorMock
= mockRepository
.CreateMock
<IServiceProviderLocator
>();
45 controllerFactoryMock
= mockRepository
.CreateMock
<IControllerFactory
>();
46 controllerMock
= mockRepository
.CreateMock
<IController
>();
47 controllerDescriptorProviderMock
= mockRepository
.CreateMock
<IControllerDescriptorProvider
>();
48 controllerContextFactoryMock
= mockRepository
.CreateMock
<IControllerContextFactory
>();
50 SetupResult
.For(container
.UrlTokenizer
).Return(new DefaultUrlTokenizer());
51 SetupResult
.For(container
.UrlBuilder
).Return(new DefaultUrlBuilder());
52 SetupResult
.For(container
.EngineContextFactory
).Return(new DefaultEngineContextFactory());
53 SetupResult
.For(container
.ControllerFactory
).Return(controllerFactoryMock
);
54 SetupResult
.For(container
.ControllerContextFactory
).Return(controllerContextFactoryMock
);
55 SetupResult
.For(container
.ControllerDescriptorProvider
).Return(controllerDescriptorProviderMock
);
56 SetupResult
.For(container
.StaticResourceRegistry
).Return(new DefaultStaticResourceRegistry());
58 handlerFactory
= new MonoRailHttpHandlerFactory(serviceProviderLocatorMock
);
59 handlerFactory
.ResetState();
60 handlerFactory
.Configuration
= new MonoRailConfiguration();
61 handlerFactory
.Container
= container
;
65 public void Request_CreatesSessionfulHandler()
67 StringWriter writer
= new StringWriter();
69 HttpResponse res
= new HttpResponse(writer
);
70 HttpRequest req
= new HttpRequest(Path
.Combine(
71 AppDomain
.CurrentDomain
.BaseDirectory
, "Handlers/Files/simplerequest.txt"),
72 "http://localhost:1333/home/something", "");
73 RouteMatch routeMatch
= new RouteMatch();
74 HttpContext httpCtx
= new HttpContext(req
, res
);
75 httpCtx
.Items
[RouteMatch
.RouteMatchKey
] = routeMatch
;
77 using(mockRepository
.Record())
79 ControllerMetaDescriptor controllerDesc
= new ControllerMetaDescriptor();
80 controllerDesc
.ControllerDescriptor
= new ControllerDescriptor(typeof(Controller
), "home", "", false);
82 Expect
.Call(controllerFactoryMock
.CreateController("", "home")).IgnoreArguments().Return(controllerMock
);
83 Expect
.Call(controllerDescriptorProviderMock
.BuildDescriptor(controllerMock
)).Return(controllerDesc
);
84 Expect
.Call(controllerContextFactoryMock
.Create("", "home", "something", controllerDesc
, routeMatch
)).
85 Return(new ControllerContext());
88 using(mockRepository
.Playback())
90 IHttpHandler handler
= handlerFactory
.GetHandler(httpCtx
, "GET", "", "");
92 Assert
.IsNotNull(handler
);
93 Assert
.IsInstanceOfType(typeof(MonoRailHttpHandler
), handler
);
98 public void Request_CreatesSessionlessHandler()
100 StringWriter writer
= new StringWriter();
102 HttpResponse res
= new HttpResponse(writer
);
103 HttpRequest req
= new HttpRequest(Path
.Combine(
104 AppDomain
.CurrentDomain
.BaseDirectory
, "Handlers/Files/simplerequest.txt"),
105 "http://localhost:1333/home/something", "");
106 RouteMatch routeMatch
= new RouteMatch();
107 HttpContext httpCtx
= new HttpContext(req
, res
);
108 httpCtx
.Items
[RouteMatch
.RouteMatchKey
] = routeMatch
;
110 using(mockRepository
.Record())
112 ControllerMetaDescriptor controllerDesc
= new ControllerMetaDescriptor();
113 controllerDesc
.ControllerDescriptor
= new ControllerDescriptor(typeof(Controller
), "home", "", true);
115 Expect
.Call(controllerFactoryMock
.CreateController("", "home")).IgnoreArguments().Return(controllerMock
);
116 Expect
.Call(controllerDescriptorProviderMock
.BuildDescriptor(controllerMock
)).Return(controllerDesc
);
117 Expect
.Call(controllerContextFactoryMock
.Create("", "home", "something", controllerDesc
, routeMatch
)).
118 Return(new ControllerContext());
121 using(mockRepository
.Playback())
123 IHttpHandler handler
= handlerFactory
.GetHandler(httpCtx
, "GET", "", "");
125 Assert
.IsNotNull(handler
);
126 Assert
.IsInstanceOfType(typeof(SessionlessMonoRailHttpHandler
), handler
);