1- #HttpClient.Helpers
1+ # HttpClient.Helpers
22
33| Stage | CI | NuGet |
44| -------------| ----| -------|
@@ -11,6 +11,12 @@ Code that uses `System.Net.Http.HttpClient` will attempt to actually call/hit th
1111
1212To prevent this from happening in a * unit* test, some simple helpers are provided in this code library.
1313
14+ ## Key Points
15+ - Use Dependency Injection to hijack your request (this library makes it _ supa dupa easy_ to do this)
16+ - Works with GET/POST/etc.
17+ - Can provide wildcards (i.e. I don't care about the Request endpoint or the request HTTP Method, etc)
18+ - Can provide multiple endpoints and see handle what is returned based on the particular request.
19+ - Can be used to test network errors during transmission. i.e. can test when the HttpClient throws an exception because of .. well ... :boom :
1420-----
1521
1622## Installation
@@ -22,70 +28,12 @@ CLI: `install-package WorldDomination.HttpClient.Helpers`
2228
2329
2430## Sample Code
25- This sample uses the Factory and a Key. The other option is to use [ constructor injection with a ` FakeHttpMessageHandler ` parameter] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Constructor-Injection ) .
26-
27- ``` C#
28- public class MyService : IMyService
29- {
30- public const string SomeKey = " pewpew" ;
31-
32- public async Task <Foo > GetSomeDataAsync ()
33- {
34- HttpResponseMessage message ;
35- string content ;
36-
37- // ** NOTE: We use the HttpClientFactory, making it easy to unit test this code.
38- using (var httpClient = HttpClientFactory .GetHttpClient (SomeKey ))
39- {
40- message = await httpClient .GetAsync (" http://www.something.com/some/website" );
41- content = await message .Content .ReadAsStringAsync ();
42- }
43-
44- if (message .StatusCode != HttpStatusCode .OK )
45- {
46- // TODO: handle this ru-roh-error.
47- }
48-
49- // Assumption: content is in a json format.
50- var foo = JsonConvert .Deserialize <Foo >(content );
51-
52- return foo ;
53- }
54- }
55-
56- // ... and a unit test ...
57-
58- [Fact ]
59- public async Task GivenAValidHttpRequest_GetSomeDataAsync_ReturnsAFoo ()
60- {
61- // Arrange.
62- const string requestUrl = " http://www.something.com/some/website" ;
63- const string responseData = " I am not some Html." ;
64- var myService = new MyService ();
65-
66- // 1. Fake response.
67- var messageResponse = FakeHttpMessageHandler .GetStringHttpResponseMessage (responseData );
68-
69- // 2. Fake handler that says: for this Url, return this (fake) response.
70- var messageHandler = new FakeHttpMessageHandler (requestUrl , messageResponse );
71-
72- // 3. Add this message handler to the factory, for the specific key.
73- HttpClientFactory .AddMessageHandler (messageHandler , MyService .SomeKey );
74-
75- // Act.
76- // NOTE: network traffic will not leave your computer because you've faked the response, above.
77- var result = myService .GetSomeDataAsync ();
78-
79- // Assert.
80- result .Id .ShouldBe (69 );
81- }
82- ```
8331
8432There's [ plenty more examples] ( https://github.com/PureKrome/HttpClient.Helpers/wiki ) about to wire up:
85-
86- - [ Multiple endpoints] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Multiple-endpoints ) at once
87- - [ Wildcard endpoints] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Wildcard-endpoints )
88- - [ Throwing exceptions] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Faking-an-Exception ) and handling it
33+ - [ A really simple example ] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Constructor-Injection )
34+ - [ Multiple endpoints] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Multiple-endpoints ) at once
35+ - [ Wildcard endpoints] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Wildcard-endpoints )
36+ - [ Throwing exceptions] ( https://github.com/PureKrome/HttpClient.Helpers/wiki/Faking-an-Exception ) and handling it
8937
9038For all the samples, please [ check out the Wiki page: Helper Examples] ( https://github.com/PureKrome/HttpClient.Helpers/wiki )
9139
0 commit comments