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