I'm trying to create a dummy BaseHttpContext
to do some (mock) testing without having to do an actual request. I like to add an IP address to my dummy context.
string url = "http://www.google.com";
//wrap in uri
Uri uri = new Uri(url);
//create request
var request = new HttpRequest("", uri.GetLeftPart(UriPartial.Path), uri.Query);
request.ServerVariables.Add("REMOTE_ADDR", ip);
//dummy context
HttpContext context = new HttpContext(
new HttpRequest("", uri.GetLeftPart(UriPartial.Path), uri.Query),
new HttpResponse(new StringWriter(System.Text.Encoding.UTF8))
);
//wrap context
var contextWrapper = new HttpContextWrapper(context);
The IP should be reflected in the BaseHttpContext.Request.UserHostAddress
. Adding an IP like this: request.ServerVariables.Add("REMOTE_ADDR", ip);
, results in the following exception:
Specified method is not supported.
This post is very helpful. To solve your issue you just need to implement HttpRequestExtensions
and NameValueCollectionExtensions
classes from the post.
You can use it like this:
string url = "http://127.0.0.1";
var request = new HttpRequest(string.Empty, url, string.Empty);
request.AddServerVariable("REMOTE_ADDR", "127.0.0.1");
Looks like faking an HttpContext
is not so easy and involves a lot of work. Using a mocking framework could make stuff easier. Here is an example using Moq:
/// <summary>
/// Fakes the HTTP context.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="ip">The ip.</param>
/// <param name="referrer">The referrer.</param>
/// <returns>A HTTP context.</returns>
public static HttpContextBase FakeHttpContext(string url, string ip, string referrer)
{
Uri uri = new Uri(url);
var context = new Mock<HttpContextBase>();
var files = new Mock<HttpFileCollectionBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
request.Setup(req => req.ApplicationPath).Returns("~/");
request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns("~/");
request.Setup(req => req.PathInfo).Returns(string.Empty);
request.Setup(req => req.Form).Returns(new NameValueCollection());
request.Setup(req => req.QueryString).Returns(HttpUtility.ParseQueryString(uri.Query));
request.Setup(req => req.Files).Returns(files.Object);
request.Setup(req => req.UserHostAddress).Returns(ip);
request.Setup(req => req.UrlReferrer).Returns(new Uri(referrer));
request.Setup(req => req.Url).Returns(uri);
request.Setup(req => req.RawUrl).Returns(url);
response.Setup(res => res.ApplyAppPathModifier(It.IsAny<string>())).Returns((string virtualPath) => virtualPath);
user.Setup(usr => usr.Identity).Returns(identity.Object);
identity.SetupGet(ident => ident.IsAuthenticated).Returns(true);
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
context.Setup(ctx => ctx.User).Returns(user.Object);
return context.Object;
}
Inspired by this git post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With