Add more tests for JmesPathMatchers and StringUtils.ParseMatchOperator (#1009)

* Add more tests for JmesPathMatchers and StringUtils.ParseMatchOperator

* .

* prio
This commit is contained in:
Stef Heyenrath
2023-10-16 17:13:52 +02:00
committed by GitHub
parent f7cd4b100e
commit 2f29d80336
8 changed files with 185 additions and 23 deletions

View File

@@ -1,9 +1,8 @@
#if !NET452
//using System;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
//using System.Net.Http.Json;
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.Matchers;
@@ -21,6 +20,103 @@ public partial class WireMockServerTests
public string? Hi { get; set; }
}
[Fact]
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch()
{
// Arrange
var server = WireMockServer.Start();
server.Given(
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
new JmesPathMatcher("requestId == '1'"),
new JmesPathMatcher("value == 'A'")
},
MatchOperator.And
)
.UsingPost()
)
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK));
server.Given(
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
new JmesPathMatcher("requestId == '2'"),
new JmesPathMatcher("value == 'A'")
},
MatchOperator.And
)
.UsingPost()
)
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.Moved));
// Act
var requestUri = new Uri($"http://localhost:{server.Port}/a");
var json = new { requestId = "1", value = "A" };
var response = await server.CreateClient().PostAsJsonAsync(requestUri, json).ConfigureAwait(false);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
server.Stop();
}
[Fact]
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_MultipleJmesPathMatchers_ShouldMatch_BestMatching()
{
// Arrange
var server = WireMockServer.Start();
server.Given(
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
new JmesPathMatcher("extra == 'X'"),
new JmesPathMatcher("requestId == '1'"),
new JmesPathMatcher("value == 'A'")
},
MatchOperator.And
)
.UsingPost()
)
.AtPriority(1) // Higher priority
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK));
server.Given(
Request.Create()
.WithPath("/a")
.WithBody(
new IMatcher[]
{
new JmesPathMatcher("requestId == '1'"),
new JmesPathMatcher("value == 'A'")
},
MatchOperator.And
)
.UsingPost()
)
.AtPriority(2)
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.Moved));
// Act
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).ConfigureAwait(false);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
server.Stop();
}
[Fact]
public async Task WireMockServer_WithBodyAsJson_Using_PostAsJsonAsync_And_WildcardMatcher_ShouldMatch()
{