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; }
252 class FakeHttpRequestWrapper : HttpRequestWrapper
258 public FakeHttpRequestWrapper (string requestUrl, string path, string appPath)
259 : base (new HttpRequest (path, "http://localhost/", String.Empty))
261 this.requestUrl = requestUrl;
263 this.appPath = appPath;
266 public override string AppRelativeCurrentExecutionFilePath
275 class HttpContextStub4 : HttpContextStub3
277 HttpRequestWrapper wrapper;
279 public override HttpRequestBase Request
287 public HttpContextStub4 (string requestUrl, string path, string appPath, bool supportHandler)
288 : base (requestUrl, path, appPath, supportHandler)
290 wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
294 class HttpContextStub5 : HttpContextStub2
296 HttpRequestWrapper wrapper;
298 public override HttpRequestBase Request
306 public HttpContextStub5 ()
311 public HttpContextStub5 (string requestUrl, string path)
312 : this (requestUrl, path, null)
317 public HttpContextStub5 (string requestUrl, string path, string appPath)
318 : base (requestUrl, path, appPath)
320 wrapper = new FakeHttpRequestWrapper (requestUrl, path, appPath);
324 public class MyStopRoutingHandler : StopRoutingHandler
326 public IHttpHandler CallGetHttpHandler (RequestContext rc)
328 return GetHttpHandler (rc);
332 public class MyUrlRoutingHandler : UrlRoutingHandler
334 public void DoProcessRequest (HttpContextBase httpContext)
336 ProcessRequest (httpContext);
339 protected override void VerifyAndProcessRequest (IHttpHandler httpHandler, HttpContextBase httpContext)
341 throw new ApplicationException ("MyUrlRoutingHandler");
345 public class ErrorRouteHandler : IRouteHandler
347 public IHttpHandler GetHttpHandler (RequestContext requestContext)
349 throw new ApplicationException ("ErrorRouteHandler");
353 public class MyRouteHandler : IRouteHandler
355 public MyRouteHandler ()
356 : this (new MyHttpHandler ())
360 public MyRouteHandler (IHttpHandler handler)
362 this.handler = handler;
365 IHttpHandler handler;
367 public IHttpHandler GetHttpHandler (RequestContext requestContext)
373 public class MyHttpHandler : IHttpHandler
375 public bool IsReusable {
379 public void ProcessRequest (HttpContext ctx)
381 throw new MyException ("HOGE");
385 public class MyException : Exception
387 public MyException (string msg) : base (msg) {}
390 public class NullRouteHandler : IRouteHandler
392 public IHttpHandler GetHttpHandler (RequestContext requestContext)
398 public class MyRoute : Route
400 public MyRoute (string url, IRouteHandler handler)
401 : this (url, handler, null)
405 public MyRoute (string url, IRouteHandler handler, Exception ex)
406 : base (url, handler)
413 public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values)
417 return base.GetVirtualPath (requestContext, values);
420 public bool DoProcessConstraint (HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
422 return ProcessConstraint (httpContext, constraint, parameterName, values, routeDirection);