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.08k stars 479 forks source link

Facing the below error - 1.1) status: expected status of 200 but was 401 1.2) body-content-type: Expected a response type of 'application/json' but the actual type was 'null' #1667

Open Ashwinikumar2002 opened 1 year ago

Ashwinikumar2002 commented 1 year ago

Facing the below error at this point in the Provider contract testing please help. @TestTemplate @ExtendWith(PactVerificationInvocationContextProvider.class) void pactVerificationTestTemplate(PactVerificationContext context) { if (context != null) { context.verifyInteraction(); } }

The error message is below - [INFO] [none] [client: none][trx: none] [http-nio-9090-exec-1] [o.a.c.c.C.[Tomcat].[localhost].[/]] : Initializing Spring DispatcherServlet 'dispatcherServlet'
[INFO] [none] [client: none][trx: none] [http-nio-9090-exec-1] [o.s.web.servlet.DispatcherServlet] : Initializing Servlet 'dispatcherServlet'
[INFO] [none] [client: none][trx: none] [http-nio-9090-exec-1] [o.s.web.servlet.DispatcherServlet] : Completed initialization in 2 ms
returns a response which has status code 200 (FAILED) has a matching body (FAILED)

Failures:

1) Verifying a pact between OrderProcessing and Namor - Pact Verification for Payment initiation: has status code 200

1.1) status: expected status of 200 but was 401

1.2) body-content-type: Expected a response type of 'application/json' but the actual type was 'null'
Ashwinikumar2002 commented 1 year ago

@uglyog pls help

rholshausen commented 1 year ago

The Pact verification test made the request to your provider, expecting a 200 response with a JSON body, but got 401 response instead with no body.

401 is an unauthorized response status. You provider rejected the request because it is unauthenticated.

Please refer to https://github.com/pact-foundation/pact-jvm/tree/master/provider/junit5#modifying-the-requests-before-they-are-sent on how you can add authentication headers to the request.

Ashwinikumar2002 commented 1 year ago

How do I resolve the response body. Also the Authorization is basic auth key. how will I pass it???

Ashwinikumar2002 commented 1 year ago
"Content-Type": "application/json;charset=UTF-8"
"Authorization": "Basic auth_key",

This is the content type and authorization the consumer is sending to my service...

Ashwinikumar2002 commented 1 year ago

I am trying to write the provider driven contract test not consumer driven contract test @rholshausen

rholshausen commented 1 year ago

No sure what that means, but if you are getting a 401 response, then this must be wrong: "Authorization": "Basic auth_key".

Have you tried accessing the API with something like curl or Postman?

Ashwinikumar2002 commented 1 year ago

Yes

Ashwinikumar2002 commented 1 year ago

I put user id and pass

Ashwinikumar2002 commented 1 year ago

Even adding the Httprequest is throwing this error- org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [au.com.dius.pact.consumer.MockServer mockServer]

Ashwinikumar2002 commented 1 year ago

and get a token I guess it is standard springboot auth which we are using.

rholshausen commented 1 year ago

Then that is using Basic Auth. You need the correct header value for that. You can find the correct header in Postman.

I.e. with image

you can find the header value that was used

image

Ashwinikumar2002 commented 1 year ago

Even adding the Httprequest is throwing this error- org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [au.com.dius.pact.consumer.MockServer mockServer]

What about this??

Ashwinikumar2002 commented 1 year ago

@rholshausen

Ashwinikumar2002 commented 1 year ago
    <dependency>
        <groupId>au.com.dius.pact.provider</groupId>
        <artifactId>junit5spring</artifactId>
        <version>4.3.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents.core5</groupId>
        <artifactId>httpcore5</artifactId>
        <version>5.1.2</version>
    </dependency>

This is my dependency

rholshausen commented 1 year ago

That error is possibly due to @ExtendWith(PactVerificationInvocationContextProvider.class) missing on your test template method. Have you setup the test correctly according to the JUnit5 docs?

Ashwinikumar2002 commented 1 year ago

Can you share that doc??

Ashwinikumar2002 commented 1 year ago

That error is possibly due to @ExtendWith(PactVerificationInvocationContextProvider.class) missing on your test template method. Have you setup the test correctly according to the JUnit5 docs?

I have this annotation

rholshausen commented 1 year ago

https://github.com/pact-foundation/pact-jvm/tree/master/provider/junit5spring#pact-springjunit5-support