0

In my attempt to learn and understand WebAPI I think I have learnt that returns from APIController methods are wrapped up into HttpResponseMessage.

You also have the option to create a new HttpResponseMessage and put your List<Product> inside it.

I am interested in testing headers for example and if I pass in a accept header with the below code I want to test for the content type returned in the response.

I know I can always return a HttpResponseMessage because strictly thats what its doing but I was just wondering if there was a way to either cast the response back from a controller method as a HttpResponseMessage without having to create a HttpServer/HttpConfiguration setup as shown here.

        var config = new HttpConfiguration();


        var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products");
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var route = config.Routes.MapHttpRoute(
         name: "DefaultApi",
         routeTemplate: "{controller}"
            );

        var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "GetData" } });
        var controller = new GetDataController();
        controller.ControllerContext = new HttpControllerContext(config, routeData, request);
        controller.Request = request;
        controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

        /************************* HELP!!!***************/
        // If Get returns List<Product> can I cast it as HttpResponseMessage??
        /************************************************/

        var result = controller.Get();

        // Assert
        Assert.Equal("application/json", result.Content.Headers.ContentType.MediaType);
4
  • Are you trying create an integration test by creating a standalone server and sending an actual HTTP request as Despertar suggests or are you creating a unit test to check your controller code directly? Commented Nov 26, 2012 at 18:54
  • Well I was hoping I could make it so that it can be called and get a valid HTTP response back without a HttpServer Commented Nov 26, 2012 at 19:27
  • You'll need a running instance of the controller then as Despertar is suggesting. Commented Nov 26, 2012 at 19:34
  • Because responses are only populated properly when invoked by a valid HTTP Request? Commented Nov 26, 2012 at 19:36

1 Answer 1

1

Controller methods are not meant to be called directly like that. You have to send an HTTP request to get an HTTP response. Try using

HttpResponseMessage response = HttpClient.SendAsync(request).Result;

Although if you are not hosting in IIS you would have to create an HttpSelfHostServer to host the controller inside the process so that it can listen for requests. This will show you how to do that, http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure I agree that controller methods are not meant to be called directly because in some cases you should call them however I want a HttpResponseMessage back but was wondering if that could be done without a HttpServer
Agree with Jon. You might want to unit test your controllers, and then this becomes an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.