diff --git a/README.md b/README.md index 64ae5f5..06ec0af 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ await client.GetAndEnsureNotFoundAsync("/authors/-1"); #### [POST](src\Ardalis.HttpClientTestExtensions\HttpClientPostExtensionMethods.cs) ```csharp +// NOTE: There's a helper for this now, too (see below) var content = new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json"); // POST and return an object T @@ -202,8 +203,6 @@ await client.DeleteAndEnsureNotFoundAsync("/wrongendpoint"); ### [HttpResponseMessage](src\Ardalis.HttpClientTestExtensions\HttpResponseMessageExtensionMethods.cs) -```csharp - All of these methods are extensions on `HttpResponseMessage`. ```csharp @@ -232,6 +231,25 @@ response.Ensure(HttpStatusCode.Created); response.EnsureContainsAsync("OMG!", _testOutputHelper); ``` +### [HttpContent](src\Ardalis.HttpClientTestExtensions\HttpContentExtensionMethods.cs) + +Extensions on `HttpContent` which you'll typically want to return a `StringContent` type as you serialize your DTO to JSON. + +```csharp + +// Convert a C# DTO to a StringContent JSON type +var authorDto = new ("Steve"); +var content = HttpContent.FromModelAsJson(authorDto); + +// now you can use this with a POST, PUT, etc. +// POST and return an object T +AuthorDto result = await client.PostAndDeserializeAsync("/authors", content); + +// Or you can do it all in one line (assuming you already have the DTO) +AuthorDto result = await client.PostAndDeserializeAsync("/authors", + HttpContent.FromModelAsJson(authorDto)); +``` + ## Notes - For now this is coupled with xUnit but if there is interest it could be split so the ITestOutputHelper dependency is removed/optional/swappable diff --git a/src/Ardalis.HttpClientTestExtensions/Ardalis.HttpClientTestExtensions.csproj b/src/Ardalis.HttpClientTestExtensions/Ardalis.HttpClientTestExtensions.csproj index 4bd6e70..194e148 100644 --- a/src/Ardalis.HttpClientTestExtensions/Ardalis.HttpClientTestExtensions.csproj +++ b/src/Ardalis.HttpClientTestExtensions/Ardalis.HttpClientTestExtensions.csproj @@ -12,9 +12,9 @@ Functional/integration tests using WebApplicationFactory and HttpClient often have a lot of repetition. These extensions minimize the repetition so fetching and deserializing data from endpoints is one line of code per test. https://github.com/ardalis/HttpClientTestExtensions aspnet asp.net aspnetcore asp.net core api web api rest endpoint controller test integration functional xunit unit - Add README to package + Add StringContent extension on HttpContent type README.md - 3.0.2 + 3.1.0 Ardalis.HttpClientTestExtensions icon.png true @@ -30,7 +30,7 @@ - + diff --git a/src/Ardalis.HttpClientTestExtensions/HttpContentExtensionMethods.cs b/src/Ardalis.HttpClientTestExtensions/HttpContentExtensionMethods.cs new file mode 100644 index 0000000..64d85ec --- /dev/null +++ b/src/Ardalis.HttpClientTestExtensions/HttpContentExtensionMethods.cs @@ -0,0 +1,13 @@ +using System.Net.Http; +using System.Text; +using System.Text.Json; + +namespace Ardalis.HttpClientTestExtensions; + +public static class HttpContentExtensionMethods +{ + public static StringContent FromModelAsJson(this HttpContent content, object model) + { + return new StringContent(JsonSerializer.Serialize(model), Encoding.UTF8, "application/json")); + } +}