MHNOT
10/10/2022, 6:24 AMpublic class DownloadEndpointsCDCTest extends ConsumerPactTest
{
@Override
protected RequestResponsePact createPact(final PactDslWithProvider aBuilder)
{
final DslPart expectedBodyResponse1 = PactDslRootValue.stringMatcher("^.+$", "whatever");
final DslPart expectedBodyResponse2 = PactDslRootValue.stringMatcher("^.+$",
"<?xml version=\"1.0\" encoding=\"utf-8\"?><example>foo</example>");
return aBuilder//
.given("A XML generation job finished successfully")
.uponReceiving("A request to download XML")
.pathFromProviderState("/xmlresult/${jobId}", "/xmlresult/" + "dummyJobId")
.method("GET")
.willRespondWith()
.status(200)
.headers(Map.of("Content-Type", "application/xml"))
// .body(expectedBodyResponse1)
.body(expectedBodyResponse2)
.toPact();
}
}
Generated contract:
{
"consumer": {
"name": "XXX_Client"
},
"interactions": [
{
"description": "A request to download a XML",
"providerStates": [
{
"name": "A XML generation finished successfully"
}
],
"request": {
"generators": {
"path": {
"dataType": "STRING",
"expression": "/nativeresult/${jobId}",
"type": "ProviderState"
}
},
"headers": {
"RM-MDC-TOKEN": "dummyTraceId"
},
"method": "GET",
"path": "/xmlresult/dummyJobId"
},
"response": {
"body": "<?xml version=\"1.0\" encoding=\"utf-8\"?><example>foo</example>",
"headers": {
"Content-Type": "application/xml"
},
"matchingRules": {
"body": {
"$": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^.+$"
}
]
}
},
"header": {}
},
"status": 200
}
}
],
"metadata": {
"pact-jvm": {
"version": "4.3.14"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "XXX_Service"
}
}
The verification fails on provider side.
In case we use "*expectedBodyResponse1*" from the above example following exception is thrown (which is comprehensible because the content is not XML):
Content is not allowed in prolog.
at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactVerificationContext.kt:66)
In case we use "*expectedBodyResponse2*" from the above example following exception is thrown:
_Verifying a pact between XXX_Client and XXX_Service - A request to download a XML has a matching body_
body: $.example.#text Expected '' to match '^.+$'
at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactVerificationContext.kt:66)
How can we verify that a response of content type "application/xml" is not empty?
Used versions:
Consumer: au.com.dius.pact.consumerjunit4.3.14
Provider: au.com.dius.pact.providerjunit54.3.13MHNOT
10/13/2022, 2:32 PMMHNOT
10/17/2022, 11:57 AM