0

I have ASP.NET MVC5 and WebApi2 application with some MVC and ApiControllers. OWIN and Autofac is used. I wrote Integration test which calls some web api method and tests a response:

[TestMethod]
public async Task GetUsersForTreeTest()
{
  using (var server = TestServer.Create<Startup>())
  using (var client = new HttpClient(server.Handler))
  {
    client.BaseAddress = new Uri("http://localhost/api/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var resp = await client.GetAsync("User/GetUsersForTree");
    resp.EnsureSuccessStatusCode();
    var users = await resp.Content.ReadAsAsync<List<UserForTreeDto>>();
    Assert.IsTrue(users.Any());
  }
}

Startup is my OWIN startup class. I initialize dependency resolvers for MVC and WebApi with Autofac inside.

When I run the test I get an exception:

System.ArgumentNullException: Value cannot be null. Parameter name: httpContext Result StackTrace: at System.Web.HttpContextWrapper..ctor(HttpContext httpContext) at Owin.AutofacMvcAppBuilderExtensions.<>c.<.cctor>b__2_0() at Owin.AutofacMvcAppBuilderExtensions.<>c.<b__1_0>d.MoveNext()

But when I comment

      app.UseAutofacMvc();

the test passed. As I understand, error happes in this line of AutofacMvcAppBuilderExtensions class.

Looks like HttpContext.Current is null.

How can I initialize HttpContext?

MirrorBoy
  • 618
  • 7
  • 11
  • Possible duplicate of [How to access HttpContext inside a unit test in ASP.NET 5 / MVC 6](http://stackoverflow.com/questions/30557521/how-to-access-httpcontext-inside-a-unit-test-in-asp-net-5-mvc-6) – Travis Illig Apr 28 '17 at 23:36
  • Unfortunatelly it doesn't help me. I can't use new DefaultHttpContext(); As I understand it Core feature – MirrorBoy May 03 '17 at 15:13
  • You might want to clarify that restriction (e.g., why you can't use the answers others have used) in your question above. – Travis Illig May 03 '17 at 15:15

1 Answers1

0

Code below works for me:

private static HttpContext GetFakeHttpContext()
{
  var httpRequest = new HttpRequest("", "http://fakeUrl/", "");
  var stringWriter = new StringWriter();
  var httpResponce = new HttpResponse(stringWriter);
  var httpContext = new HttpContext(httpRequest, httpResponce);
  return httpContext;
}

[TestMethod]
public async Task GetUsersForTreeTest()
{
  var server = TestServer.Create(app =>
  {
    app.Use(async (context, next) =>
    {
      HttpContext.Current = GetFakeHttpContext();
      await next();
    });
    new Startup().Configuration(app);
  });
  using (server)
  using (var client = new HttpClient(server.Handler))
  {
    client.BaseAddress = new Uri("http://localhost/api/");

    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var resp = await client.GetAsync("User/GetUsersForTree");
    resp.EnsureSuccessStatusCode();
    var users = await resp.Content.ReadAsAsync<List<UserForTreeDto>>();
    Assert.IsTrue(users.Any());
  }
}
MirrorBoy
  • 618
  • 7
  • 11