Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mcs / class / System.Web.Routing / Test / System.Web.Routing / TestStubTypes.cs
1 //
2 // TestStubTypes.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.Collections;
32 using System.Web;
33 using System.Web.Routing;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Web.Routing
37 {
38         class HttpContextStub : HttpContextBase
39         {
40                 HttpRequestStub req;
41                 bool returnNullRequest;
42
43                 public HttpContextStub ()
44                         : this (null)
45                 {
46                 }
47
48                 public HttpContextStub (bool returnNullRequest)
49                         : this (null)
50                 {
51                         this.returnNullRequest = returnNullRequest;
52                 }
53
54                 public HttpContextStub (string dummyRequestPath)
55                         : this (dummyRequestPath, null)
56                 {
57                 }
58
59                 public HttpContextStub (string dummyRequestPath, string pathInfo)
60                         : this (dummyRequestPath, pathInfo, null)
61                 {
62                 }
63
64                 public HttpContextStub (string dummyRequestPath, string pathInfo, string method)
65                 {
66                         if (dummyRequestPath != null) {
67                                 req = new HttpRequestStub (dummyRequestPath, pathInfo);
68                                 req.Method = method;
69                         }
70                 }
71
72                 public override HttpRequestBase Request {
73                         get {
74                                 if (returnNullRequest)
75                                         return null;
76
77                                 return req != null ? req : base.Request; 
78                         }
79                 }
80         }
81
82         class HttpRequestStub : HttpRequestBase
83         {
84                 public HttpRequestStub (string dummyRequestPath, string pathInfo)
85                 {
86                         req_path = dummyRequestPath;
87                         path_info = pathInfo;
88                 }
89
90                 string req_path, path_info;
91                 
92                 public override string AppRelativeCurrentExecutionFilePath {
93                         get { return req_path ?? base.AppRelativeCurrentExecutionFilePath; }
94                 }
95
96                 public override string PathInfo {
97                         get { return path_info ?? base.PathInfo; }
98                 }
99
100                 public override string HttpMethod {
101                         get { return Method; }
102                 }
103
104                 public string Method { get; set; }
105         }
106
107         class MyDictionary : Hashtable
108         {
109                 public override ICollection Keys {
110                         get { return null; }
111                 }
112
113                 public override object this [object key] {
114                         get {
115                                 //Console.Error.WriteLine ("Get: {0} {1}", key, key.GetHashCode ());
116                                 return base [key];
117                         }
118                         set {
119                                 //Console.Error.WriteLine ("Set: {0} {1} = {2}", key, key.GetHashCode (), value);
120                                 base [key] = value; 
121                         }
122                 }
123         }
124
125         class HttpContextStub2 : HttpContextBase
126         {
127                 public HttpContextStub2 ()
128                         : this (null, null)
129                 {
130                 }
131
132                 public HttpContextStub2 (string requestUrl, string path)
133                         : this (requestUrl, path, null)
134                 {
135                 }
136
137                 public HttpContextStub2 (string requestUrl, string path, string appPath)
138                 {
139                         request = new HttpRequestStub2 (requestUrl, path, appPath);
140                 }
141
142                 Hashtable items = new MyDictionary ();
143                 HttpRequestStub request;
144                 HttpResponseBase response;
145
146                 public override IDictionary Items {
147                         get { return items; }
148                 }
149
150                 public override HttpRequestBase Request {
151                         get { return request ?? base.Request; }
152                 }
153
154                 public override HttpResponseBase Response {
155                         get { return response ?? base.Response; }
156                 }
157
158                 public override void RewritePath (string path)
159                 {
160                         throw new ApplicationException (path);
161                 }
162
163                 public void SetResponse (HttpResponseBase response)
164                 {
165                         this.response = response;
166                 }
167
168                 public void SetRequest (HttpRequestStub request)
169                 {
170                         this.request = request;
171                 }
172         }
173
174         class HttpRequestStub2 : HttpRequestStub
175         {
176                 public HttpRequestStub2 (string dummyRequestPath, string dummyPath, string appPath)
177                         : base (dummyRequestPath, String.Empty)
178                 {
179                         path = dummyPath;
180                         app_path = appPath;
181                 }
182
183                 string path, app_path;
184
185                 public override string ApplicationPath {
186                         get { return app_path ?? base.ApplicationPath; }
187                 }
188
189                 public override string Path {
190                         get { return path ?? base.Path; }
191                 }
192         }
193
194         public class HttpResponseStub : HttpResponseBase
195         {
196                 public HttpResponseStub ()
197                         : this (0)
198                 {
199                 }
200
201                 int impl_type;
202
203                 public HttpResponseStub (int implType)
204                 {
205                         this.impl_type = implType;
206                 }
207
208                 public override string ApplyAppPathModifier (string virtualPath)
209                 {
210                         switch (impl_type) {
211                         case 3:
212                                 return virtualPath; // pass thru
213                         case 2:
214                                 return virtualPath + "_modified";
215                         case 1:
216                                 throw new ApplicationException (virtualPath);
217                         default:
218                                 return base.ApplyAppPathModifier (virtualPath);
219                         }
220                 }
221         }
222
223         class HttpContextStub3 : HttpContextStub2
224         {
225                 public HttpContextStub3 (string requestUrl, string path, string appPath, bool supportHandler)
226                         : base (requestUrl, path, appPath)
227                 {
228                         this.support_handler = supportHandler;
229                 }
230
231                 public override void RewritePath (string path)
232                 {
233                         RewrittenPath = path;
234                 }
235
236                 bool support_handler;
237                 public IHttpHandler HttpHandler { get; set; }
238
239                 public override IHttpHandler Handler {
240                         get { return support_handler ? HttpHandler : base.Handler; }
241                         set {
242                                 if (support_handler)
243                                         HttpHandler = value;
244                                 else
245                                         base.Handler = value;
246                         }
247                 }
248
249                 public string RewrittenPath { get; set; }
250         }
251         class FakeHttpRequestWrapper : HttpRequestWrapper
252         {
253                 string requestUrl;
254                 string path;
255                 string appPath;
256
257                 public FakeHttpRequestWrapper (string requestUrl, string path, string appPath)
258                         : base (new HttpRequest (path, "http://localhost/", String.Empty))
259                 {
260                         this.requestUrl = requestUrl;
261                         this.path = path;
262                         this.appPath = appPath;
263                 }
264
265                 public override string AppRelativeCurrentExecutionFilePath
266                 {
267                         get
268                         {
269                                 return appPath;
270                         }
271                 }
272         }
273
274         class HttpContextStub4 : HttpContextStub3
275         {
276                 HttpRequestWrapper wrapper;
277
278                 public override HttpRequestBase Request
279                 {
280                         get
281                         {
282                                 return wrapper;
283                         }
284                 }
285
286                 public HttpContextStub4 (string requestUrl, string path, string appPath, bool supportHandler)
287                         : base (requestUrl, path, appPath, supportHandler)
288                 {
289                         wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
290                 }
291         }
292
293         class HttpContextStub5 : HttpContextStub2
294         {
295                 HttpRequestWrapper wrapper;
296
297                 public override HttpRequestBase Request
298                 {
299                         get
300                         {
301                                 return wrapper;
302                         }
303                 }
304
305                 public HttpContextStub5 ()
306                         : this (null, null)
307                 {
308                 }
309
310                 public HttpContextStub5 (string requestUrl, string path)
311                         : this (requestUrl, path, null)
312                 {
313                         
314                 }
315
316                 public HttpContextStub5 (string requestUrl, string path, string appPath)
317                         : base (requestUrl, path, appPath)
318                 {
319                         wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
320                 }
321         }
322         public class MyStopRoutingHandler : StopRoutingHandler
323         {
324                 public IHttpHandler CallGetHttpHandler (RequestContext rc)
325                 {
326                         return GetHttpHandler (rc);
327                 }
328         }
329
330         public class MyUrlRoutingHandler : UrlRoutingHandler
331         {
332                 public void DoProcessRequest (HttpContextBase httpContext)
333                 {
334                         ProcessRequest (httpContext);
335                 }
336
337                 protected override void VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext)
338                 {
339                         throw new ApplicationException ("MyUrlRoutingHandler");
340                 }
341         }
342
343         public class ErrorRouteHandler : IRouteHandler
344         {
345                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
346                 {
347                         throw new ApplicationException ("ErrorRouteHandler");
348                 }
349         }
350
351         public class MyRouteHandler : IRouteHandler
352         {
353                 public MyRouteHandler ()
354                         : this (new MyHttpHandler ())
355                 {
356                 }
357
358                 public MyRouteHandler (IHttpHandler handler)
359                 {
360                         this.handler = handler;
361                 }
362
363                 IHttpHandler handler;
364
365                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
366                 {
367                         return handler;
368                 }
369         }
370
371         public class MyHttpHandler : IHttpHandler
372         {
373                 public bool IsReusable {
374                         get { return true; }
375                 }
376
377                 public void ProcessRequest (HttpContext ctx)
378                 {
379                         throw new MyException ("HOGE");
380                 }
381         }
382
383         public class MyException : Exception
384         {
385                 public MyException (string msg) : base (msg) {}
386         }
387
388         public class NullRouteHandler : IRouteHandler
389         {
390                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
391                 {
392                         return null;
393                 }
394         }
395
396         public class MyRoute : Route
397         {
398                 public MyRoute (string url, IRouteHandler handler)
399                         : this (url, handler, null)
400                 {
401                 }
402
403                 public MyRoute (string url, IRouteHandler handler, Exception ex)
404                         : base (url, handler)
405                 {
406                         this.ex = ex;
407                 }
408
409                 Exception ex;
410
411                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
412                 {
413                         if (ex != null)
414                                 throw ex;
415                         return base.GetVirtualPath (requestContext, values);
416                 }
417
418                 public bool DoProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
419                 {
420                         return ProcessConstraint (httpContext, constraint, parameterName, values, routeDirection);
421                 }
422         }
423 }