Minor style changes
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / UrlPartsBuilderTestCase.cs
blobddadb2f0df0ac553e1b5427afc6f9e80253f8452
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 NUnit.Framework;
19 [TestFixture]
20 public class UrlPartsBuilderTestCase
22 [Test]
23 public void CanBuildPathUrls()
25 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
27 Assert.AreEqual("controller/action", builder.BuildPath());
30 [Test]
31 public void BuildPathWithPathInfo()
33 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
34 builder.PathInfo.Add("State").Add("FL");
36 Assert.AreEqual("controller/action/State/FL", builder.BuildPath());
39 [Test]
40 public void BuildPathWithPathInfoDictionary()
42 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
43 builder.PathInfoDict["State"] ="FL";
45 Assert.AreEqual("controller/action/State/FL", builder.BuildPath());
48 [Test]
49 public void BuildPathWithPathInfoAndQueryString()
51 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
52 builder.PathInfoDict["State"] = "FL";
53 builder.SetQueryString("type=Residential");
55 Assert.AreEqual("controller/action/State/FL?type=Residential", builder.BuildPath());
58 [Test]
59 public void PathInfoDictParse_AcceptsNull()
61 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
62 builder.PathInfoDict.Parse(null);
64 Assert.AreEqual("controller/action", builder.BuildPath());
67 [Test]
68 public void PathInfoDictParse_AcceptsEmptyString()
70 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
71 builder.PathInfoDict.Parse("");
73 Assert.AreEqual("controller/action", builder.BuildPath());
76 [Test]
77 public void PathInfoDictParse_CanHandleMissingSlash()
79 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
80 builder.PathInfoDict.Parse("State/Fl");
82 Assert.AreEqual("controller/action/State/Fl", builder.BuildPath());
85 [Test]
86 public void PathInfoDictParse_CanHandleMultipleEntries()
88 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
89 builder.PathInfoDict.Parse("/State/FL/Type/Home");
91 Assert.AreEqual("controller/action/State/FL/Type/Home", builder.BuildPath());
94 [Test]
95 public void PathInfoDictParse_CanHandleOddNumberOfEntries()
97 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
98 builder.PathInfoDict.Parse("/State/FL/Type/");
100 Assert.AreEqual("controller/action/State/FL/Type", builder.BuildPath());
103 [Test]
104 public void PathInfoDictParse_CanHandleOddNumberOfEntries2()
106 UrlPartsBuilder builder = new UrlPartsBuilder("controller", "action");
107 builder.PathInfoDict.Parse("/State/FL/Type");
109 Assert.AreEqual("controller/action/State/FL/Type", builder.BuildPath());
112 [Test]
113 public void ParseCanHandleAbsolutePaths()
115 UrlPartsBuilder builder = UrlPartsBuilder.Parse("http://localhost/home/index.ext");
117 Assert.AreEqual(0, builder.PathInfoDict.Count);
119 Assert.AreEqual("http://localhost/home/index.ext", builder.BuildPath());
122 [Test]
123 public void ParseCanHandleAbsolutePathsWithQueryString()
125 UrlPartsBuilder builder = UrlPartsBuilder.Parse("http://localhost/home/index.ext?id=1&type=home");
127 Assert.AreEqual(0, builder.PathInfoDict.Count);
128 Assert.AreEqual("id=1&type=home", builder.QueryStringAsString());
130 Assert.AreEqual("http://localhost/home/index.ext?id=1&type=home", builder.BuildPath());
133 [Test]
134 public void ParseCanHandleAbsolutePathsWithPathInfo()
136 UrlPartsBuilder builder = UrlPartsBuilder.Parse("http://localhost/home/index.ext/state/fl/");
138 Assert.AreEqual(1, builder.PathInfoDict.Count);
139 Assert.AreEqual("fl", builder.PathInfoDict["state"]);
140 Assert.AreEqual("", builder.QueryStringAsString());
142 Assert.AreEqual("http://localhost/home/index.ext/state/fl", builder.BuildPath());
145 [Test]
146 public void ParseCanHandleRelativePaths()
148 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext");
150 Assert.AreEqual("home/index.ext", builder.BuildPath());
153 [Test]
154 public void ParseCanHandleRelativePathsWithEmptyPathInfo()
156 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext/");
158 Assert.AreEqual("home/index.ext", builder.BuildPath());
161 [Test]
162 public void ParseCanHandleRelativePathsWithPathInfo()
164 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext/state/fl");
166 Assert.AreEqual(1, builder.PathInfoDict.Count);
167 Assert.AreEqual("fl", builder.PathInfoDict["state"]);
168 Assert.IsNull(builder.QueryStringAsString());
170 Assert.AreEqual("home/index.ext/state/fl", builder.BuildPath());
173 [Test]
174 public void ParseCanHandleRelativePathsWithPathInfo2()
176 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext/state/fl/");
178 Assert.AreEqual(1, builder.PathInfoDict.Count);
179 Assert.AreEqual("fl", builder.PathInfoDict["state"]);
180 Assert.IsNull(builder.QueryStringAsString());
182 Assert.AreEqual("home/index.ext/state/fl", builder.BuildPath());
185 [Test]
186 public void ParseCanHandleRelativePathsWithQueryString()
188 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext?id=1&name=john");
190 Assert.AreEqual(0, builder.PathInfoDict.Count);
191 Assert.AreEqual("id=1&name=john", builder.QueryStringAsString());
193 Assert.AreEqual("home/index.ext?id=1&name=john", builder.BuildPath());
196 [Test]
197 public void ParseCanHandleRelativePathsWithEmptyPathInfoAndQueryString()
199 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext/?id=1&name=john");
201 Assert.AreEqual(0, builder.PathInfoDict.Count);
202 Assert.AreEqual("id=1&name=john", builder.QueryStringAsString());
204 Assert.AreEqual("home/index.ext?id=1&name=john", builder.BuildPath());
207 [Test]
208 public void ParseCanHandleRelativePathsWithPathInfoAndQueryString()
210 UrlPartsBuilder builder = UrlPartsBuilder.Parse("home/index.ext/state/fl/?id=1&name=john");
212 Assert.AreEqual(1, builder.PathInfoDict.Count);
213 Assert.AreEqual("fl", builder.PathInfoDict["state"]);
214 Assert.AreEqual("id=1&name=john", builder.QueryStringAsString());
216 Assert.AreEqual("home/index.ext/state/fl?id=1&name=john", builder.BuildPath());
219 [Test]
220 public void QueryStringParsesStringCorrectly()
222 UrlPartsBuilder builder = new UrlPartsBuilder("home/index.ext");
224 builder.QueryString["state"] = "FL";
226 Assert.AreEqual("home/index.ext?state=FL", builder.BuildPath());
229 [Test]
230 public void QueryStringIsExtractedAndParsed()
232 UrlPartsBuilder builder = new UrlPartsBuilder("home/index.ext");
234 builder.SetQueryString("City=SP&State=MD");
236 builder.QueryString["type"] = "home";
238 Assert.AreEqual("home/index.ext?City=SP&State=MD&type=home", builder.BuildPath());
241 [Test]
242 public void QueryStringCanHandleDuplicatedEntries()
244 UrlPartsBuilder builder = new UrlPartsBuilder("home/index.ext");
246 builder.SetQueryString("City=SP&State=MD&State=NY");
248 Assert.AreEqual("home/index.ext?City=SP&State=MD&State=NY", builder.BuildPath());
251 [Test]
252 public void QueryStringCanReplaceEntries()
254 UrlPartsBuilder builder = new UrlPartsBuilder("home/index.ext");
256 builder.QueryString["page"] = "1";
258 Assert.AreEqual("home/index.ext?page=1", builder.BuildPath());
260 builder.QueryString.Set("page", "2");
262 Assert.AreEqual("home/index.ext?page=2", builder.BuildPath());
264 builder.QueryString.Set("page", "3");
266 Assert.AreEqual("home/index.ext?page=3", builder.BuildPath());