Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Services / DefaultUrlBuilderTestCase.cs
blob084051c258e1cc66879be554ac5d760e5aeeeb3a
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.Services
17 using System.Collections.Specialized;
18 using Castle.MonoRail.Framework.Helpers;
19 using Castle.MonoRail.Framework.Routing;
20 using Castle.MonoRail.Framework.Services;
21 using NUnit.Framework;
22 using Test;
24 [TestFixture]
25 public class DefaultUrlBuilderTestCase
27 private DefaultUrlBuilder urlBuilder;
29 [SetUp]
30 public void Init()
32 urlBuilder = new DefaultUrlBuilder();
33 urlBuilder.ServerUtil = new MockServerUtility();
34 urlBuilder.RoutingEngine = new RoutingEngine();
37 [Test]
38 public void InheritsControllerAndAreaWhenCreatingUrl()
40 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
42 Assert.AreEqual("/controller/new.castle",
43 urlBuilder.BuildUrl(url, DictHelper.Create("action=new")));
46 [Test]
47 public void OverridingController()
49 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
51 Assert.AreEqual("/cars/new.castle",
52 urlBuilder.BuildUrl(url, DictHelper.Create("controller=cars", "action=new")));
55 [Test]
56 public void OverridingArea()
58 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
60 Assert.AreEqual("/admin/cars/new.castle",
61 urlBuilder.BuildUrl(url, DictHelper.Create("area=admin", "controller=cars", "action=new")));
64 [Test]
65 public void UsesAppPath()
67 UrlInfo url = new UrlInfo("", "controller", "action", "/app", ".castle");
69 Assert.AreEqual("/app/controller/new.castle",
70 urlBuilder.BuildUrl(url, DictHelper.Create("action=new")));
73 [Test]
74 public void UsesMoreThanASingleLevelAppPath()
76 UrlInfo url = new UrlInfo("", "controller", "action", "/app/some", ".castle");
78 Assert.AreEqual("/app/some/controller/new.castle",
79 urlBuilder.BuildUrl(url, DictHelper.Create("action=new")));
82 [Test]
83 public void CanHandleEmptyAppPath()
85 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
87 Assert.AreEqual("/controller/edit.castle",
88 urlBuilder.BuildUrl(url, DictHelper.Create("action=edit")));
91 [Test]
92 public void TurningOffUseExtensions()
94 urlBuilder.UseExtensions = false;
96 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
98 Assert.AreEqual("/controller/edit",
99 urlBuilder.BuildUrl(url, DictHelper.Create("action=edit")));
102 [Test]
103 public void SupportsQueryInfoAsString()
105 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
107 Assert.AreEqual("/controller/new.castle?something=1",
108 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "querystring=something=1")));
111 [Test]
112 public void SupportsPathInfoAsDictionary()
114 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
116 HybridDictionary parameters = new HybridDictionary(true);
117 parameters["action"] = "new";
118 parameters["querystring"] = DictHelper.Create("id=1", "name=john doe");
120 Assert.AreEqual("/controller/new.castle?id=1&name=john+doe",
121 urlBuilder.BuildUrl(url, parameters));
124 [Test]
125 public void SupportsPathInfoAsNameValueCollection()
127 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
129 NameValueCollection namedParams = new NameValueCollection();
130 namedParams["id"] = "1";
131 namedParams["name"] = "john doe";
133 HybridDictionary parameters = new HybridDictionary(true);
134 parameters["action"] = "new";
135 parameters["querystring"] = namedParams;
137 Assert.AreEqual("/controller/new.castle?id=1&name=john+doe",
138 urlBuilder.BuildUrl(url, parameters));
141 [Test]
142 public void SupportsSettingPathInfo()
144 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
146 Assert.AreEqual("/controller/new.castle/id/1/name/doe",
147 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "pathinfo=id/1/name/doe")));
150 [Test]
151 public void SupportsAbsolutePaths()
153 UrlInfo url = new UrlInfo("localhost", "", "", "https", 443, "", "area", "controller", "action", ".castle", "");
154 Assert.AreEqual("https://localhost/area/controller/new.castle",
155 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
157 url = new UrlInfo("localhost", "", "/app", "https", 443, "", "area", "controller", "action", ".castle", "");
158 Assert.AreEqual("https://localhost/app/area/controller/new.castle",
159 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
162 [Test]
163 public void SupportsAbsolutePathsWithSubDomains()
165 UrlInfo url = new UrlInfo("vpn", "staging", "", "https", 443, "", "area", "controller", "action", ".castle", "");
166 Assert.AreEqual("https://staging.vpn/area/controller/new.castle",
167 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
170 [Test]
171 public void CanOverrideSubDomain()
173 UrlInfo url = new UrlInfo("vpn", "staging", "", "https", 443, "", "area", "controller", "action", ".castle", "");
174 Assert.AreEqual("https://intranet.vpn/area/controller/new.castle",
175 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "subdomain=intranet", "absolute=true")));
178 [Test]
179 public void CanOverrideDomain()
181 UrlInfo url = new UrlInfo("vpn", "staging", "", "https", 443, "", "area", "controller", "action", ".castle", "");
182 Assert.AreEqual("https://staging.intranet/area/controller/new.castle",
183 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "domain=intranet", "absolute=true")));
186 [Test]
187 public void EncodesToCreateValidHtmlContent()
189 UrlInfo url = new UrlInfo("", "controller", "action", "", ".castle");
191 HybridDictionary parameters = new HybridDictionary(true);
192 parameters["action"] = "new";
193 parameters["encode"] = "true";
194 parameters["querystring"] = DictHelper.Create("id=1", "name=john doe");
196 Assert.AreEqual("/controller/new.castle?id=1&name=john+doe",
197 urlBuilder.BuildUrl(url, parameters));
199 Assert.AreEqual("/controller/new.castle?id=1&name=john+doe",
200 urlBuilder.BuildUrl(url,
201 DictHelper.Create("encode=true", "action=new", "querystring=id=1&name=john doe")));
204 [Test]
205 public void PortsAreSkippedForDefaults()
207 UrlInfo url = new UrlInfo("localhost", "", "", "https", 443, "", "", "controller", "action", ".castle", "");
208 Assert.AreEqual("https://localhost/controller/new.castle",
209 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
211 url = new UrlInfo("localhost", "", "", "http", 80, "", "", "controller", "action", ".castle", "");
212 Assert.AreEqual("http://localhost/controller/new.castle",
213 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
215 url = new UrlInfo("localhost", "", "", "http", 8080, "", "", "controller", "action", ".castle", "");
216 Assert.AreEqual("http://localhost:8080/controller/new.castle",
217 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
219 url = new UrlInfo("localhost", "", "", "https", 441, "", "", "controller", "action", ".castle", "");
220 Assert.AreEqual("https://localhost:441/controller/new.castle",
221 urlBuilder.BuildUrl(url, DictHelper.Create("action=new", "absolute=true")));
224 [Test]
225 public void UseBasePathMustDiscardTheAppVirtualDirInfo()
227 UrlInfo url = new UrlInfo("area", "controller", "action", "/app", ".castle");
229 Assert.AreEqual("http://localhost/theArea/home/index.castle",
230 urlBuilder.BuildUrl(url, DictHelper.Create("basepath=http://localhost/",
231 "area=theArea", "controller=home",
232 "action=index")));
234 Assert.AreEqual("http://localhost/theArea/home/index.castle",
235 urlBuilder.BuildUrl(url,
236 DictHelper.Create(
237 "basepath=http://localhost",
238 "area=theArea", "controller=home",
239 "action=index")));
242 [Test]
243 public void UseBasePathMustDiscardTheAreaIfTheValueIsDuplicated()
245 UrlInfo url = new UrlInfo("theArea", "controller", "action", "/app", ".castle");
247 Assert.AreEqual("http://localhost/theArea/home/index.castle",
248 urlBuilder.BuildUrl(url,
249 DictHelper.Create(
250 "basepath=http://localhost/theArea",
251 "area=theArea", "controller=home",
252 "action=index")));
254 Assert.AreEqual("http://localhost/theArea/home/index.castle",
255 urlBuilder.BuildUrl(url,
256 DictHelper.Create(
257 "basepath=http://localhost/theArea/",
258 "area=theArea", "controller=home",
259 "action=index")));
262 [Test]
263 public void UseBasePathWithQuerystring()
265 UrlInfo url = new UrlInfo("area", "controller", "action", "/app", ".castle");
267 Assert.AreEqual("http://localhost/theArea/home/index.castle?key=value",
268 urlBuilder.BuildUrl(url,
269 DictHelper.Create(
270 "basepath=http://localhost/theArea",
271 "area=theArea",
272 "controller=home",
273 "action=index",
274 "querystring=key=value")));