forked from pact-foundation/pact-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add example tests with attributes that contain slashes pact-fo…
- Loading branch information
1 parent
c507795
commit 0e41131
Showing
3 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...unit5/src/test/groovy/au/com/dius/pact/consumer/junit5/BodyAttributesWithSlashTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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"}}}}]}' | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
provider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/BodyWithSlashesTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } } } } ] }')) | ||
) | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
provider/junit5/src/test/resources/pacts/Consumer-ProviderWithSlashes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |