Skip to content

Commit

Permalink
Add a unit test and an integration test for the fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruxo Zheng committed Sep 16, 2024
1 parent 4c35696 commit 579354c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
22 changes: 21 additions & 1 deletion test/WireMock.Net.Tests/Http/HttpRequestMessageHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ public async Task HttpRequestMessageHelper_Create_Bytes()
Check.That(await message.Content.ReadAsByteArrayAsync().ConfigureAwait(false)).ContainsExactly(Encoding.UTF8.GetBytes("hi"));
}

[Fact]
public async Task HttpRequestMessageHelper_Create_TextPlain()
{
// Assign
var body = new BodyData
{
BodyAsString = "0123", // or 83 in decimal
BodyAsJson = 83,
DetectedBodyType = BodyType.Json,
DetectedBodyTypeFromContentType = BodyType.String
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp, body);

// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");

// Assert
Check.That(await message.Content!.ReadAsStringAsync().ConfigureAwait(false)).Equals("0123");
}

[Fact]
public async Task HttpRequestMessageHelper_Create_Json()
{
Expand All @@ -64,7 +84,7 @@ public async Task HttpRequestMessageHelper_Create_Json()
var message = HttpRequestMessageHelper.Create(request, "http://url");

// Assert
Check.That(await message.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals("{\"x\":42}");
Check.That(await message.Content!.ReadAsStringAsync().ConfigureAwait(false)).Equals("{\"x\":42}");
}

[Fact]
Expand Down
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

0 comments on commit 579354c

Please sign in to comment.