I'm doing (learning about) integration testing on my web api. One of my web api methods calls out to EXTERNAL web service that takes Basic
authorization.
So, I've intercepted my external web api call via WireMock with the following code in my WebApplicationFactory<>
class:
services.AddHttpClient(
$"ExternalApiClient",
httpClient =>
{
httpClient.BaseAddress = new Uri( wireMockServer.Url );
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String( Encoding.UTF8.GetBytes( "secret" ) )
);
httpClient.DefaultRequestHeaders.Add( "Accept", "*/*" );
}
);
With this, when I test my endpoint which requests its own HttpClient
for the external service, it uses the WireMock server and serves up 'known' requests:
using var httpClient = httpClientFactory.CreateClient( "ExternalApiClient" );
So I would like to write two tests:
- Valid test where everything works.
- Invalid test where my
Basic
authentication password is wrong.
I can't think of a clean way to do this. Is there a WireMock (or integration test strategy) that would allow for this? I've written a WireMock pattern matcher to successfully do this, but I have to manually run one test or the other based on whether I've put the right password in my AddHttpClient
call (I can't run all tests within the project with my solution).