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));
89 [ExpectedException (typeof (NotImplementedException))]
90 public void PostResolveRequestCacheNoPath ()
92 var m = new UrlRoutingModule ();
93 RouteTable.Routes.Add (new MyRoute ("foo/bar", new MyRouteHandler ()));
94 // it tries to get HttpContextBase.Request.Path and causes NIE.
95 m.PostResolveRequestCache (new HttpContextStub2 ("~/foo/bar", null));
99 public void PostResolveRequestCache ()
101 var m = new UrlRoutingModule ();
102 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
103 var hc = new HttpContextStub4 ("~/x/y", "z", "apppath", false);
104 hc.SetResponse (new HttpResponseStub (2));
105 m.PostResolveRequestCache (hc);
106 Assert.AreEqual (null, hc.RewrittenPath, "#1");
107 // it internally stores the handler
111 public void PostResolveRequestCacheStopRoutingHttpHandler ()
113 var m = new UrlRoutingModule ();
114 RouteTable.Routes.Add (new MyRoute ("foo/bar", new StopRoutingHandler ()));
115 var hc = new HttpContextStub3 ("~/foo/bar", String.Empty, "apppath", false);
116 m.PostResolveRequestCache (hc);
117 Assert.IsNull (hc.RewrittenPath, "StopRoutingHandler should stop before the path is rewritten");
122 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
123 public void PostResolveRequestCachePathToExistingFile ()
125 var m = new UrlRoutingModule ();
126 RouteTable.Routes.Add (new MyRoute ("~/{foo}/{bar}", new MyRouteHandler ()));
127 var hc = new HttpContextStub2 ("~/hoge/fuga", String.Empty, ".");
128 // it tries to get HttpContextBase.Response, so set it.
129 hc.SetResponse (new HttpResponseStub (3));
131 m.PostResolveRequestCache (hc);
133 } catch (ApplicationException ex) {
134 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
139 [ExpectedException (typeof (NotImplementedException))]
140 public void Pipeline1 ()
142 var m = new UrlRoutingModule ();
143 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
144 var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", false);
145 hc.SetResponse (new HttpResponseStub (2));
146 m.PostResolveRequestCache (hc);
147 Assert.AreEqual ("~/UrlRouting.axd", hc.RewrittenPath, "#1");
148 // It tries to set Handler and causes NIE
149 m.PostMapRequestHandler (hc);
153 public void Pipeline2 ()
155 var m = new UrlRoutingModule ();
156 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
157 var hc = new HttpContextStub4 ("~/x/y", "z", "apppath", true);
158 hc.HttpHandler = new MyHttpHandler ();
159 hc.SetResponse (new HttpResponseStub (2));
160 m.PostResolveRequestCache (hc);
161 Assert.AreEqual (null, hc.RewrittenPath, "#1");
162 // It tries to set Handler and causes NIE
163 m.PostMapRequestHandler (hc);
167 public void Pipeline3 ()
169 var m = new UrlRoutingModule ();
170 RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
171 var hc = new HttpContextStub5 ("~/x/y", String.Empty, "apppath");
172 hc.SetResponse (new HttpResponseStub (2));
173 Assert.IsNull (m.RouteCollection.GetRouteData (hc), "#0");