Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Create PACT for JSON body that contains only a string #89

Closed
twwd opened this issue May 10, 2022 · 4 comments
Closed

Question: Create PACT for JSON body that contains only a string #89

twwd opened this issue May 10, 2022 · 4 comments
Labels
bug Something isn't working question Further information is requested

Comments

@twwd
Copy link

twwd commented May 10, 2022

❔ Question

How can we generate the PACT JSON file so that it contains a quoted string a body (e.g. "body": "\"EXPIRED\"")?

πŸ’¬ Context

We want to write a PACT consumer test for a back-end API that returns only a single string as response, e.g.

"EXPIRED"

In our consumer, we write

.willRespondWith(status: 410,
                        headers: [HttpHeaders.contentType: "application/json"],
                        body: Matcher.EqualTo("\"EXPIRED\"")

that leads to a successful execution of our test with the client but generates the wrong JSON for that is published to the PACT broker:

Expected

"response": {
        "body": "\"EXPIRED\"",
        "headers": {
          "Content-Type": "application/json"
        },
        "matchingRules": {
          "body": {
            "$": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "equality"
                }
              ]
            }
          }
        },
        "status": 410
      }

Actual

"response": {
        "body": "EXPIRED",
        "headers": {
          "Content-Type": "application/json"
        },
        "matchingRules": {
          "body": {
            "$": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "equality"
                }
              ]
            }
          }
        },
        "status": 410
      }

Since there seems to be an ambiguity in the PACT specification regarding JSON string bodies. If the body is only a string, it is not interpreted as JSON like for objects and arrays but as plain text that is passed to the PACT provider test. Thus, the provider cannot parse the body EXPIRED since it is not valid JSON.

@twwd twwd added the question Further information is requested label May 10, 2022
@surpher
Copy link
Owner

surpher commented May 10, 2022

This seems to be a problem in pact-ffi. I've ran the same code you've shared and enabled all logging. PactSwift sends the following Pact data to pact-ffi:

{"metadata":{"pactSpecification":{"version":"3.0.0"},"pactSwift":{"version":"0.12.0"}},
"interactions":[{"response":{"status":200,"body":"\"EXPIRED\""},"providerState":"elements exist","request":{"path":"\/elements","method":"get"},
"description":"Request for a list"}],"provider":{"name":"unit-test-api-provider"},
"consumer":{"name":"pactswift-unit-tests"}}

Will pass this on to pact-ffi team. Thanks for reporting it.

@surpher
Copy link
Owner

surpher commented May 10, 2022

What possible values are you expecting? Could you use another matcher? Maybe OneOf? It uses a regex as a base and sets the Pact with " around your expected string:

Nevermind... Still have holiday brain πŸ€·β€β™‚οΈ

@surpher
Copy link
Owner

surpher commented Jan 12, 2023

As per pact-foundation/pact-reference#193 (comment) the content type is being set to JSON.
Try setting text/plain fore the response's content type instead?

@surpher
Copy link
Owner

surpher commented Jan 18, 2023

Closing due to no activity on this matter.

@surpher surpher closed this as completed Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants