From 0e411319e208e0249eb9b5c71dadcf06433974ad Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 9 Jun 2022 11:45:10 +1000 Subject: [PATCH] chore: add example tests with attributes that contain slashes #1556 --- .../junit5/BodyAttributesWithSlashTest.groovy | 47 ++++++++ .../junit5/BodyWithSlashesTest.groovy | 43 ++++++++ .../pacts/Consumer-ProviderWithSlashes.json | 100 ++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/BodyAttributesWithSlashTest.groovy create mode 100644 provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/BodyWithSlashesTest.groovy create mode 100644 provider/junit5/src/test/resources/pacts/Consumer-ProviderWithSlashes.json diff --git a/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/BodyAttributesWithSlashTest.groovy b/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/BodyAttributesWithSlashTest.groovy new file mode 100644 index 0000000000..13972ccb8c --- /dev/null +++ b/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/BodyAttributesWithSlashTest.groovy @@ -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.LambdaDsl +import au.com.dius.pact.consumer.dsl.PactDslWithProvider +import au.com.dius.pact.core.model.PactSpecVersion +import au.com.dius.pact.core.model.V4Pact +import au.com.dius.pact.core.model.annotations.Pact +import org.apache.hc.client5.http.fluent.Request +import org.apache.hc.core5.http.ClassicHttpResponse +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(PactConsumerTestExt) +@PactTestFor(providerName = 'ProviderWithSlashes', pactVersion = PactSpecVersion.V4) +class BodyAttributesWithSlashTest { + @Pact(consumer = 'Consumer') + V4Pact pact(PactDslWithProvider builder) { + builder + .uponReceiving('a request for some shipping info') + .path('/shipping/v1') + .willRespondWith() + .status(200) + // '{ "data": [ { "relationships": { "user/shippingAddress": { "data": { "id": "123", "type": "user/shipping-address" } } } } ] }' + .body(LambdaDsl.newJsonBody( body -> { + body.eachLike('data', o -> { + o.object('relationships', r -> { + r.object('user/shippingAddress', addr -> { + addr.object('data', d -> { + d.stringMatcher('id', '\\d+', '123456') + d.stringType('type', 'user/shipping-address') + }) + }) + }) + }) + }).build()) + .toPact(V4Pact) + } + + @Test + void testShippingInfo(MockServer mockServer) { + ClassicHttpResponse httpResponse = Request.get("${mockServer.url}/shipping/v1") + .execute().returnResponse() as ClassicHttpResponse + assert httpResponse.code == 200 + assert httpResponse.entity.content.text == '{"data":[{"relationships":{"user/shippingAddress":{"data":{"id":"123456","type":"user/shipping-address"}}}}]}' + } +} diff --git a/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/BodyWithSlashesTest.groovy b/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/BodyWithSlashesTest.groovy new file mode 100644 index 0000000000..0d131afe84 --- /dev/null +++ b/provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/BodyWithSlashesTest.groovy @@ -0,0 +1,43 @@ +package au.com.dius.pact.provider.junit5 + +import au.com.dius.pact.provider.junitsupport.Provider +import au.com.dius.pact.provider.junitsupport.loader.PactFolder +import com.github.tomakehurst.wiremock.WireMockServer +import groovy.util.logging.Slf4j +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.TestTemplate +import org.junit.jupiter.api.extension.ExtendWith +import ru.lanwen.wiremock.ext.WiremockResolver +import ru.lanwen.wiremock.ext.WiremockUriResolver + +import static com.github.tomakehurst.wiremock.client.WireMock.* + +@Provider('ProviderWithSlashes') +@PactFolder('pacts') +@ExtendWith([ + WiremockResolver, + WiremockUriResolver +]) +@Slf4j +class BodyWithSlashesTest { + + @TestTemplate + @ExtendWith(PactVerificationInvocationContextProvider) + void testTemplate(PactVerificationContext context) { + context.verifyInteraction() + } + + @BeforeEach + void before(PactVerificationContext context, @WiremockResolver.Wiremock WireMockServer server, + @WiremockUriResolver.WiremockUri String uri) throws MalformedURLException { + context.setTarget(HttpTestTarget.fromUrl(new URL(uri))) + + server.stubFor( + get(urlPathEqualTo('/shipping/v1')) + .willReturn(aResponse() + .withStatus(200) + .withHeader('Content-Type', 'application/json') + .withBody('{ "data": [ { "relationships": { "user/shippingAddress": { "data": { "id": "123", "type": "user/shipping-address" } } } } ] }')) + ) + } +} diff --git a/provider/junit5/src/test/resources/pacts/Consumer-ProviderWithSlashes.json b/provider/junit5/src/test/resources/pacts/Consumer-ProviderWithSlashes.json new file mode 100644 index 0000000000..5a2a7fd7fd --- /dev/null +++ b/provider/junit5/src/test/resources/pacts/Consumer-ProviderWithSlashes.json @@ -0,0 +1,100 @@ +{ + "consumer": { + "name": "Consumer" + }, + "interactions": [ + { + "comments": { + "testname": "au.com.dius.pact.consumer.junit5.BodyAttributesWithSlashTest.testShippingInfo(MockServer)", + "text": [ + + ] + }, + "description": "a request for some shipping info", + "key": "23004cfb", + "pending": false, + "request": { + "method": "GET", + "path": "/shipping/v1" + }, + "response": { + "body": { + "content": { + "data": [ + { + "relationships": { + "user/shippingAddress": { + "data": { + "id": "123456", + "type": "user/shipping-address" + } + } + } + } + ] + }, + "contentType": "application/json; charset=UTF-8", + "encoded": false + }, + "headers": { + "Content-Type": [ + "application/json; charset=UTF-8" + ] + }, + "matchingRules": { + "body": { + "$.data": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + }, + "$.data[*].relationships['user/shippingAddress'].data.id": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "\\d+" + } + ] + }, + "$.data[*].relationships['user/shippingAddress'].data.type": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + } + }, + "header": { + "Content-Type": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "application/json(;\\s?charset=[\\w\\-]+)?" + } + ] + } + } + }, + "status": 200 + }, + "type": "Synchronous/HTTP" + } + ], + "metadata": { + "pact-jvm": { + "version": "4.3.10" + }, + "pactSpecification": { + "version": "4.0" + } + }, + "provider": { + "name": "ProviderWithSlashes" + } +}