mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Refactor some code (IBodyDataExtensions)
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
using WireMock.Types;
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using WireMock.Types;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace WireMock.Util;
|
||||
|
||||
public static class IBodyDataExtension
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class IBodyDataExtensions
|
||||
{
|
||||
public static BodyType GetBodyType(this IBodyData bodyData)
|
||||
{
|
||||
@@ -11,10 +14,12 @@ public static class IBodyDataExtension
|
||||
{
|
||||
return bodyData.DetectedBodyTypeFromContentType.Value;
|
||||
}
|
||||
|
||||
if (bodyData.DetectedBodyType is not null and not BodyType.None)
|
||||
{
|
||||
return bodyData.DetectedBodyType.Value;
|
||||
}
|
||||
|
||||
return BodyType.None;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ using WireMock.Util;
|
||||
|
||||
#if !USE_ASPNETCORE
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
|
||||
@@ -143,24 +142,24 @@ namespace WireMock.Owin.Mappers
|
||||
{
|
||||
case BodyType.String:
|
||||
case BodyType.FormUrlEncoded:
|
||||
return (bodyData!.Encoding ?? _utf8NoBom).GetBytes(bodyData.BodyAsString!);
|
||||
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(bodyData.BodyAsString!);
|
||||
|
||||
case BodyType.Json:
|
||||
var formatting = bodyData!.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None;
|
||||
var formatting = bodyData.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.None;
|
||||
var jsonBody = JsonConvert.SerializeObject(bodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore });
|
||||
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);
|
||||
|
||||
#if PROTOBUF
|
||||
case BodyType.ProtoBuf:
|
||||
var protoDefinition = bodyData!.ProtoDefinition?.Invoke().Text;
|
||||
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinition, bodyData!.ProtoBufMessageType, bodyData!.BodyAsJson).ConfigureAwait(false);
|
||||
var protoDefinition = bodyData.ProtoDefinition?.Invoke().Text;
|
||||
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinition, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
|
||||
#endif
|
||||
|
||||
case BodyType.Bytes:
|
||||
return bodyData!.BodyAsBytes;
|
||||
return bodyData.BodyAsBytes;
|
||||
|
||||
case BodyType.File:
|
||||
return _options.FileSystemHandler?.ReadResponseBodyAsFile(bodyData!.BodyAsFile!);
|
||||
return _options.FileSystemHandler?.ReadResponseBodyAsFile(bodyData.BodyAsFile!);
|
||||
|
||||
case BodyType.MultiPart:
|
||||
_options.Logger.Warn("MultiPart body type is not handled!");
|
||||
@@ -179,10 +178,7 @@ namespace WireMock.Owin.Mappers
|
||||
AppendResponseHeader(
|
||||
response,
|
||||
HttpKnownHeaderNames.Date,
|
||||
new[]
|
||||
{
|
||||
DateTime.UtcNow.ToString(CultureInfo.InvariantCulture.DateTimeFormat.RFC1123Pattern, CultureInfo.InvariantCulture)
|
||||
}
|
||||
[ DateTime.UtcNow.ToString(CultureInfo.InvariantCulture.DateTimeFormat.RFC1123Pattern, CultureInfo.InvariantCulture) ]
|
||||
);
|
||||
|
||||
// Set other headers
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#if NET8_0_OR_GREATER
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
#if NET8_0_OR_GREATER
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -36,7 +37,8 @@ public sealed class ResponseWithProxyIntegrationTests(ITestOutputHelper output)
|
||||
var port = server.GetPort();
|
||||
output.WriteLine($"Server running on port {port}");
|
||||
|
||||
var settings = new WireMockServerSettings {
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
Port = 0,
|
||||
Logger = new TestOutputHelperWireMockLogger(output)
|
||||
};
|
||||
@@ -45,7 +47,7 @@ public sealed class ResponseWithProxyIntegrationTests(ITestOutputHelper output)
|
||||
.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"));
|
||||
using var content = new ByteArrayContent("0123"u8.ToArray());
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||
|
||||
// When
|
||||
@@ -65,15 +67,17 @@ public sealed class ResponseWithProxyIntegrationTests(ITestOutputHelper output)
|
||||
|
||||
sealed class TestServer(WebApplication app) : IDisposable
|
||||
{
|
||||
Disposable disposable = new(() => { });
|
||||
private Disposable _disposable = new(() => { });
|
||||
|
||||
public static TestServer 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) => {
|
||||
app.MapPatch("/zipcode", async (HttpRequest req) =>
|
||||
{
|
||||
var memory = new MemoryStream();
|
||||
await req.Body.CopyToAsync(memory);
|
||||
var content = Encoding.UTF8.GetString(memory.ToArray());
|
||||
@@ -87,20 +91,21 @@ public sealed class ResponseWithProxyIntegrationTests(ITestOutputHelper output)
|
||||
.Select(x => new Uri(x).Port)
|
||||
.First();
|
||||
|
||||
public async ValueTask<TestServer> Run() {
|
||||
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());
|
||||
_disposable = new(() => host.StopApplication());
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
disposable.Dispose();
|
||||
public void Dispose()
|
||||
{
|
||||
_disposable.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user