mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-27 11:17:31 +02:00
Merge branch 'master' into SystemTextJsonMatcher
This commit is contained in:
@@ -9,6 +9,8 @@ using WireMock.Util;
|
||||
using WireMock.Owin;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
using JsonConverter.System.Text.Json;
|
||||
|
||||
namespace WireMock.Net.Tests.Owin.Mappers;
|
||||
|
||||
@@ -34,6 +36,7 @@ public class OwinResponseMapperTests
|
||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||
_optionsMock.SetupAllProperties();
|
||||
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
||||
_optionsMock.SetupGet(o => o.DefaultJsonSerializer).Returns(new NewtonsoftJsonConverter());
|
||||
|
||||
_headers = new Mock<IHeaderDictionary>();
|
||||
_headers.SetupAllProperties();
|
||||
@@ -186,7 +189,7 @@ public class OwinResponseMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
|
||||
public async Task OwinResponseMapper_MapAsync_BodyAsJson_UsingNewtonsoftJson()
|
||||
{
|
||||
// Arrange
|
||||
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);
|
||||
}
|
||||
|
||||
[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]
|
||||
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
||||
{
|
||||
|
||||
@@ -4,10 +4,13 @@ using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using JsonConverter.System.Text.Json;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
@@ -18,11 +21,41 @@ public partial class WireMockServerTests
|
||||
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]
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
@@ -56,7 +89,7 @@ public partial class WireMockServerTests
|
||||
var requestUri = new Uri($"http://localhost:{server.Port}/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
|
||||
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()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
@@ -105,7 +137,7 @@ public partial class WireMockServerTests
|
||||
var requestUri = new Uri($"http://localhost:{server.Port}/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
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
@@ -117,7 +149,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||
@@ -132,7 +163,7 @@ public partial class WireMockServerTests
|
||||
};
|
||||
|
||||
// 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
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
@@ -144,7 +175,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
|
||||
@@ -154,7 +184,7 @@ public partial class WireMockServerTests
|
||||
);
|
||||
|
||||
// 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
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
@@ -166,7 +196,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
|
||||
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
|
||||
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()
|
||||
.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
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -203,8 +232,7 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new JsonConverter.System.Text.Json.SystemTextJsonConverter() );
|
||||
using var server = WireMockServer.Start(x => x.DefaultJsonSerializer = new SystemTextJsonConverter());
|
||||
|
||||
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
|
||||
server.Given(Request.Create()
|
||||
@@ -220,12 +248,12 @@ public partial class WireMockServerTests
|
||||
// Act
|
||||
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
|
||||
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
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
var responseText = await response.Content.ReadAsStringAsync(_ct);
|
||||
responseText.Should().Contain("OK");
|
||||
}
|
||||
#endif
|
||||
@@ -234,7 +262,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
|
||||
{
|
||||
// Arrange
|
||||
var cancelationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
@@ -249,7 +276,7 @@ public partial class WireMockServerTests
|
||||
// Act
|
||||
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
|
||||
var response = await new HttpClient()
|
||||
.PostAsync($"{server.Url}/foo", content, cancelationToken);
|
||||
.PostAsync($"{server.Url}/foo", content, _ct);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
@@ -261,7 +288,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
@@ -282,7 +308,7 @@ public partial class WireMockServerTests
|
||||
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
||||
]);
|
||||
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
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
@@ -294,7 +320,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFormUrlEncodedMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var cancelationToken = TestContext.Current.CancellationToken;
|
||||
var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
@@ -326,7 +351,7 @@ public partial class WireMockServerTests
|
||||
new KeyValuePair<string, string>("email", "johndoe@example.com")
|
||||
]);
|
||||
var responseOrdered = await new HttpClient()
|
||||
.PostAsync($"{server.Url}/foo", contentOrdered, cancelationToken)
|
||||
.PostAsync($"{server.Url}/foo", contentOrdered, _ct)
|
||||
;
|
||||
|
||||
// Assert 1
|
||||
@@ -340,7 +365,7 @@ public partial class WireMockServerTests
|
||||
new KeyValuePair<string, string>("name", "John Doe"),
|
||||
]);
|
||||
var responseUnordered = await new HttpClient()
|
||||
.PostAsync($"{server.Url}/bar", contentUnordered, cancelationToken)
|
||||
.PostAsync($"{server.Url}/bar", contentUnordered, _ct)
|
||||
;
|
||||
|
||||
// Assert 2
|
||||
@@ -353,7 +378,6 @@ public partial class WireMockServerTests
|
||||
public async Task WireMockServer_WithSseBody()
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
using var server = WireMockServer.Start();
|
||||
server
|
||||
.WhenRequest(r => r
|
||||
@@ -387,9 +411,8 @@ public partial class WireMockServerTests
|
||||
using var client = new HttpClient();
|
||||
|
||||
// Act 1
|
||||
var normal = await client.GetAsync(server.Url, cancellationToken)
|
||||
;
|
||||
(await normal.Content.ReadAsStringAsync(cancellationToken)).Should().Be("normal");
|
||||
var normal = await client.GetAsync(server.Url, _ct);
|
||||
(await normal.Content.ReadAsStringAsync(_ct)).Should().Be("normal");
|
||||
|
||||
// Act 2
|
||||
using var response = await client.GetStreamAsync($"{server.Url}/sse", _ct);
|
||||
|
||||
Reference in New Issue
Block a user