pact-foundation / pact-reference

Reference implementations for the pact specifications
https://pact.io
MIT License
91 stars 46 forks source link

Rust: JSON post request results in Request-Mismatch with default values #282

Closed joeftiger closed 1 year ago

joeftiger commented 1 year ago

I am trying to use pact-consumer and setup a pact interaction with a POST request and JSON body. However, I am constantly running into an error 500 with "x-pact": "Request-Mismatch".

I broke it down to a simple example on this repo and I do not see what I do wrong.

So far my guess is that using the following macro combination on line 17 is at fault:

i.request
  .post()
  .path("/")
  .json_utf8()
  .json_body(json_pattern!(like!(json!(Data::default()))));

Though I tried various combinations and none seem to work. The aim is to allow any POST request with arbitrary id values (deserialized from the struct, however).

Am I overlooking something or is there a bug inside pact?

mefellows commented 1 year ago

It's because you called json_utf8() which changes the expected content type via content-type header to application/json; charset=utf-8 but reqwest isn't sending it with the UTF charset parameter (just application/json). You just need json_body(...), and removing the utf ones makes the test pass.

When I change the response line to:

.json_body(json_pattern!(like!(json!({}))));

The test errors as you would expect:

thread 'post_test' panicked at 'could not read response body: reqwest::Error { kind: Decode, source: Error("missing field `id`", line: 1, column: 2) }', tests/pact_json.rs:38:46

Side note on Pact framework error messages

I noticed that Pact wasn't printing the actual error message that would have made the underlying problem clear. The real error message is:

  - Mismatch with header 'content-type': Expected 'application/json' to match '^application/json; charset=(utf|UTF)-8$'

Which I think would have helped resolve the issue. I believe this error was being suppressed because of the other assertions before `

The error is only printed when (in this case) pact goes out of scope, however because there are other assertions before that happens I believe that suppresses the real problem - what's the best way to print errors in this case?

The example in the docs would suffer the exact same problem:

Screenshot 2023-06-03 at 9 14 36 am
rholshausen commented 1 year ago

The mock server errors will now be written to standard error if the test is panicking because of another assertion that has failed. image (2)

github-actions[bot] commented 1 year ago

👋 Hi! The 'smartbear-supported' label has just been added to this issue, which will create an internal tracking ticket in PactFlow's Jira (PACT-1067). We will use this to prioritise and assign a team member to this task. All activity will be public on this ticket. For now, sit tight and we'll update this ticket once we have more information on the next steps.

See our documentation for more information.