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
17 using Castle
.MonoRail
.Framework
.Services
;
18 using NUnit
.Framework
;
21 public class UrlPartsBuilderTestCase
24 public void CanBuildPathUrls()
26 UrlParts builder
= new UrlParts("controller", "action");
28 Assert
.AreEqual("controller/action", builder
.BuildPath());
32 public void BuildPathWithPathInfo()
34 UrlParts builder
= new UrlParts("controller", "action");
35 builder
.PathInfo
.Add("State").Add("FL");
37 Assert
.AreEqual("controller/action/State/FL", builder
.BuildPath());
41 public void BuildPathWithPathInfoDictionary()
43 UrlParts builder
= new UrlParts("controller", "action");
44 builder
.PathInfoDict
["State"] ="FL";
46 Assert
.AreEqual("controller/action/State/FL", builder
.BuildPath());
50 public void BuildPathWithPathInfoAndQueryString()
52 UrlParts builder
= new UrlParts("controller", "action");
53 builder
.PathInfoDict
["State"] = "FL";
54 builder
.SetQueryString("type=Residential");
56 Assert
.AreEqual("controller/action/State/FL?type=Residential", builder
.BuildPath());
60 public void PathInfoDictParse_AcceptsNull()
62 UrlParts builder
= new UrlParts("controller", "action");
63 builder
.PathInfoDict
.Parse(null);
65 Assert
.AreEqual("controller/action", builder
.BuildPath());
69 public void PathInfoDictParse_AcceptsEmptyString()
71 UrlParts builder
= new UrlParts("controller", "action");
72 builder
.PathInfoDict
.Parse("");
74 Assert
.AreEqual("controller/action", builder
.BuildPath());
78 public void PathInfoDictParse_CanHandleMissingSlash()
80 UrlParts builder
= new UrlParts("controller", "action");
81 builder
.PathInfoDict
.Parse("State/Fl");
83 Assert
.AreEqual("controller/action/State/Fl", builder
.BuildPath());
87 public void PathInfoDictParse_CanHandleMultipleEntries()
89 UrlParts builder
= new UrlParts("controller", "action");
90 builder
.PathInfoDict
.Parse("/State/FL/Type/Home");
92 Assert
.AreEqual("controller/action/State/FL/Type/Home", builder
.BuildPath());
96 public void PathInfoDictParse_CanHandleOddNumberOfEntries()
98 UrlParts builder
= new UrlParts("controller", "action");
99 builder
.PathInfoDict
.Parse("/State/FL/Type/");
101 Assert
.AreEqual("controller/action/State/FL/Type", builder
.BuildPath());
105 public void PathInfoDictParse_CanHandleOddNumberOfEntries2()
107 UrlParts builder
= new UrlParts("controller", "action");
108 builder
.PathInfoDict
.Parse("/State/FL/Type");
110 Assert
.AreEqual("controller/action/State/FL/Type", builder
.BuildPath());
114 public void ParseCanHandleAbsolutePaths()
116 UrlParts builder
= UrlParts
.Parse("http://localhost/home/index.ext");
118 Assert
.AreEqual(0, builder
.PathInfoDict
.Count
);
120 Assert
.AreEqual("http://localhost/home/index.ext", builder
.BuildPath());
124 public void ParseCanHandleAbsolutePathsWithQueryString()
126 UrlParts builder
= UrlParts
.Parse("http://localhost/home/index.ext?id=1&type=home");
128 Assert
.AreEqual(0, builder
.PathInfoDict
.Count
);
129 Assert
.AreEqual("id=1&type=home", builder
.QueryStringAsString());
131 Assert
.AreEqual("http://localhost/home/index.ext?id=1&type=home", builder
.BuildPath());
135 public void ParseCanHandleAbsolutePathsWithPathInfo()
137 UrlParts builder
= UrlParts
.Parse("http://localhost/home/index.ext/state/fl/");
139 Assert
.AreEqual(1, builder
.PathInfoDict
.Count
);
140 Assert
.AreEqual("fl", builder
.PathInfoDict
["state"]);
141 Assert
.AreEqual("", builder
.QueryStringAsString());
143 Assert
.AreEqual("http://localhost/home/index.ext/state/fl", builder
.BuildPath());
147 public void ParseCanHandleRelativePaths()
149 UrlParts builder
= UrlParts
.Parse("home/index.ext");
151 Assert
.AreEqual("home/index.ext", builder
.BuildPath());
155 public void ParseCanHandleRelativePathsWithEmptyPathInfo()
157 UrlParts builder
= UrlParts
.Parse("home/index.ext/");
159 Assert
.AreEqual("home/index.ext", builder
.BuildPath());
163 public void ParseCanHandleRelativePathsWithPathInfo()
165 UrlParts builder
= UrlParts
.Parse("home/index.ext/state/fl");
167 Assert
.AreEqual(1, builder
.PathInfoDict
.Count
);
168 Assert
.AreEqual("fl", builder
.PathInfoDict
["state"]);
169 Assert
.IsNull(builder
.QueryStringAsString());
171 Assert
.AreEqual("home/index.ext/state/fl", builder
.BuildPath());
175 public void ParseCanHandleRelativePathsWithPathInfo2()
177 UrlParts builder
= UrlParts
.Parse("home/index.ext/state/fl/");
179 Assert
.AreEqual(1, builder
.PathInfoDict
.Count
);
180 Assert
.AreEqual("fl", builder
.PathInfoDict
["state"]);
181 Assert
.IsNull(builder
.QueryStringAsString());
183 Assert
.AreEqual("home/index.ext/state/fl", builder
.BuildPath());
187 public void ParseCanHandleRelativePathsWithQueryString()
189 UrlParts builder
= UrlParts
.Parse("home/index.ext?id=1&name=john");
191 Assert
.AreEqual(0, builder
.PathInfoDict
.Count
);
192 Assert
.AreEqual("id=1&name=john", builder
.QueryStringAsString());
194 Assert
.AreEqual("home/index.ext?id=1&name=john", builder
.BuildPath());
198 public void ParseCanHandleRelativePathsWithEmptyPathInfoAndQueryString()
200 UrlParts builder
= UrlParts
.Parse("home/index.ext/?id=1&name=john");
202 Assert
.AreEqual(0, builder
.PathInfoDict
.Count
);
203 Assert
.AreEqual("id=1&name=john", builder
.QueryStringAsString());
205 Assert
.AreEqual("home/index.ext?id=1&name=john", builder
.BuildPath());
209 public void ParseCanHandleRelativePathsWithPathInfoAndQueryString()
211 UrlParts builder
= UrlParts
.Parse("home/index.ext/state/fl/?id=1&name=john");
213 Assert
.AreEqual(1, builder
.PathInfoDict
.Count
);
214 Assert
.AreEqual("fl", builder
.PathInfoDict
["state"]);
215 Assert
.AreEqual("id=1&name=john", builder
.QueryStringAsString());
217 Assert
.AreEqual("home/index.ext/state/fl?id=1&name=john", builder
.BuildPath());
221 public void QueryStringParsesStringCorrectly()
223 UrlParts builder
= new UrlParts("home/index.ext");
225 builder
.QueryString
["state"] = "FL";
227 Assert
.AreEqual("home/index.ext?state=FL", builder
.BuildPath());
231 public void QueryStringIsExtractedAndParsed()
233 UrlParts builder
= new UrlParts("home/index.ext");
235 builder
.SetQueryString("City=SP&State=MD");
237 builder
.QueryString
["type"] = "home";
239 Assert
.AreEqual("home/index.ext?City=SP&State=MD&type=home", builder
.BuildPath());
243 public void QueryStringCanHandleDuplicatedEntries()
245 UrlParts builder
= new UrlParts("home/index.ext");
247 builder
.SetQueryString("City=SP&State=MD&State=NY");
249 Assert
.AreEqual("home/index.ext?City=SP&State=MD&State=NY", builder
.BuildPath());
253 public void QueryStringCanReplaceEntries()
255 UrlParts builder
= new UrlParts("home/index.ext");
257 builder
.QueryString
["page"] = "1";
259 Assert
.AreEqual("home/index.ext?page=1", builder
.BuildPath());
261 builder
.QueryString
.Set("page", "2");
263 Assert
.AreEqual("home/index.ext?page=2", builder
.BuildPath());
265 builder
.QueryString
.Set("page", "3");
267 Assert
.AreEqual("home/index.ext?page=3", builder
.BuildPath());