In one of my interactions, I stated that the Mock ...
# pact-js
a
In one of my interactions, I stated that the Mock API doesn't return any headers in its response. See below:
Copy code
mockProvider.addInteraction({
        state: "",
        uponReceiving: `An invalid POST request to ${Config.config.SSO_TOKEN_PATH}. Returns 400 status and "invalid_grant_type" message`,
        withRequest: {
          method: "POST",
          path: "/v1/api/token",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
          },
          body: createFormBody(request),
        },
        willRespondWith: {
          status: 400,
          body: {
            error: "unsupported_grant_type",
            error_description:
              "Invalid grant type INCORRECT_GRANT_TYPE, expected authorization_code",
            error_uri: null,
          },
        },
      });
But for some reason, when the pact is generated, a header is added in the interaction in the API response
Copy code
{
  "description": "An invalid POST request to /v1/api/token. Returns 400 status and \"invalid_grant_type\" message",
  "request": {
    "body": "grant_type=INCORRECT_GRANT_TYPE&client_id=34343433434&redirect_uri=REDIRECT_URL&code=AUTH_CODE&code_verifier=VERIFIER",
    "headers": {
      "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    },
    "method": "POST",
    "path": "/v1/api/token"
  },
  "response": {
    "body": {
      "error": "unsupported_grant_type",
      "error_description": "Invalid grant type INCORRECT_GRANT_TYPE, expected authorization_code",
      "error_uri": null
    },
    "headers": {
      "Content-Type": "application/json"
    },
    "status": 400
  }
}
Any ideas on how to stop this from happening
t
Why do you want this? Content-Type is strongly recommended by RFC 7231:
A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message unless the intended media type of the enclosed representation is unknown to the sender
An aside - this statement isn’t quite right:
I stated that the Mock API doesn’t return any headers in its response.
You’ve stated that your client doesn’t care about the headers, not that the response has no headers.
Pact is about verifying that the server sends what you need - see here for the rationale
I see why you might be surprised that content-type is required because you didn’t specify that your client needs it - but since you also specified a JSON payload, Content-Type should at least be some form of
application/json
. Is there some reason you need it not to be?