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.