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 Interceptor Overload for Send #694

Merged
merged 2 commits into from
Dec 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public InterceptingHttpMessageHandler(HttpClientInterceptorOptions options, Http
_options = options ?? throw new ArgumentNullException(nameof(options));
}

#if NET6_0_OR_GREATER
/// <inheritdoc />
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) =>
SendAsync(request, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a bit ick, but as it's a test library and probably quite a minority subset of cases (as it's taken 2 years to hit this) this is fine.

#endif

/// <inheritdoc />
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI.Unshipped.txt" />
<AdditionalFiles Include="PublicAPI/$(TargetFramework)/PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI/$(TargetFramework)/PublicAPI.Unshipped.txt" />
</ItemGroup>
</Project>
117 changes: 117 additions & 0 deletions src/HttpClientInterception/PublicAPI/net6.0/PublicAPI.Shipped.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new bit should go in the unshipped files until it actually gets to NuGet, then it'll get moved post-release.

However that's probably not worth doing if we just ship this on Monday with no other changes.

117 changes: 117 additions & 0 deletions src/HttpClientInterception/PublicAPI/net7.0/PublicAPI.Shipped.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
27 changes: 27 additions & 0 deletions tests/HttpClientInterception.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,33 @@ public static async Task Intercept_Custom_Http_Method()
content.ShouldBe([0, 1]);
}

[Fact]
public static void Intercept_Custom_Synchronous_Http_Method()
{
// Arrange
var builder = new HttpRequestInterceptionBuilder()
.ForMethod(new HttpMethod("custom"))
.ForHost("custom.domain.com")
.ForQuery("length=2")
.WithContent(() => new byte[] { 0, 1 });

var options = new HttpClientInterceptorOptions()
.Register(builder);

using var client = options.CreateHttpClient();
using var message = new HttpRequestMessage(new HttpMethod("custom"), "http://custom.domain.com?length=2");

// Act
using var response = client.Send(message);
using var responseStream = response.Content.ReadAsStream();
using var responseBuffer = new MemoryStream();
responseStream.CopyTo(responseBuffer);
byte[] content = responseBuffer.ToArray();

// Assert
content.ShouldBe([0, 1]);
}

[Fact]
public static async Task Inject_Fault_For_Http_Get()
{
Expand Down