Skip to content

Commit

Permalink
fix(regression): HTTP Pact with root json array fails when using unor…
Browse files Browse the repository at this point in the history
…dered array matching #1631
  • Loading branch information
rholshausen committed Jan 4, 2023
1 parent be8d57d commit 639f7f8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package au.com.dius.pact.consumer.junit5;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpResponse;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;

import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonArrayUnordered;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(PactConsumerTestExt.class)
public class ArrayUnorderedTest {
@Test
@PactTestFor(
providerName = "PactProvider",
pactMethod = "pactPassIdArray",
pactVersion = PactSpecVersion.V3)
void testContract(MockServer mockServer) throws IOException {
HttpResponse response = Request.post(mockServer.getUrl() + "/passIdArray")
.bodyString("[{\"id\":\"123\"}]", ContentType.APPLICATION_JSON).execute().returnResponse();
assertEquals(response.getCode(), 200);
}

@Pact(consumer = "PactConsumer")
RequestResponsePact pactPassIdArray(PactDslWithProvider provider) {
return provider
.uponReceiving("publish entity")
.path("/passIdArray")
.method("POST")
.body(
newJsonArrayUnordered(refs ->
refs.object(ref -> ref.stringType("id", "123"))
).build()
)
.willRespondWith()
.status(200)
.toPact();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ abstract class BaseMockServer(val pact: BasePact, val config: MockProviderConfig
), GeneratorTestMode.Consumer)
}
is PartialRequestMatch -> {
logger.error { "PartialRequestMatch: ${matchResult.description()}" }
val interaction = matchResult.problems.keys.first().asSynchronousRequestResponse()!!
mismatchedRequests.putIfAbsent(interaction.request, mutableListOf())
mismatchedRequests[interaction.request]?.add(PactVerificationResult.PartialMismatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ data class ContentTypeMatcher(val contentType: String) : MatchingRule {
*/
object EqualsIgnoreOrderMatcher : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "ignore-order")
override fun canMatch(contentType: ContentType) = true
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V4) {
listOf("Ignore Order matchers can only be used with Pact specification versions >= V4")
Expand All @@ -521,7 +520,6 @@ object EqualsIgnoreOrderMatcher : MatchingRule {
*/
data class MinEqualsIgnoreOrderMatcher(val min: Int) : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "ignore-order", "min" to min)
override fun canMatch(contentType: ContentType) = true
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V4) {
listOf("Ignore Order matchers can only be used with Pact specification versions >= V4")
Expand All @@ -543,7 +541,6 @@ data class MinEqualsIgnoreOrderMatcher(val min: Int) : MatchingRule {
*/
data class MaxEqualsIgnoreOrderMatcher(val max: Int) : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "ignore-order", "max" to max)
override fun canMatch(contentType: ContentType) = true
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V4) {
listOf("Ignore Order matchers can only be used with Pact specification versions >= V4")
Expand All @@ -565,7 +562,6 @@ data class MaxEqualsIgnoreOrderMatcher(val max: Int) : MatchingRule {
*/
data class MinMaxEqualsIgnoreOrderMatcher(val min: Int, val max: Int) : MatchingRule {
override fun toMap(spec: PactSpecVersion) = mapOf("match" to "ignore-order", "min" to min, "max" to max)
override fun canMatch(contentType: ContentType) = true
override fun validateForVersion(pactVersion: PactSpecVersion): List<String> {
return if (pactVersion < PactSpecVersion.V4) {
listOf("Ignore Order matchers can only be used with Pact specification versions >= V4")
Expand Down

0 comments on commit 639f7f8

Please sign in to comment.