So, I'm doing a project that needs to consume a REST service. I'm using C#, and I'm writing implementation myself with
HttpClient class. I also try to create some Unit Testing for my library, but that class doesn't make it easy. Here are parts of the conflict:
HttpClientis designed to be used as singleton, as indicated by, for example, this article.
- Testing interaction with a service should swap the dependecy on real service for a fake, since unit-test should be as independent as possible.
- HttpClient doesn't implement any interface, so you can't make your own implementation, instead you seem to be supposed to implement
HttpMessageHandlerto swap requests like here
HttpClientis initialized with
HttpMessageHandlerand the used handler can't be changed at runtime.
I now struggle to implement the library with all that in mind. Right now I have Handler as a paramater in singleton GetInstance, however the parameter is only used if it's first call and ignored all other times, which feels like bad design and unexpected behaviour. Please advise.