I'm trying to write a contract test to the service:
@PactTestFor(providerName = "java-provider")
@PactFolder("target/pacts")
public class ConsumerPact extends BaseConsumerTest {
@Pact(consumer = "java-consumer")
public RequestResponsePact buildPactShouldReturnNoContent(final PactDslWithProvider builder) {
return builder
.uponReceiving("Service should return no content with status code 204")
.path("/api/v1/service/email")
.headers("Authorization",
Constants.Request.token)
.method("POST")
.body("{\n" +
" \"emails\": [\n" +
" {\n" +
" \"bccRecipients\": \"John Doe <[email protected]>; Jane Doe <[email protected]>\",\n" +
" \"body\": \"body\",\n" +
" \"ccRecipients\": \"John Doe <[email protected]>; Jane Doe <[email protected]>\",\n" +
" \"recipients\": \"John Doe <[email protected]>; Jane Doe <[email protected]>\",\n" +
" \"subject\": \"subject\"\n" +
" }\n" +
" ]\n" +
"}")
.willRespondWith()
.status(NO_CONTENT.value())
.toPact();
}
@Test
@PactTestFor(pactMethod = "buildPactShouldReturnNoContent")
public void shouldReturnNoContent(final MockServer mockServer) throws IOException {
int responseCode = postAuthorizedRequestToMockServer(mockServer, defaultPath);
assertThat(responseCode, is(HttpStatus.SC_NO_CONTENT));
}
}
here is the method thats is used for Authorization:
@ExtendWith(PactConsumerTestExt.class)
public abstract class BaseConsumerTest {
public int postAuthorizedRequestToMockServer(final MockServer mockServer, final String path)
throws IOException {
return Request.Post(mockServer.getUrl() + path)
.addHeader("Authorization", Constants.Request.token)
.execute()
.returnResponse()
.getStatusLine()
.getStatusCode();
}
once i ran the test error message is displayed:
21:20:52.659 [HTTP-Dispatcher] DEBUG au.com.dius.pact.consumer.BaseJdkMockServer - Received request: method: POST
path: /api/v1/service/email
query: {}
headers: {Accept-encoding=[gzip,deflate], Connection=[Keep-Alive], Host=[hub.docker.local:57274], User-agent=[Apache-HttpClient/4.5.10 (Java/11.0.6)], Authorization=[Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Ijg1NThkNmJiZGE2MjkyOWUxMmYwOTQ4ZjNhMDY1NzVhIiwidHlwIjoiSldUIn0.eyJuYmYiOjE1ODcwMzYwODjoxNTg3MDM2MDg2LCJpZHAiOiJsb2NhbCIsInBlb3BsZV91c2VyX2lkIjoiMTE5Iiwic2NvcGUiOlsiZ2NwLWFwaSIsIklkZW50aXR5U2VydmVyQXBpIiwib2ZmbGluZV9hY2Nlc3MiXSwiYW1yIjpbInB3ZCJdfQ.b4o-u6K9mGPxjbuPOTg87OlK7H9LvoTYtW8IxdVlBKZAh8lzzykqrRhZBEi_flu6CEPShZqe5eMWkrqWK0CSP5mjsPyGyq9fSMuruKZYqbTZUpNTlypt5Ms6scbnf9pqV6xiuaulgducL7Y9eUISSmlPO49OTdwf9qWxqXzkaUgGZSPvMkE9jiy1UQo2JGrYcSjRIUV6CAu36D_lCIXu3cklo-W9KOXkbw4gDsaPM5GH-fopkt7UITdpf0QrUsPe-Tc1dDo-UXoaDNlTInSqn3dgkV_RAPC4pbWFEYHjL-Wh1cD4A2_vKogWc2OSn_UqfVkSDBjC4xYeAD6-rJpHJQ], Content-length=[0]}
matchers: MatchingRules(rules={})
generators: Generators(categories={})
body: EMPTY
21:20:52.666 [HTTP-Dispatcher] DEBUG au.com.dius.pact.core.matchers.RequestMatching - comparing to expected request:
method: POST
path: /api/v1/service/email
query: {}
headers: {Authorization=[Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Ijg1NThkNmJiZGE2MjkyOWUxMmYwOTQ4ZjNhMDY1NzVhIiwidHlwIjoiSldUIn0.eyJuYmYiOjE1ODcwMzYwOjRIUV6CAu36D_lCIXu3cklo-W9KOXkbw4gDsaPM5GH-fopkt7UITdpf0QrUsPe-Tc1dDo-UXoaDNlTInSqn3dgkV_RAPC4pbWFEYHjL-Wh1cD4A2_vKogWc2OSn_UqfVkSDBjC4xYeAD6-rJpHJQ]}
matchers: MatchingRules(rules={})
generators: Generators(categories={})
body: PRESENT({
"emails": [
{
"bccRecipients": "John Doe <[email protected]>; Jane Doe <[email protected]>",
"body": "body",
"ccRecipients": "John Doe <[email protected]>; Jane Doe <[email protected]>",
"recipients": "John Doe <[email protected]>; Jane Doe <[email protected]>",
"subject": "subject"
}
]
})
21:20:52.677 [HTTP-Dispatcher] DEBUG au.com.dius.pact.core.matchers.HeaderMatcher - Comparing header 'Authorization': 'Bearer eyJhbGciOiJSUzI1NiIsImUqfVkSDBjC4xYeAD6-rJpHJQ' to 'Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Ijg1NThkNmJiZGE2MjkyOWUxMmYwOTQ4ZjNhMDY1NzVhIiwidHlwIjoiSldUIn0.eyJunB3ZCJdfQ.b4o-u6K9mGPxjbuQrUsPe-Tc1dDo-UXoaDNlTInSqn3dgkV_RAPC4pbWFEYHjL-Wh1cD4A2_vKogWc2OSn_UqfVkSDBjC4xYeAD6-rJpHJQ'
21:20:52.712 [HTTP-Dispatcher] DEBUG au.com.dius.pact.core.matchers.RequestMatching - Request mismatch: [BodyTypeMismatch(expected=application/json, actual=text/plain)]
21:20:52.729 [HTTP-Dispatcher] DEBUG au.com.dius.pact.consumer.BaseJdkMockServer - Generating response: status: 500
headers: {Access-Control-Allow-Origin=[*], Content-Type=[application/json], X-Pact-Unexpected-Request=[1]}
matchers: MatchingRules(rules={})
generators: Generators(categories={})
body: PRESENT({ "error": "Unexpected request : \tmethod: POST\n\tpath: \/api\/v1\/service\/email\n\tquery: {}\n\theaders: {Accept-encoding=[gzip,deflate], Connection=[Keep-Alive], Host=[hub.docker.local:57274], User-agent=[Apache-HttpClient\/4.5.10 (Java\/11.0.6)], Authorization=[Bearer eyJhbGciOiJSUzI1NiIsImqfVkSDBjC4xYeAD6-rJpHJQ], Content-length=[0]}\n\tmatchers: MatchingRules(rules={})\n\tgenerators: Generators(categories={})\n\tbody: EMPTY" })
21:20:52.729 [main] DEBUG org.apache.http.wire - http-outgoing-1 << "HTTP/1.1 500 Internal Server Error[\r][\n]"
i'm not sure why it's complaining
Request mismatch: [BodyTypeMismatch(expected=application/json, actual=text/plain)]
How do i need to pass the body as application/json in my request?
You can either add the content type header, or add the content type as the second parameter to .body.
Most helpful comment
You can either add the content type header, or add the content type as the second parameter to
.body.