Need to add custom header to request in unit test
Asked Answered
S

3

21

I finally was able to get the HttpContext.Current to be not null by finding some code online. But I still have not be able to add custom headers to the request in my unit test. Here is my test:

[TestClass]
public class TagControllerTest
{
    private static Mock<IGenericService<Tag>> Service { get; set; }
    private TagController controller;

    [TestInitialize]
    public void ThingServiceTestSetUp()
    {
        Tag tag = new Tag(1, "people");
        Response<Tag> response = new Response<Tag>();
        response.PayLoad = new List<Tag>() { tag };

        Service = new Mock<IGenericService<Tag>>(MockBehavior.Default);
        Service.Setup(s => s.FindAll("username", "password", "token")).Returns(response);

        controller = new TagController(Service.Object);
        HttpContext.Current = FakeHttpContext();
    }

    public static HttpContext FakeHttpContext()
    {
        var httpRequest = new HttpRequest("", "http://kindermusik/", "");
        var stringWriter = new StringWriter();
        var httpResponce = new HttpResponse(stringWriter);
        var httpContext = new HttpContext(httpRequest, httpResponce);

        var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
                                                new HttpStaticObjectsCollection(), 10, true,
                                                HttpCookieMode.AutoDetect,
                                                SessionStateMode.InProc, false);

        httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
                                    BindingFlags.NonPublic | BindingFlags.Instance,
                                    null, CallingConventions.Standard,
                                    new[] { typeof(HttpSessionStateContainer) },
                                    null)
                            .Invoke(new object[] { sessionContainer });
        httpContext.Request.Headers["username"] = "username"; //It throws a PlatformNotSupportedException exception
        httpContext.Request.Headers["password"] = "password"; //.Headers.Add("blah", "blah") throws same error
        httpContext.Request.Headers["token"] = "token"; //And so to .Headers.Set("blah", "blah")

        return httpContext;
    }

    [TestMethod]
    public void TagControllerGetTest()
    {
        // Arrange
        Response<Tag> result = controller.Get();

        // Assert
        Assert.AreEqual(true, result.IsSuccess);
        Assert.AreEqual(1, result.PayLoad.Count);
        Assert.AreEqual("people", result.PayLoad[0].Name);
    }

This is the code that is being tested.

public class TagController : ApiController
{
    public IGenericService<Tag> _service;

    public TagController()
    {
        _service = new TagService();
    }

    public TagController(IGenericService<Tag> service)
    {
        this._service = service;
    }

    // GET api/values
    public Response<Tag> Get()
    {
        HttpContext context = HttpContext.Current;
        string username = context.Request.Headers["username"].ToString();
        string password = context.Request.Headers["password"].ToString();
        string token = context.Request.Headers["token"].ToString();
        return (Response<Tag>) _service.FindAll(username, password, token);
    }
}
Sustentacular answered 20/8, 2013 at 15:20 Comment(0)
M
6

You can use this, it worked with:

Setting HttpContext.Current.Session in a unit test

User Anthony's answer, and add this code in GetMockedHttpContext:

request.SetupGet(req => req.Headers).Returns(new NameValueCollection());

then you can add:

HttpContextFactory.Current.Request.Headers.Add(key, value);

by this you can post headers. But unfortunately you have to use HttpContextFactory instead of HttpContext

Marozas answered 24/9, 2013 at 13:9 Comment(0)
G
6

Thanks to Adam Reed's blog it is possible to modify Headers collection using reflexion : MOCK HTTPCONTEXT.CURRENT.REQUEST.HEADERS UNIT TEST

HttpContext.Current = new HttpContext(
new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));

NameValueCollection headers = HttpContext.Current.Request.Headers;

Type t = headers.GetType();
const BindingFlags nonPublicInstanceMethod = BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance;

t.InvokeMember("MakeReadWrite", nonPublicInstanceMethod, null, headers, null);
t.InvokeMember("InvalidateCachedArrays", nonPublicInstanceMethod, null, headers, null);

// eg. add Basic Authorization header
t.InvokeMember("BaseRemove", nonPublicInstanceMethod, null, headers, new object[] { "Authorization" });
t.InvokeMember("BaseAdd", nonPublicInstanceMethod, null, headers, 
    new object[] { "Authorization", new ArrayList{"Basic " + api_key} });

t.InvokeMember("MakeReadOnly", nonPublicInstanceMethod, null, headers, null);
Gastrovascular answered 2/2, 2018 at 14:16 Comment(0)
I
1

I believe that in the API controller methods you can use "Request" property:

var testValue = this.Request.Headers.GetValues("headerKey").FirstOrDefault();

And then you can add test values in your unit tests this way:

var controller = new TestController();
controller.Request = new HttpRequestMessage();
controller.Request.Headers.Add("headerKey", "testValue");
Irrespirable answered 23/4, 2021 at 6:17 Comment(1)
You cannot set controller.Request. You will get an error. Property or indexer 'property' cannot be assigned to -- it is read onlyCaviar

© 2022 - 2024 — McMap. All rights reserved.