8

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.

4

2 回答 2

3

这篇文章很有帮助。要解决您的问题,您只需要从帖子中实现HttpRequestExtensionsNameValueCollectionExtensions类。

你可以像这样使用它:

string url = "http://127.0.0.1";
var request = new HttpRequest(string.Empty, url, string.Empty);
request.AddServerVariable("REMOTE_ADDR", "127.0.0.1");
于 2017-06-19T09:09:31.710 回答
1

看起来假装HttpContext不是那么容易,需要做很多工作。使用模拟框架可以使事情变得更容易。这是一个使用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;
}

这篇 git post 的启发。

于 2012-10-31T14:34:50.200 回答