Nested integration testing #28200
Unanswered
benmccallum
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been trying to setup (somewhat successfully) an integration test that covers our gateway service + x number of downstream services that the gateway calls via named
HttpClient
s; by using WebApplicationFactory inception.Basically I've:
WebApplicationFactory<TGatewayStartup>
and configured it to swap out itsIHttpClientFactory
with my own implementationIHttpClientFactory
mock which is just a lookup by client name into a built-up dictionary of WebApplicationFactory-spawned HttpClient instances for each downstream service.This all works well until it come to auth, as we have a delegating handler that passes on the JWT in the header.
The problem I have though is that I need to create the HttpClient instances for the downstream services upfront, and although I can create the handler lazily off the
WebApplicationFactory<TGatewayStartup>
instance's.Services
, it's then singleton, unlike how I would've registered it in myTGatewayStartup
as transient and then configured with theIHttpClientBuilder
extension.AddHttpHandler<AuthenticationDelegatingHandler>()
.Our handler has this ctor:
I believe
IHttpContextAccessor
is registered singleton, so potentiallyAuthenticationDelegatingHandler
could be singleton too and I shouldn't worry?I guess what I'm wondering is whether there's anything at the framework level that could be introduced that would facilitate this nesting integration testing. i.e. Something that let's me "take-over/swap-out" an existing named httpclient (keeping intact its handlers but pointing it to the relevant downstream in-memory TestServer) rather than swapping out
IHttpClientFactory
entirely, which would also be better as I have a feeling in future that my IHttpClientFactory mock will need to start handling other clients that the gateway may use for other http calls.Thanks for anyone who joins in on this discussion!
Beta Was this translation helpful? Give feedback.
All reactions