I have a web API project that I want to test with unit tests.
In the test class I create a HttpClient that is supposed to redirect the HTTP calls directly to my controllers:
public static HttpClient GetInMemoryServerClient(HttpConfiguration serverConfig)
{
var config = serverConfig ?? new HttpConfiguration();
MyService.WebApiConfig.Register(config, isTest: true);
var server = new HttpServer();
server.Configuration.Services.Replace(typeof(IHttpActionInvoker), server.Configuration.Services.GetActionInvoker());
return new HttpClient(server) { BaseAddress = new Uri("http://fakehost:9999/") };
}
In the service project I have this class that calls config.MapHttpAttributeRoutes(), which works in prod but not when called by the test:
public class WebApiConfig{
//...
public static void Register(HttpConfiguration config) // this is called in Global.asax.cs
{
Register(config, false);
}
public static Register(HttpConfiguration config, bool isTest){
//... various config and dependency injection setup here
config.MapHttpAttributeRoutes();
}
}
My controllers use attribute routing:
[RoutePrefix("api/groups/{groupId}/sets/{setId:minlength(1)}/items")]
public class ItemsController : BaseController
{
[Route("{itemId:minlength(1)}")]
[HttpGet]
public async Task<API.Item> Get(string groupId, string setId, string itemId)
{...}
}
When I use the HttpClient to make a call I get
{"Message":"No HTTP resource was found that matches the request URI
'http://fakehost:9999/api/groups/abc/sets/xyz/items/item123'."}
and inspecting the httpClient with the debugger I see httpClient.handler.Configuration.Routes.Count == 0, so mapping the routing attributes didn't work
Q: What is the correct way to set up the fake HttpClient so my unit tests can use it to call the controllers?
localhostwhen used in testing. So your default address is not going to work. Also this would be categorized as an integration test. not a unit test.