Add support for Matcher.Pattern in Pact Body mapping (#789)

* Add support for Matcher.Pattern in Pact Body mapping

* SavePact_Get_Request_And_Response_WithNullBody
This commit is contained in:
Stef Heyenrath
2022-08-15 09:28:16 +02:00
committed by GitHub
parent d2a1d0f069
commit e2bd56531d
3 changed files with 75 additions and 13 deletions

View File

@@ -1,10 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using DevLab.JmesPath.Interop;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Extensions; using WireMock.Extensions;
using WireMock.Matchers;
using WireMock.Pact.Models.V2; using WireMock.Pact.Models.V2;
using WireMock.Server; using WireMock.Server;
using WireMock.Util; using WireMock.Util;
@@ -88,9 +86,9 @@ internal static class PactMapper
return response.BodyAsJson; return response.BodyAsJson;
} }
if (response?.Body != null) // In case the body is a string, try to deserialize into object, else just return the string if (response?.Body != null)
{ {
return JsonUtils.TryDeserializeObject<object?>(response.Body) ?? response.Body; return TryDeserializeJsonStringAsObject(response.Body);
} }
return null; return null;
@@ -140,18 +138,26 @@ internal static class PactMapper
private static object? MapBody(BodyModel? body) private static object? MapBody(BodyModel? body)
{ {
if (body?.Matcher == null || body.Matchers == null) return MapMatcherPattern(body?.Matcher ?? body?.Matchers?.FirstOrDefault());
}
private static object? MapMatcherPattern(MatcherModel? matcher)
{
var pattern = matcher?.Pattern ?? matcher?.Patterns?.FirstOrDefault();
if (pattern is string patternAsString)
{ {
return null; return TryDeserializeJsonStringAsObject(patternAsString);
} }
if (body.Matcher is { Name: nameof(JsonMatcher) }) return pattern;
{ }
return body.Matcher.Pattern;
}
var jsonMatcher = body.Matchers.FirstOrDefault(m => m.Name == nameof(JsonMatcher)); /// <summary>
return jsonMatcher?.Pattern; /// In case it's a string, try to deserialize into object, else just return the string
/// </summary>
private static object? TryDeserializeJsonStringAsObject(string? value)
{
return value != null ? JsonUtils.TryDeserializeObject<object?>(value) ?? value : null;
} }
//private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue) //private static string GetPatternAsStringFromMatchers(MatcherModel[]? matchers, string defaultValue)

View File

@@ -101,6 +101,58 @@ public class PactTests
pact.Interactions[0].Response.Body.Should().Be("test"); pact.Interactions[0].Response.Body.Should().Be("test");
} }
[Fact]
public void SavePact_Get_Request_And_Response_WithNullBody()
{
// Act
var server = WireMockServer.Start();
server
.Given(Request.Create()
.UsingGet()
.WithPath("/tester")
)
.RespondWith(
Response.Create()
.WithStatusCode(HttpStatusCode.OK)
);
var memoryStream = new MemoryStream();
server.SavePact(memoryStream);
var json = Encoding.UTF8.GetString(memoryStream.ToArray());
var pact = JsonConvert.DeserializeObject<WireMock.Pact.Models.V2.Pact>(json)!;
// Assert
pact.Interactions.Should().HaveCount(1);
pact.Interactions[0].Response.Body.Should().BeNull();
}
[Fact]
public void SavePact_Post_Request_WithBody_JsonPartialMatcher()
{
// Act
var server = WireMockServer.Start();
server
.Given(Request.Create()
.UsingPost()
.WithBody(new JsonPartialMatcher(@"{ ""name"": ""stef"" }"))
.WithPath("/tester")
)
.RespondWith(Response.Create());
var memoryStream = new MemoryStream();
server.SavePact(memoryStream);
var json = Encoding.UTF8.GetString(memoryStream.ToArray());
var pact = JsonConvert.DeserializeObject<WireMock.Pact.Models.V2.Pact>(json)!;
// Assert
pact.Interactions.Should().HaveCount(1);
var expectedBody = new JObject { { "name", "stef" } };
pact.Interactions[0].Request.Body.Should().BeEquivalentTo(expectedBody);
}
[Fact] [Fact]
public void SavePact_Multiple_Requests() public void SavePact_Multiple_Requests()
{ {

View File

@@ -32,7 +32,11 @@
"Accept": "application/json" "Accept": "application/json"
}, },
"method": "POST", "method": "POST",
"path": "/add" "path": "/add",
"body": {
"Id": "1",
"FirstName": "Totally"
}
}, },
"response": { "response": {
"body": { "body": {