This commit is contained in:
Stef Heyenrath
2026-02-14 17:28:37 +01:00
parent 385450a3a8
commit 334675f39c
9 changed files with 139 additions and 132 deletions

View File

@@ -23,7 +23,8 @@ public partial class WireMockServerTests
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
{
// Arrange
var server = WireMockServer.Start();
var cancellationToken = TestContext.Current.CancellationToken;
using var server = WireMockServer.Start();
server.Given(
Request.Create()
.WithPath("/a")
@@ -56,7 +57,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);
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, cancellationToken);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
@@ -68,7 +69,8 @@ public partial class WireMockServerTests
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
{
// Arrange
var server = WireMockServer.Start();
var cancellationToken = TestContext.Current.CancellationToken;
using var server = WireMockServer.Start();
server.Given(
Request.Create()
.WithPath("/a")
@@ -104,7 +106,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);
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json, cancellationToken);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
@@ -116,7 +118,8 @@ public partial class WireMockServerTests
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
{
// Arrange
var server = WireMockServer.Start();
var cancellationToken = TestContext.Current.CancellationToken;
using var server = WireMockServer.Start();
server.Given(
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
)
@@ -130,7 +133,7 @@ public partial class WireMockServerTests
};
// Act
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject);
var response = await new HttpClient().PostAsJsonAsync("http://localhost:" + server.Ports[0] + "/foo", jsonObject, cancellationToken);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
@@ -142,7 +145,8 @@ public partial class WireMockServerTests
public async Task WireMockServer_WithBodyAsJson_Using_PostAsync_And_WildcardMatcher_ShouldMatch()
{
// Arrange
var server = WireMockServer.Start();
var cancellationToken = TestContext.Current.CancellationToken;
using var server = WireMockServer.Start();
server.Given(
Request.Create().UsingPost().WithPath("/foo").WithBody(new WildcardMatcher("*Hello*"))
)
@@ -151,7 +155,7 @@ public partial class WireMockServerTests
);
// Act
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"));
var response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("{ Hi = \"Hello World\" }"), cancellationToken);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
@@ -163,6 +167,7 @@ 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);
@@ -182,16 +187,15 @@ public partial class WireMockServerTests
);
// Act
var cancelationToken = TestContext.Current.CancellationToken;
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"), cancelationToken)
.PostAsync($"{server.Url}/foo", new StringContent(content, Encoding.UTF8, "application/json"), cancellationToken)
;
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseText = await response.Content.ReadAsStringAsync(cancelationToken);
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
responseText.Should().Contain("ec475f56d4694b48bc737500ba575b35-1");
}
@@ -200,6 +204,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() );
var matcher = new JsonPartialWildcardMatcher(new { id = "^[a-f0-9]{32}-[0-9]$" }, ignoreCase: true, regex: true);
@@ -216,12 +221,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"));
var response = await httpClient.PostAsync($"{server.Url}/system-text-json", new StringContent(content, Encoding.UTF8, "application/json"), cancellationToken);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseText = await response.Content.ReadAsStringAsync();
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
responseText.Should().Contain("OK");
}
#endif
@@ -245,8 +250,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, cancelationToken);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
@@ -258,7 +262,8 @@ public partial class WireMockServerTests
public async Task WireMockServer_WithBodyAsFormUrlEncoded_Using_PostAsync_And_WithExactMatcher()
{
// Arrange
var server = WireMockServer.Start();
var cancellationToken = TestContext.Current.CancellationToken;
using var server = WireMockServer.Start();
server.Given(
Request.Create()
.UsingPost()
@@ -272,15 +277,14 @@ public partial class WireMockServerTests
);
// Act
var content = new FormUrlEncodedContent(new[]
{
var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
});
var response = await new HttpClient()
.PostAsync($"{server.Url}/foo", content)
]);
using var httpClient = new HttpClient();
var response = await httpClient.PostAsync($"{server.Url}/foo", content, cancellationToken)
;
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
@@ -293,7 +297,7 @@ public partial class WireMockServerTests
// Arrange
var cancelationToken = TestContext.Current.CancellationToken;
var matcher = new FormUrlEncodedMatcher(["email=johndoe@example.com", "name=John Doe"]);
var server = WireMockServer.Start();
using var server = WireMockServer.Start();
server.Given(
Request.Create()
.UsingPost()
@@ -317,11 +321,11 @@ public partial class WireMockServerTests
);
// Act 1
var contentOrdered = new FormUrlEncodedContent(new[]
{
var contentOrdered = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
});
]);
var responseOrdered = await new HttpClient()
.PostAsync($"{server.Url}/foo", contentOrdered, cancelationToken)
;
@@ -331,11 +335,11 @@ public partial class WireMockServerTests
// Act 2
var contentUnordered = new FormUrlEncodedContent(new[]
{
var contentUnordered = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("email", "johndoe@example.com"),
new KeyValuePair<string, string>("name", "John Doe"),
});
]);
var responseUnordered = await new HttpClient()
.PostAsync($"{server.Url}/bar", contentUnordered, cancelationToken)
;
@@ -350,7 +354,8 @@ public partial class WireMockServerTests
public async Task WireMockServer_WithSseBody()
{
// Arrange
var server = WireMockServer.Start();
var cancellationToken = TestContext.Current.CancellationToken;
using var server = WireMockServer.Start();
server
.WhenRequest(r => r
.UsingGet()
@@ -383,10 +388,9 @@ public partial class WireMockServerTests
using var client = new HttpClient();
// Act 1
var normal = await new HttpClient()
.GetAsync(server.Url)
var normal = await client.GetAsync(server.Url, cancellationToken)
;
(await normal.Content.ReadAsStringAsync()).Should().Be("normal");
(await normal.Content.ReadAsStringAsync(cancellationToken)).Should().Be("normal");
// Act 2
using var response = await client.GetStreamAsync($"{server.Url}/sse");
@@ -395,7 +399,7 @@ public partial class WireMockServerTests
var data = string.Empty;
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();
var line = reader.ReadLine();
data += line;
}