Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new extension to produce StringContent #36

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -202,8 +203,6 @@ await client.DeleteAndEnsureNotFoundAsync("/wrongendpoint");

### [HttpResponseMessage](src\Ardalis.HttpClientTestExtensions\HttpResponseMessageExtensionMethods.cs)

```csharp

All of these methods are extensions on `HttpResponseMessage`.

```csharp
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<Summary>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.</Summary>
<RepositoryUrl>https://github.com/ardalis/HttpClientTestExtensions</RepositoryUrl>
<PackageTags>aspnet asp.net aspnetcore asp.net core api web api rest endpoint controller test integration functional xunit unit</PackageTags>
<PackageReleaseNotes>Add README to package</PackageReleaseNotes>
<PackageReleaseNotes>Add StringContent extension on HttpContent type</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>3.0.2</Version>
<Version>3.1.0</Version>
<AssemblyName>Ardalis.HttpClientTestExtensions</AssemblyName>
<PackageIcon>icon.png</PackageIcon>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -30,7 +30,7 @@

<ItemGroup>
<None Include="icon.png" Pack="true" Visible="false" PackagePath="" />
<None Include="docs\README.md" Pack="true" PackagePath="../../README.md"/>
<None Include="docs\README.md" Pack="true" PackagePath="../../README.md" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}