Skip to content

Commit

Permalink
chore: add example tests with attributes that contain slashes pact-fo…
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jun 9, 2022
1 parent c507795 commit 0e41131
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 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.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"}}}}]}'
}
}
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" } } } } ] }'))
)
}
}
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"
}
}

0 comments on commit 0e41131

Please sign in to comment.