Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/WireMock.Net/Serialization/PactMapper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DevLab.JmesPath.Interop;
using WireMock.Admin.Mappings;
using WireMock.Extensions;
using WireMock.Matchers;
using WireMock.Pact.Models.V2;
using WireMock.Server;
using WireMock.Util;
Expand Down Expand Up @@ -88,9 +86,9 @@ private static PactResponse MapResponse(ResponseModel? response)
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;
Expand Down Expand Up @@ -140,18 +138,26 @@ private static int MapStatusCode(object? statusCode)

private static object? MapBody(BodyModel? body)
{
if (body?.Matcher == null || body.Matchers == null)
{
return null;
}
return MapMatcherPattern(body?.Matcher ?? body?.Matchers?.FirstOrDefault());
}

if (body.Matcher is { Name: nameof(JsonMatcher) })
private static object? MapMatcherPattern(MatcherModel? matcher)
{
var pattern = matcher?.Pattern ?? matcher?.Patterns?.FirstOrDefault();
if (pattern is string patternAsString)
{
return body.Matcher.Pattern;
return TryDeserializeJsonStringAsObject(patternAsString);
}

var jsonMatcher = body.Matchers.FirstOrDefault(m => m.Name == nameof(JsonMatcher));
return jsonMatcher?.Pattern;
return pattern;
}

/// <summary>
/// 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)
Expand Down
52 changes: 52 additions & 0 deletions test/WireMock.Net.Tests/Pact/PactTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,58 @@ public void SavePact_Get_Request_And_Response_WithBody_StringIsString()
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]
public void SavePact_Multiple_Requests()
{
Expand Down
6 changes: 5 additions & 1 deletion test/WireMock.Net.Tests/Pact/files/pact-multiple.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"Accept": "application/json"
},
"method": "POST",
"path": "/add"
"path": "/add",
"body": {
"Id": "1",
"FirstName": "Totally"
}
},
"response": {
"body": {
Expand Down