Skip to content

Commit 150573d

Browse files
authored
Release v0.9 (#189)
* Make HttpRequestMessageExtensions internal and remove HttpResponseMessageExtensions * Prepare for 0.9 release * Fix PublicAPI files
1 parent 8b35d48 commit 150573d

17 files changed

+59
-854
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ All notable changes to TestableHttpClient will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and
55
this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [0.9] - unplanned
7+
## [0.9] - 2022-11-25
88
### Deprecated
99
- `Responses.NoContent()` has been deprecated, since it doesn't fit well with the rest of the API. Please use `Responses.StatusCode(HttpStatusCode.NoContent)` instead.
1010

1111
### Removed
1212
- Official support for .NET Core 3.1 has been removed. This means we no longer provide a specific version for .NET Core 3.0 and we no longer test this version explicitly. Since we support .NET Standard 2.0, the library could still be used.
1313
- TestableHttpClient.NFluent has been moved to it's own repository.
14+
- `HttpRequestMessageExtensions` have been made internal.
15+
- `HttpResponseMessageExtensions` have been removed, since it not needed for making HttpClients testable.
1416

1517
### Added
1618
- Added `Responses.Route` that allows changing the response based on the url. The url supports patterns.
@@ -243,6 +245,7 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
243245
- Automatically build project when pushing changes to github and when creating a pull request
244246
- Automatically deploy to NuGet when creating a tag in github
245247

248+
[0.9]: https://github.com/testablehttpclient/TestableHttpClient/compare/v0.8...v0.9
246249
[0.8]: https://github.com/testablehttpclient/TestableHttpClient/compare/v0.7...v0.8
247250
[0.7]: https://github.com/testablehttpclient/TestableHttpClient/compare/v0.6...v0.7
248251
[0.6]: https://github.com/testablehttpclient/TestableHttpClient/compare/v0.5...v0.6

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# TestableHttpClient
22

3-
![GitHub](https://img.shields.io/github/license/testablehttpclient/TestableHttpClient) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/testablehttpclient/TestableHttpClient/CI)
3+
![GitHub](https://img.shields.io/github/license/testablehttpclient/TestableHttpClient) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/testablehttpclient/TestableHttpClient/CI) ![Nuget](https://img.shields.io/nuget/v/TestableHttpClient)
44

55
Creating unittest for code that uses `HttpClient` can be difficult to test. It requires a custom HttpMessageHandler or a mocked version. TestableHttpClient provides a testable version of HttpMessageHandler and several helper functions to configure the `TestableHttpHandler` and several ways to assert which requests were made.
66

@@ -13,9 +13,21 @@ dotnet add package TestableHttpClient
1313

1414
## How to use TestableHttpClient
1515

16+
The following code block shows the basic use case for asserting that certain requests are made:
1617
```csharp
17-
var testHandler = new TestableHttpMessageHandler();
18-
var httpClient = new HttpClient(testHandler); // or testHandler.CreateClient();
18+
TestableHttpMessageHandler testHandler = new();
19+
HttpClient httpClient = new(testHandler); // or testHandler.CreateClient();
20+
21+
var result = await httpClient.GetAsync("http://httpbin.org/status/200");
22+
23+
testHandler.ShouldHaveMadeRequestsTo("https://httpbin.org/*");
24+
```
25+
26+
The default response is an empty response message with a 200 OK StatusCode, in order to return real content the response need to be configured:
27+
```csharp
28+
TestableHttpMessageHandler testHandler = new();
29+
testHandler.RespondWith(Responses.Json(new { Hello: "World" }));
30+
HttpClient httpClient = new(testHandler); // or testHandler.CreateClient();
1931
2032
var result = await httpClient.GetAsync("http://httpbin.org/status/200");
2133

@@ -29,7 +41,7 @@ More examples can be found in the [IntegrationTests project](test/TestableHttpCl
2941
TestableHttpClient is build as a netstandard2.0 library, so theoretically it can work on every .NET version that support netstandard2.0.
3042
The following versions are being actively tested and thus supported:
3143

32-
- .NET Framework 4.6 and up
44+
- .NET Framework 4.6, 4.7 and 4.8
3345
- .NET 6.0
3446
- .NET 7.0
3547

src/TestableHttpClient/HttpRequestMessageExtensions.cs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
/// <summary>
44
/// A set of static methods for checking values on a <see cref="HttpRequestMessage"/>.
55
/// </summary>
6-
[Obsolete("This class is not intended for public use and will be marked internal in the next release.")]
7-
public static class HttpRequestMessageExtensions
6+
internal static class HttpRequestMessageExtensions
87
{
98
/// <summary>
109
/// Determines whether a specific HttpVersion is set on a request.
1110
/// </summary>
1211
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct version on.</param>
1312
/// <param name="httpVersion">The expected version.</param>
1413
/// <returns>true when the HttpVersion matches; otherwise, false.</returns>
15-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
16-
public static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, Version httpVersion)
14+
internal static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, Version httpVersion)
1715
{
1816
if (httpRequestMessage == null)
1917
{
@@ -34,8 +32,7 @@ public static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, Ve
3432
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct version on.</param>
3533
/// <param name="httpVersion">The expected version.</param>
3634
/// <returns>true when the HttpVersion matches; otherwise, false.</returns>
37-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
38-
public static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, string httpVersion)
35+
internal static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, string httpVersion)
3936
{
4037
if (httpRequestMessage == null)
4138
{
@@ -56,8 +53,7 @@ public static bool HasHttpVersion(this HttpRequestMessage httpRequestMessage, st
5653
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
5754
/// <param name="httpMethod">The expected method.</param>
5855
/// <returns>true when the HttpMethod matches; otherwise, false.</returns>
59-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
60-
public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, HttpMethod httpMethod)
56+
internal static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, HttpMethod httpMethod)
6157
{
6258
if (httpRequestMessage == null)
6359
{
@@ -78,8 +74,7 @@ public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, Htt
7874
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct method on.</param>
7975
/// <param name="httpMethod">The expected method.</param>
8076
/// <returns>true when the HttpMethod matches; otherwise, false.</returns>
81-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
82-
public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, string httpMethod)
77+
internal static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, string httpMethod)
8378
{
8479
if (httpRequestMessage == null)
8580
{
@@ -101,8 +96,7 @@ public static bool HasHttpMethod(this HttpRequestMessage httpRequestMessage, str
10196
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the request header on.</param>
10297
/// <param name="headerName">The name of the header to locate on the request.</param>
10398
/// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
104-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
105-
public static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage, string headerName)
99+
internal static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage, string headerName)
106100
{
107101
if (httpRequestMessage == null)
108102
{
@@ -125,8 +119,7 @@ public static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage,
125119
/// <param name="headerName">The name of the header to locate on the request.</param>
126120
/// <param name="headerValue">The value the header should have. Wildcard is supported.</param>
127121
/// <returns>true when the request contains a header with the specified name and value; otherwise, false.</returns>
128-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
129-
public static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
122+
internal static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
130123
{
131124
if (httpRequestMessage == null)
132125
{
@@ -153,8 +146,7 @@ public static bool HasRequestHeader(this HttpRequestMessage httpRequestMessage,
153146
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the content header on.</param>
154147
/// <param name="headerName">The name of the header to locate on the request content.</param>
155148
/// <returns>true when the request contains a header with the specified name; otherwise, false.</returns>
156-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
157-
public static bool HasContentHeader(this HttpRequestMessage httpRequestMessage, string headerName)
149+
internal static bool HasContentHeader(this HttpRequestMessage httpRequestMessage, string headerName)
158150
{
159151
if (httpRequestMessage == null)
160152
{
@@ -182,8 +174,7 @@ public static bool HasContentHeader(this HttpRequestMessage httpRequestMessage,
182174
/// <param name="headerName">The name of the header to locate on the request content.</param>
183175
/// <param name="headerValue">The value the header should have. Wildcard is supported.</param>
184176
/// <returns>true when the request contains a header with the specified name and value; otherwise, false.</returns>
185-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
186-
public static bool HasContentHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
177+
internal static bool HasContentHeader(this HttpRequestMessage httpRequestMessage, string headerName, string headerValue)
187178
{
188179
if (httpRequestMessage == null)
189180
{
@@ -214,8 +205,7 @@ public static bool HasContentHeader(this HttpRequestMessage httpRequestMessage,
214205
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct uri on.</param>
215206
/// <param name="pattern">A pattern to match with the request uri, supports * as wildcards.</param>
216207
/// <returns>true when the request uri matches the pattern; otherwise, false.</returns>
217-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
218-
public static bool HasMatchingUri(this HttpRequestMessage httpRequestMessage, string pattern, bool ignoreCase = true)
208+
internal static bool HasMatchingUri(this HttpRequestMessage httpRequestMessage, string pattern, bool ignoreCase = true)
219209
{
220210
if (httpRequestMessage == null)
221211
{
@@ -241,8 +231,7 @@ public static bool HasMatchingUri(this HttpRequestMessage httpRequestMessage, st
241231
/// </summary>
242232
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check for content.</param>
243233
/// <returns>true when the request has content; otherwise, false.</returns>
244-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
245-
public static bool HasContent(this HttpRequestMessage httpRequestMessage)
234+
internal static bool HasContent(this HttpRequestMessage httpRequestMessage)
246235
{
247236
if (httpRequestMessage == null)
248237
{
@@ -258,8 +247,7 @@ public static bool HasContent(this HttpRequestMessage httpRequestMessage)
258247
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct content on.</param>
259248
/// <param name="pattern">A pattern to match the request content, supports * as wildcards.</param>
260249
/// <returns>true when the request content matches the pattern; otherwise, false.</returns>
261-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
262-
public static bool HasContent(this HttpRequestMessage httpRequestMessage, string pattern)
250+
internal static bool HasContent(this HttpRequestMessage httpRequestMessage, string pattern)
263251
{
264252
if (httpRequestMessage == null)
265253
{
@@ -292,8 +280,7 @@ public static bool HasContent(this HttpRequestMessage httpRequestMessage, string
292280
/// <param name="httpRequestMessage">A <see cref="HttpRequestMessage"/> to check the correct request uir querystring on.</param>
293281
/// <param name="pattern">A pattern to match the request uri querystring, supports * as wildcards.</param>
294282
/// <returns>true when the request uri querystring matches the pattern; otherwise, false.</returns>
295-
[Obsolete("This method is not intended for public use and will be marked internal in the next release.")]
296-
public static bool HasQueryString(this HttpRequestMessage httpRequestMessage, string pattern)
283+
internal static bool HasQueryString(this HttpRequestMessage httpRequestMessage, string pattern)
297284
{
298285
if (httpRequestMessage == null)
299286
{

src/TestableHttpClient/HttpRequestMessagesCheckExtensions.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ private static IHttpRequestMessagesCheck WithRequestUri(this IHttpRequestMessage
4242
condition = $"uri pattern '{pattern}'";
4343
}
4444

45-
#pragma warning disable CS0618 // Type or member is obsolete
4645
return check.WithFilter(x => x.HasMatchingUri(pattern, ignoreCase), expectedNumberOfRequests, condition);
47-
#pragma warning restore CS0618 // Type or member is obsolete
4846
}
4947

5048
/// <summary>
@@ -83,9 +81,7 @@ private static IHttpRequestMessagesCheck WithQueryString(this IHttpRequestMessag
8381
_ => $"querystring pattern '{pattern}'"
8482
};
8583

86-
#pragma warning disable CS0618 // Type or member is obsolete
8784
return check.WithFilter(x => x.HasQueryString(pattern), expectedNumberOfRequests, condition);
88-
#pragma warning restore CS0618 // Type or member is obsolete
8985
}
9086

9187
/// <summary>
@@ -117,9 +113,7 @@ private static IHttpRequestMessagesCheck WithHttpMethod(this IHttpRequestMessage
117113
throw new ArgumentNullException(nameof(httpMethod));
118114
}
119115

120-
#pragma warning disable CS0618 // Type or member is obsolete
121116
return check.WithFilter(x => x.HasHttpMethod(httpMethod), expectedNumberOfRequests, $"HTTP Method '{httpMethod}'");
122-
#pragma warning restore CS0618 // Type or member is obsolete
123117
}
124118

125119
/// <summary>
@@ -151,9 +145,7 @@ private static IHttpRequestMessagesCheck WithHttpVersion(this IHttpRequestMessag
151145
throw new ArgumentNullException(nameof(httpVersion));
152146
}
153147

154-
#pragma warning disable CS0618 // Type or member is obsolete
155148
return check.WithFilter(x => x.HasHttpVersion(httpVersion), expectedNumberOfRequests, $"HTTP Version '{httpVersion}'");
156-
#pragma warning restore CS0618 // Type or member is obsolete
157149
}
158150

159151
/// <summary>
@@ -187,9 +179,7 @@ private static IHttpRequestMessagesCheck WithRequestHeader(this IHttpRequestMess
187179
throw new ArgumentNullException(nameof(headerName));
188180
}
189181

190-
#pragma warning disable CS0618 // Type or member is obsolete
191182
return check.WithFilter(x => x.HasRequestHeader(headerName), expectedNumberOfRequests, $"request header '{headerName}'");
192-
#pragma warning restore CS0618 // Type or member is obsolete
193183
}
194184

195185
/// <summary>
@@ -230,9 +220,7 @@ private static IHttpRequestMessagesCheck WithRequestHeader(this IHttpRequestMess
230220
throw new ArgumentNullException(nameof(headerValue));
231221
}
232222

233-
#pragma warning disable CS0618 // Type or member is obsolete
234223
return check.WithFilter(x => x.HasRequestHeader(headerName, headerValue), expectedNumberOfRequests, $"request header '{headerName}' and value '{headerValue}'");
235-
#pragma warning restore CS0618 // Type or member is obsolete
236224
}
237225

238226
/// <summary>
@@ -266,9 +254,7 @@ private static IHttpRequestMessagesCheck WithContentHeader(this IHttpRequestMess
266254
throw new ArgumentNullException(nameof(headerName));
267255
}
268256

269-
#pragma warning disable CS0618 // Type or member is obsolete
270257
return check.WithFilter(x => x.HasContentHeader(headerName), expectedNumberOfRequests, $"content header '{headerName}'");
271-
#pragma warning restore CS0618 // Type or member is obsolete
272258
}
273259

274260
/// <summary>
@@ -309,9 +295,7 @@ private static IHttpRequestMessagesCheck WithContentHeader(this IHttpRequestMess
309295
throw new ArgumentNullException(nameof(headerValue));
310296
}
311297

312-
#pragma warning disable CS0618 // Type or member is obsolete
313298
return check.WithFilter(x => x.HasContentHeader(headerName, headerValue), expectedNumberOfRequests, $"content header '{headerName}' and value '{headerValue}'");
314-
#pragma warning restore CS0618 // Type or member is obsolete
315299
}
316300

317301
/// <summary>
@@ -343,9 +327,7 @@ private static IHttpRequestMessagesCheck WithHeader(this IHttpRequestMessagesChe
343327
throw new ArgumentNullException(nameof(headerName));
344328
}
345329

346-
#pragma warning disable CS0618 // Type or member is obsolete
347330
return check.WithFilter(x => x.HasRequestHeader(headerName) || x.HasContentHeader(headerName), expectedNumberOfRequests, $"header '{headerName}'");
348-
#pragma warning restore CS0618 // Type or member is obsolete
349331
}
350332

351333
/// <summary>
@@ -384,9 +366,7 @@ private static IHttpRequestMessagesCheck WithHeader(this IHttpRequestMessagesChe
384366
throw new ArgumentNullException(nameof(headerValue));
385367
}
386368

387-
#pragma warning disable CS0618 // Type or member is obsolete
388369
return check.WithFilter(x => x.HasRequestHeader(headerName, headerValue) || x.HasContentHeader(headerName, headerValue), expectedNumberOfRequests, $"header '{headerName}' and value '{headerValue}'");
389-
#pragma warning restore CS0618 // Type or member is obsolete
390370
}
391371

392372
/// <summary>
@@ -420,9 +400,7 @@ private static IHttpRequestMessagesCheck WithContent(this IHttpRequestMessagesCh
420400
throw new ArgumentNullException(nameof(pattern));
421401
}
422402

423-
#pragma warning disable CS0618 // Type or member is obsolete
424403
return check.WithFilter(x => x.HasContent(pattern), expectedNumberOfRequests, $"content '{pattern}'");
425-
#pragma warning restore CS0618 // Type or member is obsolete
426404
}
427405

428406
/// <summary>
@@ -472,9 +450,7 @@ private static IHttpRequestMessagesCheck WithJsonContent(this IHttpRequestMessag
472450

473451
var jsonString = JsonSerializer.Serialize(jsonObject, jsonSerializerOptions ?? check.Options.JsonSerializerOptions);
474452

475-
#pragma warning disable CS0618 // Type or member is obsolete
476453
return check.WithFilter(x => x.HasContent(jsonString) && x.HasContentHeader("Content-Type", "application/json*"), expectedNumberOfRequests, $"json content '{jsonString}'");
477-
#pragma warning restore CS0618 // Type or member is obsolete
478454
}
479455

480456
/// <summary>
@@ -511,8 +487,6 @@ private static IHttpRequestMessagesCheck WithFormUrlEncodedContent(this IHttpReq
511487
using var content = new FormUrlEncodedContent(nameValueCollection);
512488
var contentString = content.ReadAsStringAsync().Result;
513489

514-
#pragma warning disable CS0618 // Type or member is obsolete
515490
return check.WithFilter(x => x.HasContent(contentString) && x.HasContentHeader("Content-Type", "application/x-www-form-urlencoded*"), expectedNumberOfRequests, $"form url encoded content '{contentString}'");
516-
#pragma warning restore CS0618 // Type or member is obsolete
517491
}
518492
}

0 commit comments

Comments
 (0)