MHNOT
07/04/2022, 2:10 PM@Override
protected RequestResponsePact createPact(final PactDslWithProvider aBuilder)
{
final Map<String, String> requestHeaders = ChatTestUtil.createRequestHeaderMap();
return aBuilder//
.given("A text prod finished successfully")//
.uponReceiving("A request to download text")//
.pathFromProviderState("/textresult/${jobId}", "/textresult/" + ChatTestUtil.DUMMY_JOB_ID)//
.method("GET")//
.headers(requestHeaders)
.willRespondWith()//
.status(200)//
.headers(Map.of("Content-Type", "text/plain"))//
.matchHeader("Content-Length", "[1-9][0-9]+")//
.body(PactDslRootValue.stringType())
//.body(PactDslRootValue.stringMatcher(".*", "whatever"))
//.withBinaryData("FooBar".getBytes(), "text/plain")
.toPact();
}
The variant with "*.body(PactDslRootValue.stringType())*" fails with following exception on provider side:
java.lang.AssertionError:
Failures:
1) Verifying a pact between Core_Chat_Client and Chat_Service - A request to download text
1.1) BodyMismatch: / BodyMismatch: Expected body 'string' to match 'Participants:
TestUser
TestUser sent message
16 Oct 2001 10:14 PM +02:00 Hello World
TestUser sent message
16 Oct 2001 10:14 PM +02:00 Lorem Ipsumupczl
' using equality but did not match
The variant with "*.body(PactDslRootValue.stringMatcher(".*", "whatever")*" fails with also on provider side:
java.lang.AssertionError:
Failures:
1) Verifying a pact between Core_Chat_Client and Chat_Service - A request to download text
1.1) BodyMismatch: / BodyMismatch: Expected body 'whatever' to match 'Participants:
TestUser
TestUser sent message
16 Oct 2001 10:14 PM +02:00 Hello World
TestUser sent message
16 Oct 2001 10:14 PM +02:00 Lorem Ipsumphaai
' using regex '.*' but did not match
And the last variant "*.withBinaryData("FooBar".getBytes(), "text/plain")*" fails also:
java.lang.AssertionError:
Failures:
1) Verifying a pact between Core_Chat_Client and Chat_Service - A request to download text
1.1) BodyMismatch: / BodyMismatch: Expected body 'FooBar' to match 'Participants:
TestUser
TestUser sent message
16 Oct 2001 10:14 PM +02:00 Hello World
TestUser sent message
16 Oct 2001 10:14 PM +02:00 Lorem Ipsumxetoj
' using equality but did not match
How can I verify that the body of a response is not empty?uglyog
PactDslRootValue.stringMatcher
is the only one that works on plain text bodies. You should use .+
as the regex as .*
can match an empty string (* means zero or more), and you may need to add the start and end markers.uglyog
^.+$
MHNOT
07/06/2022, 8:34 AMFailures:
1) Verifying a pact between Core_Chat_Client and Chat_Service - A request to download text
1.1) BodyMismatch: / BodyMismatch: Expected body 'whatever' to match 'Hello
World' using regex '^.+$' but did not match
at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactJUnit5VerificationProvider.kt:94)
at foo.ContractVerificationTest.pactVerificationTestTemplate(ContractVerificationTest.java:147)
I attached also the JSON contract.
Should I open a bug for this issue?uglyog
MHNOT
07/07/2022, 10:27 AMMHNOT
08/08/2022, 8:58 AMprotected RequestResponsePact createPact(final PactDslWithProvider aBuilder) {
return aBuilder//
.given("A image prod finished successfully")//
.uponReceiving("A request to download an image")//
.pathFromProviderState("/result/${jobId}", "/result/" + ChatTestUtil.DUMMY_JOB_ID)//
.method("GET")//
.headers(myRequestHeaders)
//
.willRespondWith()//
.status(200)//
.headers(Map.of("Content-Type", "application/pdf"))//
.matchHeader(ChatProductionApi.HEADER_PARAM_PAGE_COUNT, "[0-9]+")//
.body(PactDslRootValue.stringMatcher("^.+$", "whatever"))
The generated pact file looks like this:
{
"consumer": {
"name": "Core_Chat_Client"
},
"interactions": [
{
"description": "A request to download an image",
"providerStates": [
{
"name": "A image prod finished successfully"
}
],
"request": {
"generators": {
"path": {
"dataType": "STRING",
"expression": "/result/${jobId}",
"type": "ProviderState"
}
},
"headers": {
"RM-MDC-TOKEN": "dummyTraceId"
},
"method": "GET",
"path": "/result/dummyJobId"
},
"response": {
"headers": {
"Content-Type": "application/pdf",
"PDF-PAGE-COUNT": "32"
},
"matchingRules": {
"header": {
"PDF-PAGE-COUNT": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9]+"
}
]
}
}
},
"status": 200
}
}
],
"metadata": {
"pact-jvm": {
"version": "4.3.13"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "Chat_Service"
}
}
Failure on provider side:
Failures:
1) Verifying a pact between Core_Chat_Client and Chat_Service - A request to download an image has a matching body
1.1) body: / Actual body 'Hello
World' is not equal to the expected body 'whatever'
java.lang.AssertionError: Core_Chat_Client - Upon A request to download an image
Failures:
1) Verifying a pact between Core_Chat_Client and Chat_Service - A request to download an image has a matching body
1.1) body: / Actual body 'Hello
World' is not equal to the expected body 'whatever'
at au.com.dius.pact.provider.junit5.PactVerificationContext.verifyInteraction(PactVerificationContext.kt:66)
at com.recommind.autotest.ContractVerificationTest.pactVerificationTestTemplate(ContractVerificationTest.java:147)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at ...
The provider simply sent "Hello World" as response body in this test.
How can I validate that the provider sends any data in the response body for mimetypes other than plain text ?uglyog
withBinaryData
, it does exactly what you needMHNOT
08/12/2022, 8:23 AM