1 // Copyright 2004-2007 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
18 using System
.Collections
.Specialized
;
19 using Castle
.MonoRail
.Framework
.Helpers
;
20 using Castle
.MonoRail
.Framework
.Routing
;
21 using Castle
.MonoRail
.Framework
.Services
;
22 using Castle
.MonoRail
.Framework
.Test
;
23 using NUnit
.Framework
;
26 public class DefaultUrlBuilderTestCase
28 private readonly UrlInfo noAreaUrl
, areaUrl
, withSubDomain
, diffPort
, withPathInfo
;
29 private DefaultUrlBuilder urlBuilder
;
31 public DefaultUrlBuilderTestCase()
33 DefaultUrlTokenizer tokenizer
= new DefaultUrlTokenizer();
35 noAreaUrl
= tokenizer
.TokenizeUrl("/home/index.rails", null, new Uri("http://localhost/home/index.rails"), true, "/");
36 areaUrl
= tokenizer
.TokenizeUrl("/area/home/index.rails", null, new Uri("http://localhost/area/home/index.rails"), true, "/");
37 withSubDomain
= tokenizer
.TokenizeUrl("/app/home/index.rails", null, new Uri("http://sub.domain.com/app/home/index.rails"), false, "/app");
38 diffPort
= tokenizer
.TokenizeUrl("/app/home/index.rails", null, new Uri("http://localhost:81/app/home/index.rails"), false, "/app");
39 withPathInfo
= tokenizer
.TokenizeUrl("/home/index.rails", "/state/fl", new Uri("http://localhost:81/home/index.rails"), false, "/");
45 urlBuilder
= new DefaultUrlBuilder();
46 urlBuilder
.ServerUtil
= new MockServerUtility();
47 urlBuilder
.RoutingEngine
= new RoutingEngine();
51 public void ShouldUseRoutingEngineForNamedRoutes()
53 urlBuilder
.RoutingEngine
.Add(PatternRule
.Build("link", "products", typeof(HomeController
), "View"));
55 HybridDictionary dict
= new HybridDictionary(true);
56 dict
["named"] = "link";
58 Assert
.AreEqual("/products", urlBuilder
.BuildUrl(noAreaUrl
, dict
));
62 public void ShouldUseTheParamsEntryForRoutesWithParams()
64 urlBuilder
.RoutingEngine
.Add(PatternRule
.Build("link", "products/<id:number>", typeof(HomeController
), "View"));
66 HybridDictionary dict
= new HybridDictionary(true);
67 dict
["named"] = "link";
68 dict
["params"] = DictHelper
.Create("id=1");
70 Assert
.AreEqual("/products/1", urlBuilder
.BuildUrl(noAreaUrl
, dict
));
74 public void SimpleOperations()
76 Assert
.AreEqual("/product/list.rails", urlBuilder
.BuildUrl(noAreaUrl
, "product", "list"));
77 Assert
.AreEqual("/home/list.rails", urlBuilder
.BuildUrl(noAreaUrl
, DictHelper
.Create("action=list")));
78 Assert
.AreEqual("/product/list.rails?id=1&name=hammett", urlBuilder
.BuildUrl(noAreaUrl
, "product", "list", DictHelper
.Create("id=1", "name=hammett")));
82 public void OperationsWithArea()
84 Assert
.AreEqual("/product/list.rails/state/FL?key=value",
85 urlBuilder
.BuildUrl(withPathInfo
,
86 DictHelper
.Create("controller=product", "action=list", "pathinfo=/state/FL", "querystring=key=value")));
88 Assert
.AreEqual("/product/list.rails/state/FL?key=value",
89 urlBuilder
.BuildUrl(withPathInfo
,
90 DictHelper
.Create("controller=product", "action=list", "pathinfo=state/FL", "querystring=key=value")));
94 public void OperationsWithPathInfo()
96 Assert
.AreEqual("/product/list.rails", urlBuilder
.BuildUrl(noAreaUrl
, "product", "list"));
97 Assert
.AreEqual("/home/list.rails", urlBuilder
.BuildUrl(noAreaUrl
, DictHelper
.Create("action=list")));
98 Assert
.AreEqual("/product/list.rails?id=1&name=hammett", urlBuilder
.BuildUrl(noAreaUrl
, "product", "list", DictHelper
.Create("id=1", "name=hammett")));
102 public void NoExtensions()
104 urlBuilder
.UseExtensions
= false;
106 Assert
.AreEqual("/product/list", urlBuilder
.BuildUrl(noAreaUrl
, "product", "list"));
107 Assert
.AreEqual("/home/list", urlBuilder
.BuildUrl(noAreaUrl
, DictHelper
.Create("action=list")));
108 Assert
.AreEqual("/product/list?id=1&name=hammett", urlBuilder
.BuildUrl(noAreaUrl
, "product", "list", DictHelper
.Create("id=1", "name=hammett")));
109 Assert
.AreEqual("/area/home/list", urlBuilder
.BuildUrl(areaUrl
, DictHelper
.Create("action=list")));
110 Assert
.AreEqual("/app/home/list", urlBuilder
.BuildUrl(withSubDomain
, DictHelper
.Create("action=list")));
114 public void AbsoluteUrls()
116 Assert
.AreEqual("http://localhost:81/app/home/list.rails", urlBuilder
.BuildUrl(diffPort
, DictHelper
.Create("absolute=true", "action=list")));
120 public void AbsoluteUrlWithDifferentPort()
122 Assert
.AreEqual("http://localhost:81/app/home/list.rails", urlBuilder
.BuildUrl(diffPort
, DictHelper
.Create("absolute=true", "action=list")));
126 public void WithDomains()
128 Assert
.AreEqual("http://sub.domain.com/app/home/list.rails", urlBuilder
.BuildUrl(withSubDomain
, DictHelper
.Create("absolute=true", "action=list")));
132 public void SwitchingDomains()
134 Assert
.AreEqual("http://testsub.domain.com/app/home/list.rails", urlBuilder
.BuildUrl(withSubDomain
, DictHelper
.Create("absolute=true", "action=list", "subdomain=test")));
135 Assert
.AreEqual("http://something.else/app/home/list.rails", urlBuilder
.BuildUrl(withSubDomain
, DictHelper
.Create("absolute=true", "action=list", "domain=something.else")));
139 public void UseBasePathMustDiscardTheAppVirtualDirInfo()
141 Assert
.AreEqual("http://localhost/theArea/home/index.rails", urlBuilder
.BuildUrl(areaUrl
,
142 DictHelper
.Create("basepath=http://localhost/", "area=theArea", "controller=home", "action=index")));
144 Assert
.AreEqual("http://localhost/theArea/home/index.rails", urlBuilder
.BuildUrl(areaUrl
,
145 DictHelper
.Create("basepath=http://localhost", "area=theArea", "controller=home", "action=index")));
149 public void UseBasePathMustDiscardTheAreaIfTheValueIsDuplicated()
151 Assert
.AreEqual("http://localhost/theArea/home/index.rails", urlBuilder
.BuildUrl(areaUrl
,
152 DictHelper
.Create("basepath=http://localhost/theArea", "area=theArea", "controller=home", "action=index")));
154 Assert
.AreEqual("http://localhost/theArea/home/index.rails", urlBuilder
.BuildUrl(areaUrl
,
155 DictHelper
.Create("basepath=http://localhost/theArea/", "area=theArea", "controller=home", "action=index")));
159 public void UseBasePathWithQuerystring()
161 Assert
.AreEqual("http://localhost/theArea/home/index.rails?key=value", urlBuilder
.BuildUrl(areaUrl
,
162 DictHelper
.Create("basepath=http://localhost/theArea", "area=theArea", "controller=home", "action=index", "querystring=key=value")));
165 public class HomeController
: Controller