This commit is contained in:
Stef Heyenrath
2026-02-14 08:59:23 +01:00
23 changed files with 533 additions and 110 deletions

View File

@@ -1,21 +1,15 @@
// Copyright © WireMock.Net
#if !NET452
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit;
namespace WireMock.Net.Tests;
@@ -35,11 +29,10 @@ public partial class WireMockServerTests
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
[
new JmesPathMatcher("requestId == '1'"),
new JmesPathMatcher("value == 'A'")
},
],
MatchOperator.And
)
.UsingPost()
@@ -50,11 +43,10 @@ public partial class WireMockServerTests
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
[
new JmesPathMatcher("requestId == '2'"),
new JmesPathMatcher("value == 'A'")
},
],
MatchOperator.And
)
.UsingPost()
@@ -82,12 +74,11 @@ public partial class WireMockServerTests
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
[
new JmesPathMatcher("extra == 'X'"),
new JmesPathMatcher("requestId == '1'"),
new JmesPathMatcher("value == 'A'")
},
],
MatchOperator.And
)
.UsingPost()
@@ -99,11 +90,10 @@ public partial class WireMockServerTests
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
[
new JmesPathMatcher("requestId == '1'"),
new JmesPathMatcher("value == 'A'")
},
],
MatchOperator.And
)
.UsingPost()
@@ -189,6 +179,7 @@ public partial class WireMockServerTests
{"jsonrpc":"2.0","id":"{{request.bodyAsJson.id}}","result":{"protocolVersion":"2024-11-05","capabilities":{"logging":{},"prompts":{"listChanged":true},"resources":{"subscribe":true,"listChanged":true},"tools":{"listChanged":true}},"serverInfo":{"name":"ExampleServer","version":"1.0.0"}}}
""")
.WithStatusCode(200)
.WithTransformer(true)
);
// Act
@@ -200,15 +191,47 @@ public partial class WireMockServerTests
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseText = await response.RequestMessage!.Content!.ReadAsStringAsync();
var responseText = await response.Content.ReadAsStringAsync();
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
}
#if NET6_0_OR_GREATER
[Fact]
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_JsonPartialWildcardMatcher_And_SystemTextJson_ShouldMatch()
{
// Arrange
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);
server.Given(Request.Create()
.WithHeader("Content-Type", "application/json*")
.UsingPost()
.WithPath("/system-text-json")
.WithBody(matcher)
)
.RespondWith(Response.Create()
.WithBody("OK")
);
// Act
var content = """{"id":"ec475f56d4694b48bc737500ba575b35-1"}""";
var response = await new HttpClient()
.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"))
.ConfigureAwait(false);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseText = await response.Content.ReadAsStringAsync();
responseText.Should().Contain("OK");
}
#endif
[Fact]
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithFunc()
{
// Arrange
var server = WireMockServer.Start();
using var server = WireMockServer.Start();
server.Given(
Request.Create()
.UsingPost()
@@ -220,7 +243,7 @@ public partial class WireMockServerTests
);
// Act
var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("key1", "value1") });
var content = new FormUrlEncodedContent([new KeyValuePair<string, string>("key1", "value1")]);
var response = await new HttpClient()
.PostAsync($"{server.Url}/foo", content)
;