This library try to solve some common issues when doing integration tests with AspNetCore.TestHost
Most Http API's are protected with some authentication scheme that ensures only authenticated users can send requests to the API. In many cases they are protected by JWT tokens that some server should issue.
Acheve.TestHost implements an authentication middleware and several extension methods to easily indicate the claims for authenticated calls to the API, so it becomes very easy to test scenarios related to authentication and authorization.
Other important aspect in integration tests is maintain the URI endpoints you want to test. Usually you end with code like this:
var request = _server.CreateRequest("api/values/public"); var response = await request.GetAsync();
where you write the endpoint hardcoded in the test or in a static class that maintain all the URI's in the same class.
This has several problems:
- If any route convention is changed all integration test will fail
- If you refactor any parameter order the integration test will fail
- It is difficult to navigate to the action method that manages the request
With Acheve.TestHost you can create the URI dynamically using the attribute routing directly from your controllers.
var request = _server.CreateHttpApiRequest<ValuesController>(controller=>controller.PublicValues()); var response = await request.GetAsync();