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

NDJson support #1495

Open eimjustas opened 2 years ago

eimjustas commented 2 years ago

We're trying to do contract testing for API that provides tada in application/x-ndjson format (line separated jsons). It seems like it's not supported by Pact because errors are thrown if we provide such data in the consumer test.

Versions au.com.dius.pact.consumer:junit5:4.1.29 "au.com.dius.pact" version "4.1.29" plugin

Sample code

  @Pact(consumer = "ExampleApplication", provider = "DataService")
  RequestResponsePact getAllData(PactDslWithProvider builder) {
    return builder.given("data exists")
        .uponReceiving("get all data")
        .method("GET")
        .path("/data")
        .willRespondWith()
        .headers(Map.of("content-type", "application/x-ndjson"))
        .status(200)
        .body("""
            {"data":{"name":"firt","id":10}}
            {"data":{"name":"second","id":11}}
            """, "application/x-ndjson")
        .toPact();
  }

...

    return webClient.get()
        .uri("/data")
        .retrieve()
        .bodyToFlux(new ParameterizedTypeReference<>() {});
  }

Expected result I would like to see the data getting retrieved from web client (works with real API).

Actual result "{"error": Invalid Json document (2:2) - found unexpected characters '{'}" 500 error is returned from mock server.

If content type is set to application/text then mock server works fine, but the code is not able to parse it.

Question Is there any workarounds to make it work or it needs changes in library itself?

mefellows commented 2 years ago

We don't currently support ND JSON, this might be best made as a plugin when the plugin system is stable. It shouldn't be hard, because it should be able to delegate most of the work to the existing matchers/content-type plugin (JSON).

That being said, it's interesting that Pact is trying to parse it as JSON instead of plain text. If that were the case, you'd at least be able to test exact matches.

I'll leave it to the maintainer to respond in due course, but wanted to share this information.