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

Response status and type verification error #856

Open aaguila opened 5 years ago

aaguila commented 5 years ago

Hi,

I have an example project (https://github.com/QAJungle/vertx-testing-example) where I have a Vertx application. I'm doing pact testing.

One of the problems is that when I launch a GetABookByISBNPactTest GetABookByNamePactTest or GetAllBooksPactTest (https://github.com/QAJungle/vertx-testing-example/tree/master/vertx-testing-example-server/src/test/java/com/qajungle/vertx) I obtain the next error, but is random, not alwails fails.

java.lang.AssertionError: 0 - Expected a header 'Content-Type' but was missing 1 - Expected a response type of 'application/json' but the actual type was 'text/plain'

The second error is status assertion error with AddABookPactTest. When I launch tests I obtain the next error:

java.lang.AssertionError: 0 - expected status of 201 but was 200

If you see the pact (https://github.com/QAJungle/vertx-testing-example/blob/master/vertx-testing-example-server/src/test/resources/pacts/add_book_test_consumer-add_book_test_provider.json) and controller code (https://github.com/QAJungle/vertx-testing-example/blob/master/vertx-testing-example-server/src/main/java/com/qajungle/vertx/infrastructure/delivery/vertx/controller/BookController.java#L47) I send 201 status code. In postman I validated it correctly.

You can see the provider and the client in the same repo.

I use: pact-jvm-provider-junit5_2.12 3.6.2 version.

Thanks.

uglyog commented 5 years ago

I don't know much about vert.x, but I can see a difference when running the app versus when the pact test runs.

With the app running:

$ curl -v http://localhost:8080/books/978-0-321-53446-0
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x55c7ea9295c0)
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /books/978-0-321-53446-0 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.64.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< content-type: application/json; charset=utf-8
< content-length: 62
< 
{
  "name" : "Agile Testing",
  "isbn" : "978-0-321-53446-0"
* Connection #0 to host localhost left intact
}

and when the pact test is run with debug logging enabled:

11:43:40.918 [main] DEBUG au.com.dius.pact.provider.junit5.PactVerificationStateChangeExtension - beforeEach for interaction 'GET REQUEST'
11:43:40.919 [main] DEBUG au.com.dius.pact.provider.junit5.PactVerificationStateChangeExtension - Invoking state change method getBookByISBN for state 'a book by ISBN'

Verifying a pact between get_book_by_isbn_test_consumer and get_book_by_isbn_test_provider
  Given a book by ISBN
  GET REQUEST
11:43:41.046 [main] DEBUG au.com.dius.pact.provider.ProviderClient - Making request for provider au.com.dius.pact.provider.ProviderInfo(http, localhost, 8080, /, provider, null, null, null, null, null, false, null, changeit, null, true, false, true, null, [], []):
11:43:41.060 [main] DEBUG au.com.dius.pact.provider.ProviderClient -    method: GET
    path: /books/978-0-321-53446-0
    query: [:]
    headers: [:]
    matchers: MatchingRules(rules={})
    generators: Generators(categories={})
    body: MISSING
11:43:41.205 [vert.x-eventloop-thread-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
11:43:41.205 [vert.x-eventloop-thread-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
11:43:41.205 [vert.x-eventloop-thread-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
11:43:41.205 [vert.x-eventloop-thread-2] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
11:43:41.241 [vert.x-eventloop-thread-2] DEBUG io.netty.handler.codec.compression.ZlibCodecFactory - -Dio.netty.noJdkZlibDecoder: false
11:43:41.241 [vert.x-eventloop-thread-2] DEBUG io.netty.handler.codec.compression.ZlibCodecFactory - -Dio.netty.noJdkZlibEncoder: false
11:43:41.316 [main] DEBUG au.com.dius.pact.provider.ProviderClient - Received response: HTTP/1.1 200 OK
11:43:41.318 [main] DEBUG au.com.dius.pact.provider.ProviderClient - Response: {statusCode=200, headers={content-length=[0]}, contentType=application/json; charset=UTF-8, data=}
    returns a response which
      has status code 200 (OK)
      includes headers
        "Content-Type" with value "application/json; charset=utf-8" (FAILED)
      has a matching body (FAILED)

Failures:

0) Verifying a pact between get_book_by_isbn_test_consumer and get_book_by_isbn_test_provider - GET REQUEST returns a response which includes headers "Content-Type" with value "[application/json; charset=utf-8]"
      Expected a header 'Content-Type' but was missing

1) Verifying a pact between get_book_by_isbn_test_consumer and get_book_by_isbn_test_provider - GET REQUEST returns a response which has a matching body
      comparison -> Expected a response type of 'application/json' but the actual type was 'text/plain'

particularly, the line headers={content-length=[0]}, contentType=application/json; charset=UTF-8, data=} shows that the content type header is not being set correctly. It should be content-Type not contentType.

uglyog commented 5 years ago

Changing VertxPactTest.java:36 to .requestHandler(req -> req.response().putHeader("content-type", "application/json; charset=utf-8").end()) fixes that problem, but the test still fails because no response is being generated.

sbartsa commented 4 years ago

Any solutions to that problem? I am also getting the same issue when doing a GET request with a query param. Any help would be valuable @bethesque Thanks

bethesque commented 4 years ago

Sorry, I'm not the pact-JVM maintainer, and I don't do much Java, so I can't give you any insight into this issue.

kumarkrish85 commented 4 years ago

Any solution for this issue!? facing the same error.

0 - Expected a response type of 'application/json' but the actual type was 'text/plain' 1 - Expected a header 'Content-Type' but was missing

kumarkrish85 commented 4 years ago

@uglyog can you please help?

sbartsa commented 4 years ago

In my case I think it was the messageConverters in the mockMVC target which were not setup correct. (JsonHttpMessageConverter and ProtobufHttpMessageConverter) Also I suggest taking a look at all the pact-jvm dependencies to all align at the same version.

uglyog commented 4 years ago

@kumarkrish85 are you also using a Vertx application?

praneeth459 commented 4 years ago

I am also facing the same issue 0 - Expected a header 'Content-Type' but was missing 1 - Expected a response type of 'application/json' but the actual type was 'text/plain'

uglyog commented 4 years ago

@praneeth459 are you also using a Vertx application?

goelshubham1509 commented 4 years ago

0 - Expected a header 'Content-Type' but was missing 1 - expected status of 200 but was 404 2 - Expected a response type of 'application/json' but the actual type was 'text/plain'