Update Castle.MicroKernel-vs2005.csproj with missing files.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Routing / PatternRouteTestCase.cs
bloba551bc3b564fc0c46b95ba935a8ab7e7949dccb4
1 // Copyright 2004-2008 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.Routing
17 using Framework.Routing;
18 using NUnit.Framework;
20 [TestFixture]
21 public class PatternRouteTestCase : BaseRuleTestFixture
23 [Test]
24 public void ShouldMatchStatic()
26 PatternRoute route = new PatternRoute("/some/path");
27 RouteMatch match = new RouteMatch();
28 Assert.AreEqual(8, route.Matches("/some/path", CreateGetContext(), match));
31 [Test]
32 public void ShouldMatchStaticWithFileExtension()
34 PatternRoute route = new PatternRoute("/default.aspx");
35 RouteMatch match = new RouteMatch();
36 Assert.AreEqual(8, route.Matches("/default.aspx", CreateGetContext(), match));
39 [Test]
40 public void ShouldMatchStaticCaseInsensitive()
42 PatternRoute route = new PatternRoute("/default.aspx");
43 RouteMatch match = new RouteMatch();
44 Assert.AreEqual(8, route.Matches("/DEFAULT.ASPX", CreateGetContext(), match));
46 route = new PatternRoute("/some/path");
47 match = new RouteMatch();
48 Assert.AreEqual(8, route.Matches("/SOME/Path", CreateGetContext(), match));
51 [Test]
52 public void ShouldMatchHiphensAndUnderlines()
54 PatternRoute route = new PatternRoute("/some/path_to-this");
55 RouteMatch match = new RouteMatch();
56 Assert.AreEqual(8, route.Matches("/some/path_to-this", CreateGetContext(), match));
59 [Test]
60 public void NamedRequiredParameters()
62 PatternRoute route = new PatternRoute("/<controller>/<action>");
63 RouteMatch match = new RouteMatch();
64 Assert.AreEqual(4, route.Matches("/some/act", CreateGetContext(), match));
65 Assert.AreEqual("some", match.Parameters["controller"]);
66 Assert.AreEqual("act", match.Parameters["action"]);
69 [Test]
70 public void NamedRequiredParametersForExtension()
72 PatternRoute route = new PatternRoute("/<controller>/<action>.<format>");
73 RouteMatch match = new RouteMatch();
74 Assert.AreEqual(6, route.Matches("/some/act.xml", CreateGetContext(), match));
75 Assert.AreEqual("some", match.Parameters["controller"]);
76 Assert.AreEqual("act", match.Parameters["action"]);
77 Assert.AreEqual("xml", match.Parameters["format"]);
80 [Test]
81 public void NamedParametersCanHaveUnderlines()
83 PatternRoute route = new PatternRoute("/<controller>/<action>");
84 RouteMatch match = new RouteMatch();
85 Assert.AreEqual(4, route.Matches("/some/act_name", CreateGetContext(), match));
86 Assert.AreEqual("some", match.Parameters["controller"]);
87 Assert.AreEqual("act_name", match.Parameters["action"]);
90 [Test]
91 public void NamedParametersCanHaveHiphens()
93 PatternRoute route = new PatternRoute("/<controller>/<action>");
94 RouteMatch match = new RouteMatch();
95 Assert.AreEqual(4, route.Matches("/some/act-name", CreateGetContext(), match));
96 Assert.AreEqual("some", match.Parameters["controller"]);
97 Assert.AreEqual("act-name", match.Parameters["action"]);
100 [Test]
101 public void NamedOptionalParameters()
103 PatternRoute route = new PatternRoute("/<controller>/[action]/[id]");
104 RouteMatch match = new RouteMatch();
105 Assert.AreEqual(4, route.Matches("/some/act", CreateGetContext(), match));
106 Assert.AreEqual("some", match.Parameters["controller"]);
107 Assert.AreEqual("act", match.Parameters["action"]);
109 match = new RouteMatch();
110 Assert.AreEqual(6, route.Matches("/some/act/10", CreateGetContext(), match));
111 Assert.AreEqual("some", match.Parameters["controller"]);
112 Assert.AreEqual("act", match.Parameters["action"]);
113 Assert.AreEqual("10", match.Parameters["id"]);
116 [Test]
117 public void NamedOptionalParametersWithDefaults()
119 PatternRoute route = new PatternRoute("/<controller>/[action]/[id]")
120 .DefaultFor("action").Is("index").DefaultFor("id").Is("0");
121 RouteMatch match = new RouteMatch();
122 Assert.AreEqual(2, route.Matches("/some", CreateGetContext(), match));
123 Assert.AreEqual("some", match.Parameters["controller"]);
124 Assert.AreEqual("index", match.Parameters["action"]);
125 Assert.AreEqual("0", match.Parameters["id"]);
128 [Test]
129 public void NamedOptionalParametersWithRestrictions()
131 PatternRoute route = new PatternRoute("/<controller>/[action]/[id]")
132 .Restrict("action").AnyOf("index", "list")
133 .Restrict("id").ValidInteger;
135 RouteMatch match = new RouteMatch();
136 Assert.AreEqual(4, route.Matches("/some/index", CreateGetContext(), match));
137 Assert.AreEqual(4, route.Matches("/some/list", CreateGetContext(), match));
138 Assert.AreEqual(0, route.Matches("/some/new", CreateGetContext(), match));
139 Assert.AreEqual(0, route.Matches("/some/index/foo", CreateGetContext(), match));
140 Assert.AreEqual(0, route.Matches("/some/list/bar", CreateGetContext(), match));
141 Assert.AreEqual(6, route.Matches("/some/list/1", CreateGetContext(), match));
144 [Test]
145 public void NamedRequiredParametersWithRestrictions()
147 string matchGuid =
148 "[A-Fa-f0-9]{32}|" +
149 "({|\\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\\))?|" +
150 "({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})";
152 PatternRoute route = new PatternRoute("/<param>/<key>")
153 .Restrict("key").ValidRegex(matchGuid);
155 RouteMatch match = new RouteMatch();
156 Assert.AreEqual(0, route.Matches("/something/zzzzzzzz-c123-11dc-95ff-0800200c9a66", CreateGetContext(), match));
157 Assert.AreEqual(4, route.Matches("/something/173e0970-c123-11dc-95ff-0800200c9a66", CreateGetContext(), match));
158 Assert.AreEqual("something", match.Parameters["param"]);
159 Assert.AreEqual("173e0970-c123-11dc-95ff-0800200c9a66", match.Parameters["key"]);