Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpClient with asp.net WebApi in unit testing scenario
    primarykey
    data
    text
    <p>I have an integration test which I wanted to use as the basis of testing my WebAPI controllers with.</p> <p>Initially I thought I would have to set-up WebAPI in self-host mode and carry-out end-to-end tests over local Http.</p> <p>However I realised later by looking at the tests in the WebApiContrib project that its possible to set up an HttpClient with an HttpServer set-up with the correct service route to the WebAPI controller. I seems I can unit test the controllers without setting up WebApi in self-host mode. I can put in any domain name in the request on the client and HttpClient seems to auto-magically bind to the correct controller.</p> <p>Is there any Http transport happening here, using some local interprocess comms or purely 'seeing' that the server is in the same app domain and thus using reflection? What is happening under the hood for this to happen?</p> <p>code:</p> <pre class="lang-cs prettyprint-override"><code>[Test] public void Test_WebApi_Controller() { Assembly.Load("myproj.Web"); var prodServiceMock = new Mock&lt;IProductService&gt;(); ObjectFactory.Initialize(x =&gt; x.For&lt;IProductService&gt;().Use(prodServiceMock.Object)); var config = new HttpConfiguration(); config.Routes.MapHttpRoute("default", "webapi/{controller}/{id}", new { id = RouteParameter.Optional }); config.ServiceResolver.SetResolver(new WebApiDependencyResolver()); var server = new HttpServer(config); var client = new HttpClient(server); var response = client.GetAsync("http://anything.com/webapi/product").Result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload