2 // UrlRoutingModuleTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Routing;
33 using NUnit.Framework;
35 namespace MonoTests.System.Web.Routing
38 public class UrlRoutingModuleTest
43 RouteTable.Routes.Clear ();
47 public void SetRouteCollectionNull ()
49 var m = new UrlRoutingModule ();
50 RouteTable.Routes.Add (new Route (null, null));
51 Assert.IsNotNull (m.RouteCollection, "#1");
52 Assert.AreEqual (RouteTable.Routes, m.RouteCollection, "#1-2");
53 m.RouteCollection = null;
54 Assert.IsNotNull (m.RouteCollection, "#2");
55 Assert.AreEqual (RouteTable.Routes, m.RouteCollection, "#2-2");
58 // PostMapRequestHandler
61 public void PostMapRequestHandlerNoMatch ()
63 var m = new UrlRoutingModule ();
64 RouteTable.Routes.Add (new MyRoute (null, null));
65 m.PostMapRequestHandler (new HttpContextStub2 ());
69 public void PostMapRequestHandlerNoPath ()
71 var m = new UrlRoutingModule ();
72 RouteTable.Routes.Add (new MyRoute ("foo/bar", new MyRouteHandler ()));
73 // ... huh? no NIE? what does it do then?
74 m.PostMapRequestHandler (new HttpContextStub2 ("~/foo/bar", null));
77 // PostResolveRequestCache
80 [ExpectedException (typeof (InvalidOperationException))]
81 public void PostResolveRequestCacheNullRouteHandler ()
83 var m = new UrlRoutingModule ();
84 RouteTable.Routes.Add (new MyRoute ("foo/bar", null));
85 m.PostResolveRequestCache (new HttpContextStub2 ("~/foo/bar", null));
90 [ExpectedException (typeof (InvalidOperationException))]
91 public void PostResolveRequestCacheNullHttpHandler ()
93 var m = new UrlRoutingModule ();
94 RouteTable.Routes.Add (new MyRoute ("foo/bar", new NullRouteHandler ()));
96 m.PostResolveRequestCache (new HttpContextStub2 ("~/foo/bar", null));
100 [ExpectedException (typeof (NotImplementedException))]
101 public void PostResolveRequestCacheNoPath ()
103 var m = new UrlRoutingModule ();
104 RouteTable.Routes.Add (new MyRoute ("foo/bar", new MyRouteHandler ()));
105 // it tries to get HttpContextBase.Request.Path and causes NIE.
106 m.PostResolveRequestCache (new HttpContextStub2 ("~/foo/bar", null));
111 public void PostResolveRequestCacheCallRewritePath ()
113 var m = new UrlRoutingModule ();
114 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
115 var hc = new HttpContextStub2 ("~/x/y", "z");
117 m.PostResolveRequestCache (hc);
119 } catch (ApplicationException ex) {
120 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
125 public void PostResolveRequestCacheModifiedPath ()
127 var m = new UrlRoutingModule ();
128 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
129 var hc = new HttpContextStub2 ("~/x/y", "z", "apppath");
130 hc.SetResponse (new HttpResponseStub (2));
132 m.PostResolveRequestCache (hc);
134 } catch (ApplicationException ex) {
135 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
140 public void PostResolveRequestCache ()
142 var m = new UrlRoutingModule ();
143 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
145 var hc = new HttpContextStub4 ("~/x/y", "z", "apppath", false);
147 var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", false);
149 hc.SetResponse (new HttpResponseStub (2));
150 m.PostResolveRequestCache (hc);
152 Assert.AreEqual (null, hc.RewrittenPath, "#1");
154 Assert.AreEqual ("~/UrlRouting.axd", hc.RewrittenPath, "#1");
156 // it internally stores the handler
160 public void PostResolveRequestCacheStopRoutingHttpHandler ()
162 var m = new UrlRoutingModule ();
163 RouteTable.Routes.Add (new MyRoute ("foo/bar", new StopRoutingHandler ()));
164 var hc = new HttpContextStub3 ("~/foo/bar", String.Empty, "apppath", false);
165 m.PostResolveRequestCache (hc);
166 Assert.IsNull (hc.RewrittenPath, "StopRoutingHandler should stop before the path is rewritten");
171 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
172 public void PostResolveRequestCachePathToExistingFile ()
174 var m = new UrlRoutingModule ();
175 RouteTable.Routes.Add (new MyRoute ("~/{foo}/{bar}", new MyRouteHandler ()));
176 var hc = new HttpContextStub2 ("~/hoge/fuga", String.Empty, ".");
177 // it tries to get HttpContextBase.Response, so set it.
178 hc.SetResponse (new HttpResponseStub (3));
180 m.PostResolveRequestCache (hc);
182 } catch (ApplicationException ex) {
183 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
188 [ExpectedException (typeof (NotImplementedException))]
189 public void Pipeline1 ()
191 var m = new UrlRoutingModule ();
192 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
193 var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", false);
194 hc.SetResponse (new HttpResponseStub (2));
195 m.PostResolveRequestCache (hc);
196 Assert.AreEqual ("~/UrlRouting.axd", hc.RewrittenPath, "#1");
197 // It tries to set Handler and causes NIE
198 m.PostMapRequestHandler (hc);
202 public void Pipeline2 ()
204 var m = new UrlRoutingModule ();
205 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
207 var hc = new HttpContextStub4 ("~/x/y", "z", "apppath", true);
209 var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", true);
211 hc.HttpHandler = new MyHttpHandler ();
212 hc.SetResponse (new HttpResponseStub (2));
213 m.PostResolveRequestCache (hc);
215 Assert.AreEqual (null, hc.RewrittenPath, "#1");
217 Assert.AreEqual ("~/UrlRouting.axd", hc.RewrittenPath, "#1");
219 // It tries to set Handler and causes NIE
220 m.PostMapRequestHandler (hc);
225 [ExpectedException (typeof (ApplicationException))]
227 public void Pipeline3 ()
229 var m = new UrlRoutingModule ();
230 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
232 var hc = new HttpContextStub5 ("~/x/y", String.Empty, "apppath");
234 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
236 hc.SetResponse (new HttpResponseStub (2));
238 Assert.IsNull (m.RouteCollection.GetRouteData (hc), "#0");
240 Assert.IsNotNull (m.RouteCollection.GetRouteData (hc), "#0");
241 m.PostResolveRequestCache (hc);
243 m.PostMapRequestHandler (hc);
245 } catch (ApplicationException ex) {
246 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");