pk-development - WorldDomination.HttpClient.Helpers 44.0.0-dev
Some simple System.Net.Http.HttpClient helpers to help with your unit tests or when you don't really want to call the endpoint.
PM> Install-Package WorldDomination.HttpClient.Helpers -Version 44.0.0-dev -Source https://www.myget.org/F/pk-development/api/v3/index.json
> nuget.exe install WorldDomination.HttpClient.Helpers -Version 44.0.0-dev -Source https://www.myget.org/F/pk-development/api/v3/index.json
> dotnet add package WorldDomination.HttpClient.Helpers --version 44.0.0-dev --source https://www.myget.org/F/pk-development/api/v3/index.json
<PackageReference Include="WorldDomination.HttpClient.Helpers" Version="44.0.0-dev" />
Copy to clipboard
source https://www.myget.org/F/pk-development/api/v3/index.json
nuget WorldDomination.HttpClient.Helpers ~> 44.0.0-dev
Copy to clipboard
> choco install WorldDomination.HttpClient.Helpers --version 44.0.0-dev --source https://www.myget.org/F/pk-development/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "pk-development" -SourceLocation "https://www.myget.org/F/pk-development/api/v2"
Install-Module -Name "WorldDomination.HttpClient.Helpers" -RequiredVersion "44.0.0-dev" -Repository "pk-development" -AllowPreRelease
Copy to clipboard
HttpClient.Helpers
| Stage | CI | NuGet |
|---|---|---|
| Production | ||
| Development |
Code that uses System.Net.Http.HttpClient will attempt to actually call/hit that Http endpoint.
To prevent this from happening in a unit test, some simple helpers are provided in this code library.
Key Points
- :white_check_mark: Hijack your
httpClientrequest to return some hardcoded response (this library makes it supa dupa easy to do this) - :white_check_mark: Works with GET/POST/PUT/etc.
- :white_check_mark: Can provide wildcards (i.e. I don't care about the Request endpoint or the request HTTP Method, etc)
- :white_check_mark: Can provide multiple endpoints and see handle what is returned based on the particular request.
- :white_check_mark: Can confirm the number of times an endpoint was attempted to be hit.
- :white_check_mark: Can be used to test network errors during transmission. i.e. can test when the HttpClient throws an exception because of .. well ... :boom:
Installation
Package Name: WorldDomination.HttpClient.Helpers
CLI: install-package WorldDomination.HttpClient.Helpers
TL;DR; Show me some code that leverages HttpClientFactory
What is HttpClientFactory? Read up about it here. You should be using that in your applications, peeps.
// Service that accepts an HttpClient so it has been setup to work nicely with HttpClientFactory.
public class MyService : IMyService
{
public MyService(HttpClient httpClient)
{ .. }
public async Task<string> GetWebApiStuffAsync() { .. }
}
// ** Now for a sample test **
// Act.
// Setup which http request 'stuff' you wish to capture and return.
// E.g. If we hit "this url" then return "this payload" and "this response type. 200 OK, etc".
var options = new HttpMessageOptions { ....};
// Create the fake handler with the specific options to check for.
var messageHandler = new FakeHttpMessageHandler(options);
// Now create a simple HttpClient which will use the fake handler to
// cut short / capture the request.
var httpClient = new HttpClient(messageHandler);
var service = new MyService(httpClient);
// Act.
await service.GetWebApiStuffAsync();
Sample Code
There's plenty more examples about to wire up:
- A really simple example
- Multiple endpoints at once
- Wildcard endpoints
- Throwing exceptions and handling it
For all the samples, please check out the Wiki page: Helper Examples
Special Ackowledgements
A special and sincere thank you to David Fowler (@davidfowl) who explained how I should be unit testing the HttpClient class and gave me the guidence to make this library. I was soooo on the wrong path - and he guided me back on track.
Thank you David! :ok_hand: :cocktail: :space_invader:
Summary
Finally, unit testing HttpClient is now awesome and simple!

- .NETFramework 4.5: 4.5.0.0
OwnersPure Krome |
AuthorsJustin Adler |
Project URLhttps://github.com/PureKrome/HttpClient.Helpers |
LicenseMIT |
Tagshttpclient worlddomination worldomination unicorn magicalunicorn magical-unicorn |
Info3240 total downloads |
| 80 downloads for version 44.0.0-dev |
| Download (6.73 KB) |
| Found on the current feed only |
Package history
| Version | Size | Last updated | Downloads | Mirrored? | |||
|---|---|---|---|---|---|---|---|
|
107.0.0-dev | 240.91 KB | Mon, 04 Oct 2021 10:03:31 GMT | 77 |
|
||
|
104.0.0-dev | 240.95 KB | Fri, 19 Feb 2021 01:54:39 GMT | 82 |
|
||
|
102.0.0-dev | 240.99 KB | Fri, 19 Feb 2021 01:09:01 GMT | 67 |
|
||
|
99.0.0-dev | 240.99 KB | Fri, 19 Feb 2021 00:39:58 GMT | 78 |
|
||
|
98.0.0-dev | 240.98 KB | Fri, 19 Feb 2021 00:34:18 GMT | 79 |
|
||
|
96.0.0-dev | 235.25 KB | Mon, 19 Oct 2020 14:06:35 GMT | 76 |
|
||
|
95.0.0-dev | 235.25 KB | Mon, 19 Oct 2020 13:49:23 GMT | 75 |
|
||
|
93.0.0-dev | 235.25 KB | Mon, 19 Oct 2020 13:37:54 GMT | 71 |
|
||
|
92.0.0-dev | 235.25 KB | Mon, 19 Oct 2020 13:32:22 GMT | 83 |
|
||
|
81.0.0-dev | 7.85 KB | Tue, 28 Jul 2020 01:33:22 GMT | 67 |
|
||
|
80.0.0-dev | 7.85 KB | Tue, 28 Jul 2020 01:32:12 GMT | 82 |
|
||
|
79.0.0-dev | 8.08 KB | Thu, 09 Jan 2020 13:33:31 GMT | 80 |
|
||
|
77.0.0-dev | 8.09 KB | Mon, 08 Jul 2019 02:27:31 GMT | 65 |
|
||
|
76.0.0-dev | 8.08 KB | Mon, 08 Jul 2019 02:24:15 GMT | 79 |
|
||
|
75.0.0-dev | 8.09 KB | Mon, 08 Jul 2019 02:16:10 GMT | 78 |
|
||
|
74.0.0-dev | 8.08 KB | Mon, 08 Jul 2019 02:14:26 GMT | 82 |
|
||
|
72.0.0-dev | 7.94 KB | Tue, 28 May 2019 02:56:54 GMT | 72 |
|
||
|
71.0.0-dev | 7.94 KB | Tue, 28 May 2019 02:54:02 GMT | 70 |
|
||
|
70.0.0-dev | 7.94 KB | Tue, 28 May 2019 02:52:16 GMT | 79 |
|
||
|
69.0.0-dev | 19.92 KB | Thu, 01 Feb 2018 04:48:52 GMT | 91 |
|
||
|
67.0.0-dev | 8.12 KB | Wed, 07 Jun 2017 07:06:07 GMT | 80 |
|
||
|
66.0.0-dev | 8.12 KB | Wed, 07 Jun 2017 06:58:43 GMT | 69 |
|
||
|
65.0.0-dev | 8.12 KB | Wed, 07 Jun 2017 06:35:29 GMT | 77 |
|
||
|
63.0.0-dev | 8.07 KB | Sat, 27 May 2017 13:22:14 GMT | 85 |
|
||
|
62.0.0-dev | 8.08 KB | Wed, 26 Apr 2017 04:32:08 GMT | 80 |
|
||
|
61.0.0-dev | 8.07 KB | Wed, 26 Apr 2017 04:10:22 GMT | 64 |
|
||
|
60.0.0-dev | 8.13 KB | Wed, 12 Apr 2017 02:43:13 GMT | 82 |
|
||
|
57.0.0-dev | 8.02 KB | Fri, 31 Mar 2017 03:57:29 GMT | 71 |
|
||
|
56.0.0-dev | 8.02 KB | Fri, 31 Mar 2017 03:55:28 GMT | 72 |
|
||
|
55.0.0-dev | 8.02 KB | Fri, 31 Mar 2017 03:53:17 GMT | 79 |
|
||
|
54.0.0-dev | 7.93 KB | Fri, 31 Mar 2017 03:08:54 GMT | 70 |
|
||
|
53.0.0-dev | 7.93 KB | Fri, 31 Mar 2017 03:03:28 GMT | 75 |
|
||
|
52.0.0-dev | 7.87 KB | Fri, 31 Mar 2017 00:19:19 GMT | 76 |
|
||
|
51.0.0-dev | 7.87 KB | Thu, 30 Mar 2017 23:57:50 GMT | 85 |
|
||
|
49.0.0-dev | 7.71 KB | Wed, 15 Feb 2017 01:52:01 GMT | 84 |
|
||
|
48.0.0-dev | 7.71 KB | Sun, 05 Feb 2017 23:21:20 GMT | 79 |
|
||
|
47.0.0-dev | 7.67 KB | Sun, 05 Feb 2017 22:44:56 GMT | 93 |
|
||
|
46.0.0-dev | 6.73 KB | Fri, 23 Dec 2016 00:33:40 GMT | 78 |
|
||
|
45.0.0-dev | 6.73 KB | Fri, 23 Dec 2016 00:28:45 GMT | 78 |
|
||
|
44.0.0-dev | 6.73 KB | Fri, 23 Dec 2016 00:27:07 GMT | 80 |
|
||
|
43.0.0-dev | 6.73 KB | Wed, 23 Nov 2016 01:37:15 GMT | 76 |
|
||
|
4.0.0 | 6.73 KB | Thu, 10 Nov 2016 02:24:11 GMT | 74 |
|
