Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / System.Web.Routing / Test / System.Web.Routing / UrlRoutingModuleTest.cs
1 //
2 // UrlRoutingModuleTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System;
31 using System.Web;
32 using System.Web.Routing;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Web.Routing
36 {
37         [TestFixture]
38         public class UrlRoutingModuleTest
39         {
40                 [SetUp]
41                 public void SetUp ()
42                 {
43                         RouteTable.Routes.Clear ();
44                 }
45
46                 [Test]
47                 public void SetRouteCollectionNull ()
48                 {
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");
56                 }
57
58                 // PostMapRequestHandler
59
60                 [Test]
61                 public void PostMapRequestHandlerNoMatch ()
62                 {
63                         var m = new UrlRoutingModule ();
64                         RouteTable.Routes.Add (new MyRoute (null, null));
65                         m.PostMapRequestHandler (new HttpContextStub2 ());
66                 }
67
68                 [Test]
69                 public void PostMapRequestHandlerNoPath ()
70                 {
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));
75                 }
76
77                 // PostResolveRequestCache
78
79                 [Test]
80                 [ExpectedException (typeof (InvalidOperationException))]
81                 public void PostResolveRequestCacheNullRouteHandler ()
82                 {
83                         var m = new UrlRoutingModule ();
84                         RouteTable.Routes.Add (new MyRoute ("foo/bar", null));
85                         m.PostResolveRequestCache (new HttpContextStub2 ("~/foo/bar", null));
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (NotImplementedException))]
90                 public void PostResolveRequestCacheNoPath ()
91                 {
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));
96                 }
97
98                 [Test]
99                 public void PostResolveRequestCache ()
100                 {
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 
108                 }
109                 
110                 [Test]
111                 public void PostResolveRequestCacheStopRoutingHttpHandler ()
112                 {
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");
118                 }
119
120
121                 [Test]
122                 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
123                 public void PostResolveRequestCachePathToExistingFile ()
124                 {
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));
130                         try {
131                                 m.PostResolveRequestCache (hc);
132                                 Assert.Fail ("#1");
133                         } catch (ApplicationException ex) {
134                                 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
135                         }
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (NotImplementedException))]
140                 public void Pipeline1 ()
141                 {
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);
150                 }
151
152                 [Test]
153                 public void Pipeline2 ()
154                 {
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);
164                 }
165
166                 [Test]
167                 public void Pipeline3 ()
168                 {
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");
174                 }
175         }
176 }