Skip to content

Commit

Permalink
Merge pull request #106 from xinlifoobar/master
Browse files Browse the repository at this point in the history
Proxy Http Reason Phrase to Avoid Unconscious Incompatibilities.
  • Loading branch information
twitchax committed Oct 18, 2023
2 parents 333a011 + 3041d02 commit b3f70af
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Core/Extensions/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using AspNetCore.Proxy.Builders;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCore.Proxy
Expand Down Expand Up @@ -124,6 +125,13 @@ private static Task WriteProxiedHttpResponseAsync(this HttpContext context, Http
var response = context.Response;

response.StatusCode = (int)responseMessage.StatusCode;

var httpResponseFeature = context.Features.Get<IHttpResponseFeature>();
if (httpResponseFeature != null)
{
httpResponseFeature.ReasonPhrase = responseMessage.ReasonPhrase;
}

foreach (var header in responseMessage.Headers)
{
response.Headers[header.Key] = header.Value.ToArray();
Expand Down
26 changes: 26 additions & 0 deletions src/Test/Http/HttpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.DependencyInjection;
using AspNetCore.Proxy.Options;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http.Features;

[assembly: SuppressMessage("Readability", "RCS1090", Justification = "Not a library, so no need for `ConfigureAwait`.")]

Expand Down Expand Up @@ -143,6 +144,31 @@ public Task GetWithCustomResponse(int postId)
return this.HttpProxyAsync($"https://jsonplaceholder.typicode.com/posts/{postId}", options);
}

[Route("api/controller/customreasonphase")]
public void GetWithCustomReasonPhase(int postId)
{
}

[Route("api/controller/customreasonphase/proxy")]
public Task GetWithCustomReasonPhase()
{
var options = HttpProxyOptionsBuilder.Instance.WithIntercept(
async (ctx) =>

Check warning on line 156 in src/Test/Http/HttpHelpers.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 156 in src/Test/Http/HttpHelpers.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var httpResponseFeature = ctx.Features.Get<IHttpResponseFeature>();
if (httpResponseFeature != null)
{
httpResponseFeature.ReasonPhrase = "I am dummy!";
}
return true;
}).Build();

return this.HttpProxyAsync("https://postman-echo.com/post", options);
}


[Route("api/controller/timeoutclient/{postId}")]
public Task GetWithTimeoutClient(int postId)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Test/Http/HttpIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ public async Task CanGetIntercept()
Assert.Equal("This was intercepted and not proxied!", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task CanProxyReasonPhase()
{
var response = await _client.GetAsync("api/controller/customreasonphase/proxy");

response.EnsureSuccessStatusCode();
Assert.Equal("I am dummy!", response.ReasonPhrase);
}

[Fact]
public async Task CanProxyConcurrentCalls()
{
Expand Down

0 comments on commit b3f70af

Please sign in to comment.