locked
How to shim HttpResponseMessage.Headers.Location in an ApiController? RRS feed

  • Question

  • I'm using Microsft fakes and it works for everything we are implemting tests for except I can't shim (or stub) 

    HttpResponseMessage .Headers.Location

    // Code listed below

    HttpResponseMessage response;

    response = Request.CreateResponse(HttpStatusCode.Created, uploadResult, new JsonMediaTypeFormatter()); // Works fine
    response.Headers.Location = new Uri("xxx"); // Always get Location = null....

    How can i set the Location in Headers in code below which defines our shim?

    // The shim looks like this

     HttpRequestMessage requestMessage = new HttpRequestMessage(method, "http://localhost/api/" + apiUri);

                        

     MultipartFormDataContent multipartContent = new MultipartFormDataContent("----Boundary----");
     multipartContent.Headers.ContentLocation = new Uri("http://localhost/api/" + apiUri);
     ByteArrayContent content = new ByteArrayContent(fileContents);
    content.Headers.Add("Content-Type", "image/jpeg");
     multipartContent.Add(content, "content", "test file.jpg");

     requestMessage.Content = multipartContent;

     IHttpRoute route = configuration.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
     HttpRouteData routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", controllerName }, { "action", actionName } });

     HttpControllerContext context = new HttpControllerContext(configuration, routeData, requestMessage);
     ApiController controller.ControllerContext = context;
                        controller.Request = requestMessage;
                        controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;

    Tuesday, October 7, 2014 1:00 PM

All replies

  • Hi Agnes,

    This blog: ASP.NET Web Api, Faking ApiController in designing true Restful services described detailed steps to fake ApiController. There are detailed code snippets in that blog.

    The author used the following code to define Location:

    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = customer.Id }));

    Hoping it can help you. You can feel free to come back if it is not helpful and provide us more information.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Amanda Zhu Monday, October 13, 2014 1:54 AM
    Wednesday, October 8, 2014 9:54 AM
  • Hi Agnes,

    What about your issue now?

    If you have resolved it, you can share the solution here, which will be beneficial for other members with the same issue.

    If you did not, please tell us the result of my suggestion, I will try my best to help you.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 13, 2014 1:54 AM