-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a unit test and an integration test for the fix.
- Loading branch information
Ruxo Zheng
committed
Sep 16, 2024
1 parent
4c35696
commit 579354c
Showing
2 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
test/WireMock.Net.Tests/ResponseBuilders/ResponseWithProxyIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#if NET8_0_OR_GREATER | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using WireMock.Net.Xunit; | ||
using WireMock.RequestBuilders; | ||
using WireMock.ResponseBuilders; | ||
using WireMock.Server; | ||
using WireMock.Settings; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace WireMock.Net.Tests.ResponseBuilders; | ||
|
||
public sealed class ResponseWithProxyIntegrationTests(ITestOutputHelper output) | ||
{ | ||
[Fact] | ||
public async Task Response_UsingTextPlain() | ||
{ | ||
// Given | ||
using var server = await TestServer.New().Run(); | ||
var port = server.GetPort(); | ||
output.WriteLine($"Server running on port {port}"); | ||
|
||
var settings = new WireMockServerSettings { | ||
Port = 0, | ||
Logger = new TestOutputHelperWireMockLogger(output) | ||
}; | ||
using var mockServer = WireMockServer.Start(settings); | ||
mockServer.Given(Request.Create().WithPath("/zipcode").UsingPatch()) | ||
.RespondWith(Response.Create().WithProxy($"http://localhost:{port}")); | ||
|
||
using var client = new HttpClient { BaseAddress = new Uri(mockServer.Urls[0]) }; | ||
using var content = new ByteArrayContent(Encoding.UTF8.GetBytes("0123")); | ||
content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); | ||
|
||
// When | ||
var response = await client.PatchAsync("/zipcode", content); | ||
|
||
// Then | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
response.Content.Headers.GetValues("Content-Type").Should().BeEquivalentTo("text/plain; charset=utf-8"); | ||
var result = await response.Content.ReadAsStringAsync(); | ||
result.Should().Be("0123"); | ||
} | ||
|
||
sealed class Disposable(Action dispose) : IDisposable | ||
{ | ||
public void Dispose() => dispose(); | ||
} | ||
|
||
sealed class TestServer(WebApplication app) : IDisposable | ||
{ | ||
Disposable disposable = new(() => { }); | ||
|
||
public static TestServer New() { | ||
var builder = WebApplication.CreateBuilder(); | ||
builder.WebHost.ConfigureKestrel(opts => opts.ListenAnyIP(0)); | ||
|
||
var app = builder.Build(); | ||
|
||
app.MapPatch("/zipcode", async (HttpRequest req) => { | ||
var memory = new MemoryStream(); | ||
await req.Body.CopyToAsync(memory); | ||
var content = Encoding.UTF8.GetString(memory.ToArray()); | ||
return content; | ||
}); | ||
return new(app); | ||
} | ||
|
||
public int GetPort() | ||
=> app.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>()!.Addresses | ||
.Select(x => new Uri(x).Port) | ||
.First(); | ||
|
||
public async ValueTask<TestServer> Run() { | ||
var started = new TaskCompletionSource(); | ||
var host = app.Services.GetRequiredService<IHostApplicationLifetime>(); | ||
host.ApplicationStarted.Register(() => started.SetResult()); | ||
_ = Task.Run(() => app.RunAsync()); | ||
await started.Task; | ||
disposable = new(() => host.StopApplication()); | ||
return this; | ||
} | ||
|
||
public void Dispose() { | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
#endif |