Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / DefaultUrlBuilderTestCase.cs
blob4067d5edf4d5973b4649b4bc08790769adecc44b
1 // Copyright 2004-2007 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
17 using System;
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;
25 [TestFixture]
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, "/");
42 [SetUp]
43 public void Init()
45 urlBuilder = new DefaultUrlBuilder();
46 urlBuilder.ServerUtil = new MockServerUtility();
47 urlBuilder.RoutingEngine = new RoutingEngine();
50 [Test]
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));
61 [Test]
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));
73 [Test]
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")));
81 [Test]
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")));
93 [Test]
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")));
101 [Test]
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")));
113 [Test]
114 public void AbsoluteUrls()
116 Assert.AreEqual("http://localhost:81/app/home/list.rails", urlBuilder.BuildUrl(diffPort, DictHelper.Create("absolute=true", "action=list")));
119 [Test]
120 public void AbsoluteUrlWithDifferentPort()
122 Assert.AreEqual("http://localhost:81/app/home/list.rails", urlBuilder.BuildUrl(diffPort, DictHelper.Create("absolute=true", "action=list")));
125 [Test]
126 public void WithDomains()
128 Assert.AreEqual("http://sub.domain.com/app/home/list.rails", urlBuilder.BuildUrl(withSubDomain, DictHelper.Create("absolute=true", "action=list")));
131 [Test]
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")));
138 [Test]
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")));
148 [Test]
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")));
158 [Test]
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