From e1381f5445fe07b273bdb94a4fe42cb19d19e787 Mon Sep 17 00:00:00 2001 From: Priyanshu-ai902 <146703385+Priyanshu-ai902@users.noreply.github.com> Date: Tue, 17 Oct 2023 19:58:09 +0530 Subject: [PATCH] Create Add mocking of HttpAsyncClient --- Add mocking of HttpAsyncClient | 94 ++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Add mocking of HttpAsyncClient diff --git a/Add mocking of HttpAsyncClient b/Add mocking of HttpAsyncClient new file mode 100644 index 0000000..2264d87 --- /dev/null +++ b/Add mocking of HttpAsyncClient @@ -0,0 +1,94 @@ +//Download something from the interwebs. + + +public async Foo GetSomethingFromTheInternetAsync() +{ + + using (var httpClient = new HttpClient()) + { + html = await httpClient.GetStringAsync("http://www.google.com.au"); + } + +} + +//Given an interface (if it existed) + +public interface IHttpClient +{ + Task GetStringAsync(string requestUri); +} + +//code will now look like this + +public class SomeService(IHttpClient httpClient = null) +{ + public async Foo GetSomethingFromTheInternetAsync() + { + .... + using (var httpClient = _httpClient ?? new HttpClient()) // <-- CHANGE dotnet/corefx#1 + { + html = await httpClient.GetStringAsync("http://www.google.com.au"); + } + .... + } +} + + +//then test class + +public async Task GivenAValidEndPoint_GetSomethingFromTheInternetAsync_ReturnsSomeData() +{ + // Create the Mock : FakeItEasy > Moq. + var httpClient = A.Fake(); + + // Define what the mock returns. + A.CallTo(()=>httpClient.GetStringAsync("http://www.google.com.au")).Returns("some html here"); + + // Inject the mock. + var service = new SomeService(httpClient); + ... +} + +//Then, now with the current way... + +create a new Handler class - yes, a class! +Inject the handler into the service +public class SomeService(IHttpClient httpClient = null) +{ + public async Foo GetSomethingFromTheInternetAsync() + { + .... + using (var httpClient = _handler == null + ? new HttpClient() + : new HttpClient(handler)) + { + html = await httpClient.GetStringAsync("http://www.google.com.au"); + } + .... + } +} +public class FakeHttpMessageHandler : HttpClientHandler +{ + ... +} + +public async Foo GetSomethingFromTheInternetAsync() +{ + string[] results; + using (var httpClient = new HttpClient()) + { + var task1 = httpClient.GetStringAsync("http://www.google.com.au"); + var task2 = httpClient.GetStringAsync("http://www.microsoft.com.au"); + + results = Task.WhenAll(task1, task2).Result; + } + .... +} + +// Now,this can be made so much easier with this.. + +var httpClient = A.Fake(); +A.CallTo(() = >httpClient.GetStringAsync("http://www.google.com.au")) + .Returns("gooz was here"); +A.CallTo(() = >httpClient.GetStringAsync("http://www.microsoft.com.au")) + .Returns("ms was here");