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
.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
;
25 public class DefaultUrlBuilderTestCase
27 private DefaultUrlBuilder urlBuilder
;
32 urlBuilder
= new DefaultUrlBuilder();
33 urlBuilder
.ServerUtil
= new MockServerUtility();
34 urlBuilder
.RoutingEngine
= new RoutingEngine();
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")));
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")));
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")));
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")));
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")));
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")));
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")));
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")));
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
));
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
));
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")));
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")));
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")));
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")));
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")));
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")));
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")));
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",
234 Assert
.AreEqual("http://localhost/theArea/home/index.castle",
235 urlBuilder
.BuildUrl(url
,
237 "basepath=http://localhost",
238 "area=theArea", "controller=home",
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
,
250 "basepath=http://localhost/theArea",
251 "area=theArea", "controller=home",
254 Assert
.AreEqual("http://localhost/theArea/home/index.castle",
255 urlBuilder
.BuildUrl(url
,
257 "basepath=http://localhost/theArea/",
258 "area=theArea", "controller=home",
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
,
270 "basepath=http://localhost/theArea",
274 "querystring=key=value")));