Skip to content

Commit e7413d3

Browse files
authored
Update documentation (#10)
* 💄 Cleaned up the ReadMe.md. * Don't publish a -pre nuget if the repo is tagged. * 2x Unit Tests for 'Sad Panda' scenario's.
1 parent 4c504e3 commit e7413d3

File tree

3 files changed

+75
-86
lines changed

3 files changed

+75
-86
lines changed

README.md

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

1212
To 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

8432
There'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

9038
For all the samples, please [check out the Wiki page: Helper Examples](https://github.com/PureKrome/HttpClient.Helpers/wiki)
9139

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ deploy:
4343
secure: 36bcjhroAjclbHs7e7oh6Hsv4lokADI6xaQcCYZmux2Sdu/IIoktFc9ORK3DTdKo
4444
skip_symbols: true
4545
artifact: /.*\.nupkg/
46+
on:
47+
appveyor_repo_tag: false
4648
- provider: NuGet
4749
api_key:
4850
secure: jfcUvHZhgnUboplqTBDWr8mG5PIlrgBv5TA2fhhop4ZSiDxskyy+RtYyeHoduJFR

tests/HttpClient.Helpers.Tests/GetAsyncTests.cs

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Net;
34
using System.Net.Http;
45
using System.Threading.Tasks;
@@ -138,28 +139,6 @@ await DoGetAsync(RequestUri,
138139
fakeHttpMessageHandler);
139140
}
140141

141-
private static async Task DoGetAsync(string requestUri,
142-
string expectedResponseContent,
143-
FakeHttpMessageHandler fakeHttpMessageHandler)
144-
{
145-
requestUri.ShouldNotBeNullOrWhiteSpace();
146-
expectedResponseContent.ShouldNotBeNullOrWhiteSpace();
147-
fakeHttpMessageHandler.ShouldNotBeNull();
148-
149-
HttpResponseMessage message;
150-
string content;
151-
using (var httpClient = new System.Net.Http.HttpClient(fakeHttpMessageHandler))
152-
{
153-
// Act.
154-
message = await httpClient.GetAsync(requestUri);
155-
content = await message.Content.ReadAsStringAsync();
156-
}
157-
158-
// Assert.
159-
message.StatusCode.ShouldBe(HttpStatusCode.OK);
160-
content.ShouldBe(expectedResponseContent);
161-
}
162-
163142
[Fact]
164143
public async Task GivenAnHttpResponseMessage_GetAsync_ReturnsAFakeResponse()
165144
{
@@ -199,5 +178,65 @@ await DoGetAsync(RequestUri,
199178
ExpectedContent,
200179
fakeHttpMessageHandler);
201180
}
181+
182+
[Fact]
183+
public async Task GivenAnUnauthorisedStatusCodeResponse_GetAsync_ReturnsAFakeResponseWithAnUnauthorisedStatusCode()
184+
{
185+
// Arrange.
186+
var messageResponse = FakeHttpMessageHandler.GetStringHttpResponseMessage("pew pew", HttpStatusCode.Unauthorized);
187+
var messageHandler = new FakeHttpMessageHandler(RequestUri, messageResponse);
188+
189+
HttpResponseMessage message;
190+
using (var httpClient = new System.Net.Http.HttpClient(messageHandler))
191+
{
192+
// Act.
193+
message = await httpClient.GetAsync(RequestUri);
194+
}
195+
196+
// Assert.
197+
message.StatusCode.ShouldBe(HttpStatusCode.Unauthorized);
198+
}
199+
200+
[Fact]
201+
public async Task GivenAValidHttpRequest_GetSomeDataAsync_ReturnsAFoo()
202+
{
203+
// Arrange.
204+
const string errorMessage = "Oh man - something bad happened.";
205+
var expectedException = new HttpRequestException(errorMessage);
206+
var messageHandler = new FakeHttpMessageHandler(expectedException);
207+
208+
Exception exception;
209+
using (var httpClient = new System.Net.Http.HttpClient(messageHandler))
210+
{
211+
// Act.
212+
// NOTE: network traffic will not leave your computer because you've faked the response, above.
213+
exception = await Should.ThrowAsync<HttpRequestException>(async () => await httpClient.GetAsync(RequestUri));
214+
}
215+
216+
// Assert.
217+
exception.Message.ShouldBe(errorMessage);
218+
}
219+
220+
private static async Task DoGetAsync(string requestUri,
221+
string expectedResponseContent,
222+
FakeHttpMessageHandler fakeHttpMessageHandler)
223+
{
224+
requestUri.ShouldNotBeNullOrWhiteSpace();
225+
expectedResponseContent.ShouldNotBeNullOrWhiteSpace();
226+
fakeHttpMessageHandler.ShouldNotBeNull();
227+
228+
HttpResponseMessage message;
229+
string content;
230+
using (var httpClient = new System.Net.Http.HttpClient(fakeHttpMessageHandler))
231+
{
232+
// Act.
233+
message = await httpClient.GetAsync(requestUri);
234+
content = await message.Content.ReadAsStringAsync();
235+
}
236+
237+
// Assert.
238+
message.StatusCode.ShouldBe(HttpStatusCode.OK);
239+
content.ShouldBe(expectedResponseContent);
240+
}
202241
}
203242
}

0 commit comments

Comments
 (0)