HttpClientHandler design clashing with Tests

121 Views Asked by At

I have Integration Tests failing when run at once, but pass if run separately.

The tests are for Service 1 which initializes dependent Service 2. Service 2 receives HttpClientHandler through DI where it sets default behavior (AllowAutoRedirect property).

Due to a test scenario I have to use HttpClientHandler to create the HttpClient in Service 2. (To be specific the call through HttpClientHandler is intercepted in the Integration tests where response content is changed. There is no way to do this through HttpWebRequest and HttpClientFactory. Hence the default behaviour for HttpClientHandler is in Service 2's constructor).

Why am I getting this error when tests are run at once? What is happening twice? The tests work fine separately which is honestly baffling.

    System.InvalidOperationException : This instance has already started one or more requests. Properties can only be modified before sending the first request.
  Stack Trace: 
    SocketsHttpHandler.CheckDisposedOrStarted()
    HttpClientHandler.set_AllowAutoRedirect(Boolean value)

Have I made a design sin somewhere?

0

There are 0 best solutions below