mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 00:28:59 +01:00
* UseContentType * Fix unit tests * Add a unit test and an integration test for the fix. * Simplify body type checking with GetBodyType extension. * Split IBodyDataExtension, and use imperative style instead of functional style * Remove excessive null forgiving operators * Adjust braces --------- Co-authored-by: Ruxo Zheng <rz@just.sent.as>
106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
#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 |