Skip to content
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
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class MyService
}
}
```
<sup><a href='/src/Tests/Tests.cs#L36-L56' title='Snippet source file'>snippet source</a> | <a href='#snippet-servicethatdoeshttp' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L37-L57' title='Snippet source file'>snippet source</a> | <a href='#snippet-servicethatdoeshttp' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -85,7 +85,7 @@ await Verify(recording.Sends)
// Ignore some headers that change per request
.ModifySerialization(x => x.IgnoreMembers("Date"));
```
<sup><a href='/src/Tests/Tests.cs#L148-L167' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientrecording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L195-L214' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientrecording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -114,7 +114,7 @@ await Verify(recording.Sends)
// Ignore some headers that change per request
.ModifySerialization(x => x.IgnoreMembers("Date"));
```
<sup><a href='/src/Tests/Tests.cs#L124-L142' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientrecordingglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L171-L189' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientrecordingglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -170,7 +170,7 @@ await myService.MethodThatDoesHttp();
await Verify(recording.Sends)
.ModifySerialization(x => x.IgnoreMembers("Date"));
```
<sup><a href='/src/Tests/Tests.cs#L225-L249' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientpauseresume' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L272-L296' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientpauseresume' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

If the `AddRecordingHttpClient` helper method does not meet requirements, the `RecordingHandler` can be explicitly added:
Expand Down Expand Up @@ -201,7 +201,7 @@ await client.GetAsync("https://httpbin.org/status/undefined");
await Verify(recording.Sends)
.ModifySerialization(x => x.IgnoreMembers("Date"));
```
<sup><a href='/src/Tests/Tests.cs#L255-L280' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientrecordingexplicit' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L302-L327' title='Snippet source file'>snippet source</a> | <a href='#snippet-httpclientrecordingexplicit' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -249,7 +249,7 @@ static async Task<int> MethodThatDoesHttpCalls()
return jsonResult.Length + xmlResult.Length;
}
```
<sup><a href='/src/Tests/Tests.cs#L60-L90' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L107-L137' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The requests/response pairs will be appended to the verified file.
Expand Down Expand Up @@ -372,7 +372,7 @@ public async Task TestHttpRecordingExplicit()
});
}
```
<sup><a href='/src/Tests/Tests.cs#L92-L119' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecordingexplicit' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L139-L166' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecordingexplicit' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in the following:
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;CS8632</NoWarn>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Http, Verify</PackageTags>
<Description>Extends Verify (https://github.com/VerifyTests/Verify) to allow verification of Http bits.</Description>
Expand Down
49 changes: 48 additions & 1 deletion src/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using VerifyTests.Http;

[UsesVerify]
Expand Down Expand Up @@ -57,6 +58,52 @@ public Task MethodThatDoesHttp()

#if(NET5_0_OR_GREATER)

[Fact]
public async Task MediaTypePlainTextIsRecorded()
{
const string content = "string content 123";
var recordingHandler = new RecordingHandler(true);
recordingHandler.InnerHandler = new ConfigurableContentTypeDelegatingHandler(new StringContent(content, Encoding.UTF8));
using var client = new HttpClient(recordingHandler);

var response = await client.GetAsync("https://dont-care.org/get");

Assert.Equal(content, recordingHandler.Sends.Single().ResponseContent);
}

[Fact]
public async Task MediaTypeApplicationJsonIsRecorded()
{
const string content = "{ \"age\": 1234 }";
var recordingHandler = new RecordingHandler(true);
recordingHandler.InnerHandler = new ConfigurableContentTypeDelegatingHandler(new StringContent(content, Encoding.UTF8, "application/json"));
using var client = new HttpClient(recordingHandler);

var response = await client.GetAsync("https://dont-care.org/get");

Assert.Equal(content, recordingHandler.Sends.Single().ResponseContent);
}

class ConfigurableContentTypeDelegatingHandler : DelegatingHandler
{
StringContent content;

public ConfigurableContentTypeDelegatingHandler(StringContent content)
{
this.content = content;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellation)
{
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = content,
};

return Task.FromResult(result);
}
}

#region HttpRecording

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
Expand Down
11 changes: 0 additions & 11 deletions src/Verify.Http/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ internal static string ReadAsString(this HttpContent content)
return content.ReadAsStringAsync().GetAwaiter().GetResult();
}

public static bool IsText(this HttpContent content)
{
var contentType = content.Headers.ContentType;
if (contentType?.MediaType == null)
{
return false;
}

return contentType.MediaType.StartsWith("text");
}

public static Dictionary<string, string> ToDictionary(this HttpHeaders headers)
{
return headers
Expand Down
5 changes: 5 additions & 0 deletions src/Verify.Http/HttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ static bool TryGetMediaTypeExtension(string mediaType, [NotNullWhen(true)] out s
return mappings.TryGetValue(mediaType, out extension);
}

public static bool IsText(this HttpContent content)
{
return content.IsText(out _);
}

public static bool IsText(this HttpContent content, [NotNullWhen(true)] out string? subType)
{
var contentType = content.Headers.ContentType;
Expand Down
2 changes: 1 addition & 1 deletion src/Verify.Http/RecordingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
var item = new LoggedSend(
request.RequestUri,
#if(NET5_0_OR_GREATER)
request.Options,
request.Options,
#endif
request.Method.ToString(),
request.Headers.ToDictionary(),
Expand Down
2 changes: 1 addition & 1 deletion src/Verify.Http/RequestDataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public LoggedSend(
ResponseHeaders = responseHeaders;
ResponseContent = responseContent;
}
}
}
2 changes: 1 addition & 1 deletion src/Verify.Http/Verify.Http.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;netcoreapp3.1;net5;net6</TargetFrameworks>
<TargetFrameworks>net6;net48;netcoreapp3.1;net5</TargetFrameworks>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
Expand Down