This util allows to initialize HttpContex.Current
with fake context.
You can install the utility with NuGet
[Fact]
public void Should_initialize_HttpContext_Current()
{
// Arrange
using (new FakeHttpContext())
{
// Assert
HttpContext.Current.Should().NotBeNull();
}
}
[Fact]
public void Should_fake_user_agent()
{
// Arrange
const string ExpectedUserAgentString = "user agent string";
using (new FakeHttpContext {UserAgent = ExpectedUserAgentString})
{
// Assert
HttpContext.Current.Request.UserAgent.Should().Be(ExpectedUserAgentString);
}
}
[Fact]
public void Should_allow_to_use_map_path()
{
// Arrange
var expectedPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "myPath");
// Act
using (new FakeHttpContext())
{
// Assert
HttpContext.Current.Server.MapPath("myPath").Should().Be(expectedPath);
}
}
[Theory, AutoData]
public void Should_be_possible_to_fake_http_headers(string headerKey, string headerValue)
{
// Act
using (new FakeHttpContext { Request = { { headerKey, headerValue } } })
{
// Assert
HttpContext.Current.Request.Headers[headerKey].Should().Be(headerValue);
}
}
For more examples please see FakeHttpContext.Tests
project.