Instead of doing this, we have an option to provide the ability to add pre and post invocation actions. We could provide an InvocationCount property, but this means we are trying to predict the different ways the API will be used. Suppose we want to verify how many times we have called HttpClient, we need some mechanism to perform this. In some instances, we may want to perform additional testing that is hard to predict. } Extending the Calls With Your Own Tests New FakeHttpMessageHandler().WithVersion( new Version( 2, 1)) Īwait httpClient.PostWrapperAsync( " M圜ontent") Īssert.Equal(response.Version, new Version( 2, 1)) WhenPostIsCalled_ThenCustomVersionIsSet() Public async Task GivenValidRequestWithCustomVersion_ If we need to set version numbers for our REST calls, we have WithVersion to provide version information. Use WithTrailingResponseHeader to add the trailing headers. This feature is only available in targets that support. In the same way, we can set a header in a response, NetStandard2.1 gives us the ability to add trailing headers in a response. We can see in this sample that we are explicitly setting the status code (okay, while it's the default, I wanted to demonstrate adding multiple parts in one go) and the response header that we expect to trigger our response.Īs well as being able to set a single value for a header, there is an override that accepts an array of values. IEnumerable output = await exampleController.GetAll() New ExampleControllerHandling(httpClient) HttpClient httpClient = new HttpClient(fake) ĮxampleControllerHandling exampleController = WithResponseHeader( " order66", " babyyoda").WithExpectedContent(sample) Public async Task GivenMultipleInputsIntoController_WhenProcessing_ThenModelIsReturned()įakeHttpMessageHandler fake = new FakeHttpMessageHandler().WithStatusCode NET Standard 2.1 package on NuGet, and can be installed using: Goldlight.HttpClientTestSupport is available as a. Installing Goldlight.HttpClientTestSupport Rather than making a mockable object, we are going to provide a FakeHttpMessageHandler that allows us to control what the response we receive back contains. While Moq provides the ability to mock protected properties, if we are using a framework like FakeItEasy, then we can't directly interact with the protected method.Īll of this detective work has told us that, in order to replace our calls, we need to inherit from HttpMessageHandler and provide our own implementation that satisfies the ability to simulate web requests. Faking the Message HandlerĪn issue that we have to cope with, with regards to testing the message handler is the problem that the virtual SendAsync method is protected and most mocking frameworks are unable to mock virtual methods. This is important to because SendAsync calls out to the HttpMessageHandler.SendAsync method and this is the "touch point" that we want to interact with to mock our REST call. The HttpMessageInvoker class requires posting an HttpMessageHandler instance in it via a constructor. HttpClient inherits from a class called HttpMessageInvoker which has SendAsync as a virtual method inside it. Return SendAsync( new HttpRequestMessage(HttpMethod.Get, requestUri), (Uri requestUri, HttpCompletionOption completionOption,
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |