5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 using System.Web.Routing;
34 using NUnit.Framework;
36 namespace MonoTests.System.Web.Routing
38 class HttpContextStub : HttpContextBase
41 bool returnNullRequest;
43 public HttpContextStub ()
48 public HttpContextStub (bool returnNullRequest)
51 this.returnNullRequest = returnNullRequest;
54 public HttpContextStub (string dummyRequestPath)
55 : this (dummyRequestPath, null)
59 public HttpContextStub (string dummyRequestPath, string pathInfo)
60 : this (dummyRequestPath, pathInfo, null)
64 public HttpContextStub (string dummyRequestPath, string pathInfo, string method)
66 if (dummyRequestPath != null) {
67 req = new HttpRequestStub (dummyRequestPath, pathInfo);
72 public override HttpRequestBase Request {
74 if (returnNullRequest)
77 return req != null ? req : base.Request;
82 class HttpRequestStub : HttpRequestBase
84 public HttpRequestStub (string dummyRequestPath, string pathInfo)
86 req_path = dummyRequestPath;
90 string req_path, path_info;
92 public override string AppRelativeCurrentExecutionFilePath {
93 get { return req_path ?? base.AppRelativeCurrentExecutionFilePath; }
96 public override string PathInfo {
97 get { return path_info ?? base.PathInfo; }
100 public override string HttpMethod {
101 get { return Method; }
104 public string Method { get; set; }
107 class MyDictionary : Hashtable
109 public override ICollection Keys {
113 public override object this [object key] {
115 //Console.Error.WriteLine ("Get: {0} {1}", key, key.GetHashCode ());
119 //Console.Error.WriteLine ("Set: {0} {1} = {2}", key, key.GetHashCode (), value);
125 class HttpContextStub2 : HttpContextBase
127 public HttpContextStub2 ()
132 public HttpContextStub2 (string requestUrl, string path)
133 : this (requestUrl, path, null)
137 public HttpContextStub2 (string requestUrl, string path, string appPath)
139 request = new HttpRequestStub2 (requestUrl, path, appPath);
142 Hashtable items = new MyDictionary ();
143 HttpRequestStub request;
144 HttpResponseBase response;
146 public override IDictionary Items {
147 get { return items; }
150 public override HttpRequestBase Request {
151 get { return request ?? base.Request; }
154 public override HttpResponseBase Response {
155 get { return response ?? base.Response; }
158 public override void RewritePath (string path)
160 throw new ApplicationException (path);
163 public void SetResponse (HttpResponseBase response)
165 this.response = response;
168 public void SetRequest (HttpRequestStub request)
170 this.request = request;
174 class HttpRequestStub2 : HttpRequestStub
176 public HttpRequestStub2 (string dummyRequestPath, string dummyPath, string appPath)
177 : base (dummyRequestPath, String.Empty)
183 string path, app_path;
185 public override string ApplicationPath {
186 get { return app_path ?? base.ApplicationPath; }
189 public override string Path {
190 get { return path ?? base.Path; }
194 public class HttpResponseStub : HttpResponseBase
196 public HttpResponseStub ()
203 public HttpResponseStub (int implType)
205 this.impl_type = implType;
208 public override string ApplyAppPathModifier (string virtualPath)
212 return virtualPath; // pass thru
214 return virtualPath + "_modified";
216 throw new ApplicationException (virtualPath);
218 return base.ApplyAppPathModifier (virtualPath);
223 class HttpContextStub3 : HttpContextStub2
225 public HttpContextStub3 (string requestUrl, string path, string appPath, bool supportHandler)
226 : base (requestUrl, path, appPath)
228 this.support_handler = supportHandler;
231 public override void RewritePath (string path)
233 RewrittenPath = path;
236 bool support_handler;
237 public IHttpHandler HttpHandler { get; set; }
239 public override IHttpHandler Handler {
240 get { return support_handler ? HttpHandler : base.Handler; }
245 base.Handler = value;
249 public string RewrittenPath { get; set; }
251 class FakeHttpRequestWrapper : HttpRequestWrapper
257 public FakeHttpRequestWrapper (string requestUrl, string path, string appPath)
258 : base (new HttpRequest (path, "http://localhost/", String.Empty))
260 this.requestUrl = requestUrl;
262 this.appPath = appPath;
265 public override string AppRelativeCurrentExecutionFilePath
274 class HttpContextStub4 : HttpContextStub3
276 HttpRequestWrapper wrapper;
278 public override HttpRequestBase Request
286 public HttpContextStub4 (string requestUrl, string path, string appPath, bool supportHandler)
287 : base (requestUrl, path, appPath, supportHandler)
289 wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
293 class HttpContextStub5 : HttpContextStub2
295 HttpRequestWrapper wrapper;
297 public override HttpRequestBase Request
305 public HttpContextStub5 ()
310 public HttpContextStub5 (string requestUrl, string path)
311 : this (requestUrl, path, null)
316 public HttpContextStub5 (string requestUrl, string path, string appPath)
317 : base (requestUrl, path, appPath)
319 wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
322 public class MyStopRoutingHandler : StopRoutingHandler
324 public IHttpHandler CallGetHttpHandler (RequestContext rc)
326 return GetHttpHandler (rc);
330 public class MyUrlRoutingHandler : UrlRoutingHandler
332 public void DoProcessRequest (HttpContextBase httpContext)
334 ProcessRequest (httpContext);
337 protected override void VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext)
339 throw new ApplicationException ("MyUrlRoutingHandler");
343 public class ErrorRouteHandler : IRouteHandler
345 public IHttpHandler GetHttpHandler (RequestContext requestContext)
347 throw new ApplicationException ("ErrorRouteHandler");
351 public class MyRouteHandler : IRouteHandler
353 public MyRouteHandler ()
354 : this (new MyHttpHandler ())
358 public MyRouteHandler (IHttpHandler handler)
360 this.handler = handler;
363 IHttpHandler handler;
365 public IHttpHandler GetHttpHandler (RequestContext requestContext)
371 public class MyHttpHandler : IHttpHandler
373 public bool IsReusable {
377 public void ProcessRequest (HttpContext ctx)
379 throw new MyException ("HOGE");
383 public class MyException : Exception
385 public MyException (string msg) : base (msg) {}
388 public class NullRouteHandler : IRouteHandler
390 public IHttpHandler GetHttpHandler (RequestContext requestContext)
396 public class MyRoute : Route
398 public MyRoute (string url, IRouteHandler handler)
399 : this (url, handler, null)
403 public MyRoute (string url, IRouteHandler handler, Exception ex)
404 : base (url, handler)
411 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
415 return base.GetVirtualPath (requestContext, values);
418 public bool DoProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
420 return ProcessConstraint (httpContext, constraint, parameterName, values, routeDirection);