Skip to content

Instantly share code, notes, and snippets.

@curtismitchell
Last active December 19, 2015 02:08
Show Gist options
  • Save curtismitchell/5880719 to your computer and use it in GitHub Desktop.
Save curtismitchell/5880719 to your computer and use it in GitHub Desktop.
HttpContextStub for NET
// taken from Brad Wilson's blog: http://bradwilson.typepad.com/blog/2010/07/testing-routing-and-url-generation-in-aspnet-mvc.html
public class HttpContextStub : HttpContextBase
{
HttpRequestStub _request;
HttpResponseStub _response;
public HttpContextStub(string appPath = "/", string requestUrl = "~/")
{
_request = new HttpRequestStub(appPath, requestUrl);
_response = new HttpResponseStub();
}
public override HttpRequestBase Request
{
get { return _request; }
}
public override HttpResponseBase Response
{
get { return _response; }
}
}
public class HttpRequestStub : HttpRequestBase
{
string _appPath;
string _requestUrl;
public HttpRequestStub(string appPath, string requestUrl)
{
_appPath = appPath;
_requestUrl = requestUrl;
}
public override string ApplicationPath
{
get { return _appPath; }
}
public override string AppRelativeCurrentExecutionFilePath
{
get { return _requestUrl; }
}
public override string PathInfo
{
get { return ""; }
}
public override System.Uri Url
{
get
{
return new System.Uri(_requestUrl);
}
}
public override NameValueCollection ServerVariables
{
get { return new NameValueCollection(); }
}
}
public class HttpResponseStub : HttpResponseBase
{
public override string ApplyAppPathModifier(string virtualPath)
{
return virtualPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment