817ae81973e892c81add39b6b86cdbd11adbb3db
[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 #if NET_4_0
252         class FakeHttpRequestWrapper : HttpRequestWrapper
253         {
254                 string requestUrl;
255                 string path;
256                 string appPath;
257
258                 public FakeHttpRequestWrapper (string requestUrl, string path, string appPath)
259                         : base (new HttpRequest (path, "http://localhost/", String.Empty))
260                 {
261                         this.requestUrl = requestUrl;
262                         this.path = path;
263                         this.appPath = appPath;
264                 }
265
266                 public override string AppRelativeCurrentExecutionFilePath
267                 {
268                         get
269                         {
270                                 return appPath;
271                         }
272                 }
273         }
274
275         class HttpContextStub4 : HttpContextStub3
276         {
277                 HttpRequestWrapper wrapper;
278
279                 public override HttpRequestBase Request
280                 {
281                         get
282                         {
283                                 return wrapper;
284                         }
285                 }
286
287                 public HttpContextStub4 (string requestUrl, string path, string appPath, bool supportHandler)
288                         : base (requestUrl, path, appPath, supportHandler)
289                 {
290                         wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
291                 }
292         }
293
294         class HttpContextStub5 : HttpContextStub2
295         {
296                 HttpRequestWrapper wrapper;
297
298                 public override HttpRequestBase Request
299                 {
300                         get
301                         {
302                                 return wrapper;
303                         }
304                 }
305
306                 public HttpContextStub5 ()
307                         : this (null, null)
308                 {
309                 }
310
311                 public HttpContextStub5 (string requestUrl, string path)
312                         : this (requestUrl, path, null)
313                 {
314                         
315                 }
316
317                 public HttpContextStub5 (string requestUrl, string path, string appPath)
318                         : base (requestUrl, path, appPath)
319                 {
320                         wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
321                 }
322         }
323 #endif
324         public class MyStopRoutingHandler : StopRoutingHandler
325         {
326                 public IHttpHandler CallGetHttpHandler (RequestContext rc)
327                 {
328                         return GetHttpHandler (rc);
329                 }
330         }
331
332         public class MyUrlRoutingHandler : UrlRoutingHandler
333         {
334                 public void DoProcessRequest (HttpContextBase httpContext)
335                 {
336                         ProcessRequest (httpContext);
337                 }
338
339                 protected override void VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext)
340                 {
341                         throw new ApplicationException ("MyUrlRoutingHandler");
342                 }
343         }
344
345         public class ErrorRouteHandler : IRouteHandler
346         {
347                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
348                 {
349                         throw new ApplicationException ("ErrorRouteHandler");
350                 }
351         }
352
353         public class MyRouteHandler : IRouteHandler
354         {
355                 public MyRouteHandler ()
356                         : this (new MyHttpHandler ())
357                 {
358                 }
359
360                 public MyRouteHandler (IHttpHandler handler)
361                 {
362                         this.handler = handler;
363                 }
364
365                 IHttpHandler handler;
366
367                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
368                 {
369                         return handler;
370                 }
371         }
372
373         public class MyHttpHandler : IHttpHandler
374         {
375                 public bool IsReusable {
376                         get { return true; }
377                 }
378
379                 public void ProcessRequest (HttpContext ctx)
380                 {
381                         throw new MyException ("HOGE");
382                 }
383         }
384
385         public class MyException : Exception
386         {
387                 public MyException (string msg) : base (msg) {}
388         }
389
390         public class NullRouteHandler : IRouteHandler
391         {
392                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
393                 {
394                         return null;
395                 }
396         }
397
398         public class MyRoute : Route
399         {
400                 public MyRoute (string url, IRouteHandler handler)
401                         : this (url, handler, null)
402                 {
403                 }
404
405                 public MyRoute (string url, IRouteHandler handler, Exception ex)
406                         : base (url, handler)
407                 {
408                         this.ex = ex;
409                 }
410
411                 Exception ex;
412
413                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
414                 {
415                         if (ex != null)
416                                 throw ex;
417                         return base.GetVirtualPath (requestContext, values);
418                 }
419
420                 public bool DoProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
421                 {
422                         return ProcessConstraint (httpContext, constraint, parameterName, values, routeDirection);
423                 }
424         }
425 }