mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-29 12:17:38 +02:00
Merge branch 'master' into SystemTextJsonMatcher
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
@@ -99,4 +100,12 @@ internal interface IWireMockMiddlewareOptions
|
|||||||
/// WebSocket settings.
|
/// WebSocket settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
WebSocketSettings? WebSocketSettings { get; set; }
|
WebSocketSettings? WebSocketSettings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the default JSON converter used for serialization.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Set this property to customize how objects are serialized to and deserialized from JSON during mapping.
|
||||||
|
/// </remarks>
|
||||||
|
IJsonConverter DefaultJsonSerializer { get; set; }
|
||||||
}
|
}
|
||||||
@@ -3,27 +3,25 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using RandomDataGenerator.FieldOptions;
|
using RandomDataGenerator.FieldOptions;
|
||||||
using RandomDataGenerator.Randomizers;
|
using RandomDataGenerator.Randomizers;
|
||||||
using Stef.Validation;
|
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.ResponseProviders;
|
using WireMock.ResponseProviders;
|
||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
|
|
||||||
namespace WireMock.Owin.Mappers
|
namespace WireMock.Owin.Mappers;
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OwinResponseMapper
|
/// OwinResponseMapper
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class OwinResponseMapper : IOwinResponseMapper
|
internal class OwinResponseMapper(IWireMockMiddlewareOptions options) : IOwinResponseMapper
|
||||||
{
|
{
|
||||||
private readonly IRandomizerNumber<double> _randomizerDouble = RandomizerFactory.GetRandomizer(new FieldOptionsDouble { Min = 0, Max = 1 });
|
private readonly IRandomizerNumber<double> _randomizerDouble = RandomizerFactory.GetRandomizer(new FieldOptionsDouble { Min = 0, Max = 1 });
|
||||||
private readonly IRandomizerBytes _randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 100, Max = 200 });
|
private readonly IRandomizerBytes _randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 100, Max = 200 });
|
||||||
private readonly IWireMockMiddlewareOptions _options;
|
|
||||||
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
||||||
|
|
||||||
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
||||||
@@ -42,15 +40,6 @@ namespace WireMock.Owin.Mappers
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructor
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">The IWireMockMiddlewareOptions.</param>
|
|
||||||
public OwinResponseMapper(IWireMockMiddlewareOptions options)
|
|
||||||
{
|
|
||||||
_options = Guard.NotNull(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task MapAsync(IResponseMessage? responseMessage, HttpResponse response)
|
public async Task MapAsync(IResponseMessage? responseMessage, HttpResponse response)
|
||||||
{
|
{
|
||||||
@@ -107,7 +96,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_options.Logger.Warn("Error writing response body. Exception : {0}", ex);
|
options.Logger.Warn("Error writing response body. Exception : {0}", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +125,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
|
|
||||||
private int MapStatusCode(int code)
|
private int MapStatusCode(int code)
|
||||||
{
|
{
|
||||||
if (_options.AllowOnlyDefinedHttpStatusCodeInResponse == true && !Enum.IsDefined(typeof(HttpStatusCode), code))
|
if (options.AllowOnlyDefinedHttpStatusCodeInResponse == true && !Enum.IsDefined(typeof(HttpStatusCode), code))
|
||||||
{
|
{
|
||||||
return (int)HttpStatusCode.OK;
|
return (int)HttpStatusCode.OK;
|
||||||
}
|
}
|
||||||
@@ -159,8 +148,13 @@ namespace WireMock.Owin.Mappers
|
|||||||
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 jsonConverterOptions = new JsonConverterOptions
|
||||||
var jsonBody = JsonConvert.SerializeObject(bodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore });
|
{
|
||||||
|
WriteIndented = bodyData.BodyAsJsonIndented == true,
|
||||||
|
IgnoreNullValues = true
|
||||||
|
};
|
||||||
|
|
||||||
|
var jsonBody = options.DefaultJsonSerializer.Serialize(bodyData.BodyAsJson!, jsonConverterOptions);
|
||||||
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);
|
return (bodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody);
|
||||||
|
|
||||||
case BodyType.ProtoBuf:
|
case BodyType.ProtoBuf:
|
||||||
@@ -175,10 +169,10 @@ namespace WireMock.Owin.Mappers
|
|||||||
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!");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BodyType.None:
|
case BodyType.None:
|
||||||
@@ -248,4 +242,3 @@ namespace WireMock.Owin.Mappers
|
|||||||
response.Headers.Append(headerName, values);
|
response.Headers.Append(headerName, values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
@@ -108,5 +110,9 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public ConcurrentDictionary<Guid, WebSocketConnectionRegistry> WebSocketRegistries { get; } = new();
|
public ConcurrentDictionary<Guid, WebSocketConnectionRegistry> WebSocketRegistries { get; } = new();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public WebSocketSettings? WebSocketSettings { get; set; }
|
public WebSocketSettings? WebSocketSettings { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IJsonConverter DefaultJsonSerializer { get; set; } = new NewtonsoftJsonConverter();
|
||||||
}
|
}
|
||||||
@@ -414,6 +414,7 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
||||||
_options.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode)_settings.ClientCertificateMode;
|
_options.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode)_settings.ClientCertificateMode;
|
||||||
_options.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
|
_options.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
|
||||||
|
_options.DefaultJsonSerializer = _settings.DefaultJsonSerializer;
|
||||||
|
|
||||||
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
||||||
var startTask = _httpServer.StartAsync();
|
var startTask = _httpServer.StartAsync();
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ using WireMock.Util;
|
|||||||
using WireMock.Owin;
|
using WireMock.Owin;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
|
using JsonConverter.System.Text.Json;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.Owin.Mappers;
|
namespace WireMock.Net.Tests.Owin.Mappers;
|
||||||
|
|
||||||
@@ -34,6 +36,7 @@ public class OwinResponseMapperTests
|
|||||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||||
_optionsMock.SetupAllProperties();
|
_optionsMock.SetupAllProperties();
|
||||||
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
||||||
|
_optionsMock.SetupGet(o => o.DefaultJsonSerializer).Returns(new NewtonsoftJsonConverter());
|
||||||
|
|
||||||
_headers = new Mock<IHeaderDictionary>();
|
_headers = new Mock<IHeaderDictionary>();
|
||||||
_headers.SetupAllProperties();
|
_headers.SetupAllProperties();
|
||||||
@@ -186,7 +189,7 @@ public class OwinResponseMapperTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
|
public async Task OwinResponseMapper_MapAsync_BodyAsJson_UsingNewtonsoftJson()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var json = new { t = "x", i = (string?)null };
|
var json = new { t = "x", i = (string?)null };
|
||||||
@@ -203,6 +206,25 @@ public class OwinResponseMapperTests
|
|||||||
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task OwinResponseMapper_MapAsync_BodyAsJson_UsingSystemTextJson()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var json = new { t = "x", i = (string?)null };
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
|
{
|
||||||
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
|
BodyData = new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = json, BodyAsJsonIndented = false }
|
||||||
|
};
|
||||||
|
_optionsMock.SetupGet(o => o.DefaultJsonSerializer).Returns(new SystemTextJsonConverter());
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,10 +4,13 @@ using System.Net;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using JsonConverter.System.Text.Json;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests;
|
namespace WireMock.Net.Tests;
|
||||||
|
|
||||||
@@ -18,11 +21,41 @@ public partial class WireMockServerTests
|
|||||||
public string? Hi { get; set; }
|
public string? Hi { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Person
|
||||||
|
{
|
||||||
|
[JsonPropertyName("first_name")]
|
||||||
|
public string FirstName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("last_name")]
|
||||||
|
public string LastName { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WireMockServer_WithBodyAsJson_UsingWireMockServerSettings_SystemTextJsonConverter_ShouldReturnCorrectResponse()
|
||||||
|
{
|
||||||
|
// Arange
|
||||||
|
var person = new Person { FirstName = "John", LastName = "Smith" };
|
||||||
|
|
||||||
|
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||||
|
{
|
||||||
|
DefaultJsonSerializer = new SystemTextJsonConverter()
|
||||||
|
});
|
||||||
|
|
||||||
|
// Act
|
||||||
|
server
|
||||||
|
.Given(Request.Create().UsingAnyMethod())
|
||||||
|
.RespondWith(Response.Create().WithBodyAsJson(person));
|
||||||
|
|
||||||
|
var response = await server.CreateClient().GetStringAsync("/", _ct);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.Should().BeEquivalentTo("{\"first_name\":\"John\",\"last_name\":\"Smith\"}");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -56,7 +89,7 @@ public partial class WireMockServerTests
|
|||||||
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
||||||
|
|
||||||
var json = new { requestId = "1", value = "A" };
|
var json = new { requestId = "1", value = "A" };
|
||||||
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, cancellationToken);
|
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -68,7 +101,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -105,7 +137,7 @@ public partial class WireMockServerTests
|
|||||||
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
var requestUri = new Uri($"http://localhost:{server.Port}/a");
|
||||||
|
|
||||||
var json = new { extra = "X", requestId = "1", value = "A" };
|
var json = new { extra = "X", requestId = "1", value = "A" };
|
||||||
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, cancellationToken);
|
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -117,7 +149,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||||
@@ -132,7 +163,7 @@ public partial class WireMockServerTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject, cancellationToken);
|
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -144,7 +175,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||||
@@ -154,7 +184,7 @@ public partial class WireMockServerTests
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"), cancellationToken);
|
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"), _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -166,7 +196,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
|
|
||||||
var matcher = new JsonPartialWildcardMatcher(new { method = "initialize", id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
var matcher = new JsonPartialWildcardMatcher(new { method = "initialize", id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||||
@@ -188,13 +217,13 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var content = "{\"jsonrpc\":\"2.0\",\"id\":\"ec475f56d4694b48bc737500ba575b35-1\",\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"GitHub Test\",\"version\":\"1.0.0\"}}}";
|
var content = "{\"jsonrpc\":\"2.0\",\"id\":\"ec475f56d4694b48bc737500ba575b35-1\",\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"GitHub Test\",\"version\":\"1.0.0\"}}}";
|
||||||
var response = await new HttpClient()
|
var response = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", new StringContent(content, Encoding.UTF8, "application/json"), cancellationToken)
|
.PostAsync($"{server.Url}/foo", new StringContent(content, Encoding.UTF8, "application/json"), _ct)
|
||||||
;
|
;
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
|
var responseText = await response.Content.ReadAsStringAsync(_ct);
|
||||||
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
|
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,8 +232,7 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new SystemTextJsonConverter());
|
||||||
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new JsonConverter.System.Text.Json.SystemTextJsonConverter() );
|
|
||||||
|
|
||||||
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||||
server.Given(Request.Create()
|
server.Given(Request.Create()
|
||||||
@@ -220,12 +248,12 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
|
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = new HttpClient();
|
||||||
var response = await httpClient.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"), cancellationToken);
|
var response = await httpClient.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"), _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
|
|
||||||
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
|
var responseText = await response.Content.ReadAsStringAsync(_ct);
|
||||||
responseText.Should().Contain("OK");
|
responseText.Should().Contain("OK");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -234,7 +262,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancelationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -249,7 +276,7 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
|
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
|
||||||
var response = await new HttpClient()
|
var response = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", content, cancelationToken);
|
.PostAsync($"{server.Url}/foo", content, _ct);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -261,7 +288,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
Request.Create()
|
Request.Create()
|
||||||
@@ -282,7 +308,7 @@ public partial class WireMockServerTests
|
|||||||
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
||||||
]);
|
]);
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = new HttpClient();
|
||||||
var response = await httpClient.PostAsync($"{server.Url}/foo", content, cancellationToken)
|
var response = await httpClient.PostAsync($"{server.Url}/foo", content, _ct)
|
||||||
;
|
;
|
||||||
// Assert
|
// Assert
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -294,7 +320,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFormUrlEncodedMatcher()
|
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFormUrlEncodedMatcher()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancelationToken = TestContext.Current.CancellationToken;
|
|
||||||
var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
|
var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server.Given(
|
server.Given(
|
||||||
@@ -326,7 +351,7 @@ public partial class WireMockServerTests
|
|||||||
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
||||||
]);
|
]);
|
||||||
var responseOrdered = await new HttpClient()
|
var responseOrdered = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/foo", contentOrdered, cancelationToken)
|
.PostAsync($"{server.Url}/foo", contentOrdered, _ct)
|
||||||
;
|
;
|
||||||
|
|
||||||
// Assert 1
|
// Assert 1
|
||||||
@@ -340,7 +365,7 @@ public partial class WireMockServerTests
|
|||||||
new KeyValuePair<string, string>("name", "John Doe"),
|
new KeyValuePair<string, string>("name", "John Doe"),
|
||||||
]);
|
]);
|
||||||
var responseUnordered = await new HttpClient()
|
var responseUnordered = await new HttpClient()
|
||||||
.PostAsync($"{server.Url}/bar", contentUnordered, cancelationToken)
|
.PostAsync($"{server.Url}/bar", contentUnordered, _ct)
|
||||||
;
|
;
|
||||||
|
|
||||||
// Assert 2
|
// Assert 2
|
||||||
@@ -353,7 +378,6 @@ public partial class WireMockServerTests
|
|||||||
public async Task WireMockServer_WithSseBody()
|
public async Task WireMockServer_WithSseBody()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var cancellationToken = TestContext.Current.CancellationToken;
|
|
||||||
using var server = WireMockServer.Start();
|
using var server = WireMockServer.Start();
|
||||||
server
|
server
|
||||||
.WhenRequest(r => r
|
.WhenRequest(r => r
|
||||||
@@ -387,9 +411,8 @@ public partial class WireMockServerTests
|
|||||||
using var client = new HttpClient();
|
using var client = new HttpClient();
|
||||||
|
|
||||||
// Act 1
|
// Act 1
|
||||||
var normal = await client.GetAsync(server.Url, cancellationToken)
|
var normal = await client.GetAsync(server.Url, _ct);
|
||||||
;
|
(await normal.Content.ReadAsStringAsync(_ct)).Should().Be("normal");
|
||||||
(await normal.Content.ReadAsStringAsync(cancellationToken)).Should().Be("normal");
|
|
||||||
|
|
||||||
// Act 2
|
// Act 2
|
||||||
using var response = await client.GetStreamAsync($"{server.Url}/sse", _ct);
|
using var response = await client.GetStreamAsync($"{server.Url}/sse", _ct);
|
||||||
|
|||||||
Reference in New Issue
Block a user