pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.07k stars 475 forks source link

Spring Boot test with Pactflow as broker always results in a 400 #1242

Closed akullpp closed 3 years ago

akullpp commented 3 years ago

I've verified that the pact can be verified with the Gradle task and a running service but can't seem to make a simple SpringBootTest as it always results in:

Caused by: au.com.dius.pact.core.pactbroker.RequestFailedException: Request to path '/' failed with response 'HTTP/1.1 400 Bad Request'

Here's my test:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = {Application.class})
@Provider("My Provider")
@PactBroker(scheme="https", host = "foo.pactflow.io", port = "443", authentication = @PactBrokerAuth(token = "foo"))
class FooControllerTest {

    @LocalServerPort
    int port;

    @MockBean
    FooRepository repository;

    @BeforeEach
    void setup(PactVerificationContext context) {
        context.setTarget(new HttpTestTarget("localhost", port));
    }

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }

    @State("Should return nothing")
    void shouldReturnNothing() {
        when(repository.findAll()).thenReturn(Collections.emptyList());
    }
}

Here's the log:

20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> GET / HTTP/1.1
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: Bearer foo
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: application/json
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept: application/hal+json, application/json
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: Bearer foo
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: foo.pactflow.io:443
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
20:37:14.266 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.12 (Java/11.0.2)
20:37:14.267 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "GET / HTTP/1.1[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Authorization: Bearer foo[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/json[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept: application/hal+json, application/json[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Authorization: Bearer foo[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: foo.pactflow.io:443[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
20:37:14.267 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.12 (Java/11.0.2)[\r][\n]"
20:37:14.268 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
20:37:14.268 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 400 Bad Request[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: awselb/2.0[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Thu, 05 Nov 2020 19:37:14 GMT[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: text/html[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 122[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "Connection: close[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "<html>[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "<head><title>400 Bad Request</title></head>[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "<body>[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "<center><h1>400 Bad Request</h1></center>[\r][\n]"
20:37:14.567 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "</body>[\r][\n]"
20:37:14.568 [Test worker] DEBUG org.apache.http.wire - http-outgoing-0 << "</html>[\r][\n]"
20:37:14.571 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 400 Bad Request
20:37:14.573 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Server: awselb/2.0
20:37:14.574 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 05 Nov 2020 19:37:14 GMT
20:37:14.574 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/html
20:37:14.574 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 122
20:37:14.575 [Test worker] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: close

Unfortunately there's absolutely no other indication what is wrong and I followed the examples step by step...

uglyog commented 3 years ago

What version of Pact-JVM are you using?

uglyog commented 3 years ago

Version 4.1.10 has been released with a fix for this

akullpp commented 3 years ago

Thanks! :-)