[eglib] Prefer <langinfo.h> to <localcharset.h>
[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 #if !NET_4_0
89                 [Test]
90                 [ExpectedException (typeof (InvalidOperationException))]
91                 public void PostResolveRequestCacheNullHttpHandler ()
92                 {
93                         var m = new UrlRoutingModule ();
94                         RouteTable.Routes.Add (new MyRoute ("foo/bar", new NullRouteHandler ()));
95
96                         m.PostResolveRequestCache (new HttpContextStub2 ("~/foo/bar", null));
97                 }
98 #endif
99                 [Test]
100                 [ExpectedException (typeof (NotImplementedException))]
101                 public void PostResolveRequestCacheNoPath ()
102                 {
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));
107                 }
108
109 #if !NET_4_0
110                 [Test]
111                 public void PostResolveRequestCacheCallRewritePath ()
112                 {
113                         var m = new UrlRoutingModule ();
114                         RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
115                         var hc = new HttpContextStub2 ("~/x/y", "z");
116                         try {
117                                 m.PostResolveRequestCache (hc);
118                                 Assert.Fail ("#1");
119                         } catch (ApplicationException ex) {
120                                 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
121                         }
122                 }
123
124                 [Test]
125                 public void PostResolveRequestCacheModifiedPath ()
126                 {
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));
131                         try {
132                                 m.PostResolveRequestCache (hc);
133                                 Assert.Fail ("#1");
134                         } catch (ApplicationException ex) {
135                                 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
136                         }
137                 }
138 #endif
139                 [Test]
140                 public void PostResolveRequestCache ()
141                 {
142                         var m = new UrlRoutingModule ();
143                         RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
144 #if NET_4_0
145                         var hc = new HttpContextStub4 ("~/x/y", "z", "apppath", false);
146 #else
147                         var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", false);
148 #endif
149                         hc.SetResponse (new HttpResponseStub (2));
150                         m.PostResolveRequestCache (hc);
151 #if NET_4_0
152                         Assert.AreEqual (null, hc.RewrittenPath, "#1");
153 #else
154                         Assert.AreEqual ("~/UrlRouting.axd", hc.RewrittenPath, "#1");
155 #endif
156                         // it internally stores the handler 
157                 }
158                 
159                 [Test]
160                 public void PostResolveRequestCacheStopRoutingHttpHandler ()
161                 {
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");
167                 }
168
169
170                 [Test]
171                 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
172                 public void PostResolveRequestCachePathToExistingFile ()
173                 {
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));
179                         try {
180                                 m.PostResolveRequestCache (hc);
181                                 Assert.Fail ("#1");
182                         } catch (ApplicationException ex) {
183                                 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
184                         }
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof (NotImplementedException))]
189                 public void Pipeline1 ()
190                 {
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);
199                 }
200
201                 [Test]
202                 public void Pipeline2 ()
203                 {
204                         var m = new UrlRoutingModule ();
205                         RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
206 #if NET_4_0
207                         var hc = new HttpContextStub4 ("~/x/y", "z", "apppath", true);
208 #else
209                         var hc = new HttpContextStub3 ("~/x/y", "z", "apppath", true);
210 #endif
211                         hc.HttpHandler = new MyHttpHandler ();
212                         hc.SetResponse (new HttpResponseStub (2));
213                         m.PostResolveRequestCache (hc);
214 #if NET_4_0
215                         Assert.AreEqual (null, hc.RewrittenPath, "#1");
216 #else
217                         Assert.AreEqual ("~/UrlRouting.axd", hc.RewrittenPath, "#1");
218 #endif
219                         // It tries to set Handler and causes NIE
220                         m.PostMapRequestHandler (hc);
221                 }
222
223                 [Test]
224 #if !NET_4_0
225                 [ExpectedException (typeof (ApplicationException))]
226 #endif
227                 public void Pipeline3 ()
228                 {
229                         var m = new UrlRoutingModule ();
230                         RouteTable.Routes.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
231 #if NET_4_0
232                         var hc = new HttpContextStub5 ("~/x/y", String.Empty, "apppath");
233 #else
234                         var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
235 #endif
236                         hc.SetResponse (new HttpResponseStub (2));
237 #if NET_4_0
238                         Assert.IsNull (m.RouteCollection.GetRouteData (hc), "#0");
239 #else
240                         Assert.IsNotNull (m.RouteCollection.GetRouteData (hc), "#0");
241                         m.PostResolveRequestCache (hc);
242                         try {
243                                 m.PostMapRequestHandler (hc);
244                                 Assert.Fail ("#1");
245                         } catch (ApplicationException ex) {
246                                 Assert.AreEqual ("~/UrlRouting.axd", ex.Message, "#2");
247                         }
248 #endif
249                 }
250         }
251 }