vendredi 24 juillet 2015

Specifiy IPAdress in HttpRequestMessage

I'm trying to make some Unit tests for a Owin Self-Hosted WebApi Project.

I have configured in the Owin Pipeline an own Middleware provider to filter White- and Blacklisted IpAdress ranges.

So now i want to write some Unit tests where i can configure the IpAddress from which it is send from, so that I can test the Filters.

I have tried to set the Ip-Adress in the MS_Httpcontext directly which is not working because the UserHostAddress is readonly.

[Fact]
public async void StartupLocalRouteFromDifferentIpTest()
{
    using (var server = TestServer.Create<OwinStartup>())
    {
        var request = server.CreateRequest("/");
        request.And(RequestBuilderForIp);

        var response = await request.GetAsync();
        Assert.True(response.StatusCode == HttpStatusCode.OK);
    }
}


private void RequestBuilderForIp(HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
        ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress = "0.0.0.0";
    }
}

Is there a way to specify the IpAdress in code for the HttpRequestMessage? So that I can run my tests.

Aucun commentaire:

Enregistrer un commentaire