2009-05-26 Atsushi Enomoto <atsushi@ximian.com>
[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
42                 public HttpContextStub ()
43                         : this (null)
44                 {
45                 }
46
47                 public HttpContextStub (string dummyRequestPath)
48                         : this (dummyRequestPath, null)
49                 {
50                 }
51
52                 public HttpContextStub (string dummyRequestPath, string pathInfo)
53                         : this (dummyRequestPath, pathInfo, null)
54                 {
55                 }
56
57                 public HttpContextStub (string dummyRequestPath, string pathInfo, string method)
58                 {
59                         if (dummyRequestPath != null) {
60                                 req = new HttpRequestStub (dummyRequestPath, pathInfo);
61                                 req.Method = method;
62                         }
63                 }
64
65                 public override HttpRequestBase Request {
66                         get { return req != null ? req : base.Request; }
67                 }
68         }
69
70         class HttpRequestStub : HttpRequestBase
71         {
72                 public HttpRequestStub (string dummyRequestPath, string pathInfo)
73                 {
74                         req_path = dummyRequestPath;
75                         path_info = pathInfo;
76                 }
77
78                 string req_path, path_info;
79                 
80                 public override string AppRelativeCurrentExecutionFilePath {
81                         get { return req_path ?? base.AppRelativeCurrentExecutionFilePath; }
82                 }
83
84                 public override string PathInfo {
85                         get { return path_info ?? base.PathInfo; }
86                 }
87
88                 public override string HttpMethod {
89                         get { return Method; }
90                 }
91
92                 public string Method { get; set; }
93         }
94
95         class MyDictionary : Hashtable
96         {
97                 public override ICollection Keys {
98                         get { return null; }
99                 }
100
101                 public override object this [object key] {
102                         get {
103                                 //Console.Error.WriteLine ("Get: {0} {1}", key, key.GetHashCode ());
104                                 return base [key];
105                         }
106                         set {
107                                 //Console.Error.WriteLine ("Set: {0} {1} = {2}", key, key.GetHashCode (), value);
108                                 base [key] = value; 
109                         }
110                 }
111         }
112
113         class HttpContextStub2 : HttpContextBase
114         {
115                 public HttpContextStub2 ()
116                         : this (null, null)
117                 {
118                 }
119
120                 public HttpContextStub2 (string requestUrl, string path)
121                         : this (requestUrl, path, null)
122                 {
123                 }
124
125                 public HttpContextStub2 (string requestUrl, string path, string appPath)
126                 {
127                         request = new HttpRequestStub2 (requestUrl, path, appPath);
128                 }
129
130                 Hashtable items = new MyDictionary ();
131                 HttpRequestStub request;
132                 HttpResponseBase response;
133
134                 public override IDictionary Items {
135                         get { return items; }
136                 }
137
138                 public override HttpRequestBase Request {
139                         get { return request ?? base.Request; }
140                 }
141
142                 public override HttpResponseBase Response {
143                         get { return response ?? base.Response; }
144                 }
145
146                 public override void RewritePath (string path)
147                 {
148                         throw new ApplicationException (path);
149                 }
150
151                 public void SetResponse (HttpResponseBase response)
152                 {
153                         this.response = response;
154                 }
155         }
156
157         class HttpRequestStub2 : HttpRequestStub
158         {
159                 public HttpRequestStub2 (string dummyRequestPath, string dummyPath, string appPath)
160                         : base (dummyRequestPath, String.Empty)
161                 {
162                         path = dummyPath;
163                         app_path = appPath;
164                 }
165
166                 string path, app_path;
167
168                 public override string ApplicationPath {
169                         get { return app_path ?? base.ApplicationPath; }
170                 }
171
172                 public override string Path {
173                         get { return path ?? base.Path; }
174                 }
175         }
176
177         public class HttpResponseStub : HttpResponseBase
178         {
179                 public HttpResponseStub ()
180                         : this (0)
181                 {
182                 }
183
184                 int impl_type;
185
186                 public HttpResponseStub (int implType)
187                 {
188                         this.impl_type = implType;
189                 }
190
191                 public override string ApplyAppPathModifier (string virtualPath)
192                 {
193                         switch (impl_type) {
194                         case 3:
195                                 return virtualPath; // pass thru
196                         case 2:
197                                 return virtualPath + "_modified";
198                         case 1:
199                                 throw new ApplicationException (virtualPath);
200                         default:
201                                 return base.ApplyAppPathModifier (virtualPath);
202                         }
203                 }
204         }
205
206         class HttpContextStub3 : HttpContextStub2
207         {
208                 public HttpContextStub3 (string requestUrl, string path, string appPath, bool supportHandler)
209                         : base (requestUrl, path, appPath)
210                 {
211                         this.support_handler = supportHandler;
212                 }
213
214                 public override void RewritePath (string path)
215                 {
216                         RewrittenPath = path;
217                 }
218
219                 bool support_handler;
220                 public IHttpHandler HttpHandler { get; set; }
221
222                 public override IHttpHandler Handler {
223                         get { return support_handler ? HttpHandler : base.Handler; }
224                         set {
225                                 if (support_handler)
226                                         HttpHandler = value;
227                                 else
228                                         base.Handler = value;
229                         }
230                 }
231
232                 public string RewrittenPath { get; set; }
233         }
234
235         public class MyStopRoutingHandler : StopRoutingHandler
236         {
237                 public IHttpHandler CallGetHttpHandler (RequestContext rc)
238                 {
239                         return GetHttpHandler (rc);
240                 }
241         }
242
243         public class MyUrlRoutingHandler : UrlRoutingHandler
244         {
245                 public void DoProcessRequest (HttpContextBase httpContext)
246                 {
247                         ProcessRequest (httpContext);
248                 }
249
250                 protected override void VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext)
251                 {
252                         throw new ApplicationException ("MyUrlRoutingHandler");
253                 }
254         }
255
256         public class ErrorRouteHandler : IRouteHandler
257         {
258                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
259                 {
260                         throw new ApplicationException ("ErrorRouteHandler");
261                 }
262         }
263
264         public class MyRouteHandler : IRouteHandler
265         {
266                 public MyRouteHandler ()
267                         : this (new MyHttpHandler ())
268                 {
269                 }
270
271                 public MyRouteHandler (IHttpHandler handler)
272                 {
273                         this.handler = handler;
274                 }
275
276                 IHttpHandler handler;
277
278                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
279                 {
280                         return handler;
281                 }
282         }
283
284         public class MyHttpHandler : IHttpHandler
285         {
286                 public bool IsReusable {
287                         get { return true; }
288                 }
289
290                 public void ProcessRequest (HttpContext ctx)
291                 {
292                         throw new MyException ("HOGE");
293                 }
294         }
295
296         public class MyException : Exception
297         {
298                 public MyException (string msg) : base (msg) {}
299         }
300
301         public class NullRouteHandler : IRouteHandler
302         {
303                 public IHttpHandler GetHttpHandler (RequestContext requestContext)
304                 {
305                         return null;
306                 }
307         }
308
309         public class MyRoute : Route
310         {
311                 public MyRoute (string url, IRouteHandler handler)
312                         : this (url, handler, null)
313                 {
314                 }
315
316                 public MyRoute (string url, IRouteHandler handler, Exception ex)
317                         : base (url, handler)
318                 {
319                         this.ex = ex;
320                 }
321
322                 Exception ex;
323
324                 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
325                 {
326                         if (ex != null)
327                                 throw ex;
328                         return base.GetVirtualPath (requestContext, values);
329                 }
330
331                 public bool DoProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
332                 {
333                         return ProcessConstraint (httpContext, constraint, parameterName, values, routeDirection);
334                 }
335         }
336 }