Matías Cárdenas
05/09/2022, 7:21 AMintercept
the endpoint and then use cy.visit
in the test I get the following error:
The `content-type` of the response we received from your web server was:
> `application/json`
This was considered a failure because responses must have `content-type: 'text/html'`
I can set at the endpoint the header { 'Content-Type': 'text/html' }
and the ignore it, in that case it doesn't fail and publish the contract, nevertheless it will fail when checking against the provider as the response is sent as string.
Is there a way to workaround this or I am missing something here?Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
201
created returns a JSON body (and that’s your expectation), the header should be of the correct content typeMatías Cárdenas
05/09/2022, 8:15 AMconst orderResponse = require('../fixtures/created_order.json')
describe('Orders creation', () => {
beforeEach( () => {
cy.intercept(
{
method: 'POST',
url: '/orders/'
},
{
statusCode: 201,
body: orderResponse,
headers: { 'Content-Type': 'text/html' },
},
).as('postOrder')
cy.setupPact('pactflow-example-bi-directional-consumer-cypress', 'pactflow-order-api-postman-provider')
cy.setupPactHeaderBlocklist(['Content-Type'])
})
it.only('Displays created order', () => {
cy.visit({method: 'POST', url: '/orders/'})
})
after(() => {
cy.usePactWait(['postOrder'])
})
})
Matías Cárdenas
05/09/2022, 8:16 AMI’m guess, that if aYes indeed, and that's the issue I am actually having, cypress complains when I want to usecreated returns a JSON body (and that’s your expectation), the header should be of the correct content type201
cy.visit
and to the intercepted endpoint if it is returning application/json
(which is what by default it does when I don't set any content-type
on it 😞Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
headers: { 'Content-Type': 'text/html' },
You want the response type to be HTML?Matt (pactflow.io / pact-js / pact-go)
headers: { 'Content-Type': 'application/json' },
Matías Cárdenas
05/09/2022, 8:17 AMYou want the response type to be HTML?no no, it's the only way I manage to cypress not to complain . If I remove that test won't pass
Matt (pactflow.io / pact-js / pact-go)
Matías Cárdenas
05/09/2022, 8:19 AMMatt (pactflow.io / pact-js / pact-go)
Matías Cárdenas
05/09/2022, 8:19 AMThe `content-type` of the response we received from your web server was:
> `application/json`
This was considered a failure because responses must have `content-type: 'text/html'`
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
cy.visit({method: 'POST', url: '/orders/'})
Why are you “visiting” the API?Matt (pactflow.io / pact-js / pact-go)
POST
to /orders
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
cy.visit
is for visiting web pages, not making API callsMatías Cárdenas
05/09/2022, 8:28 AMWhy are you “visiting” the API?I was doing it because I saw it was being used in the example you shared: https://github.com/pactflow/example-bi-directional-consumer-cypress/
Matías Cárdenas
05/09/2022, 8:28 AMMatías Cárdenas
05/09/2022, 8:29 AMcy.request
but apparently that one ignores intercept
endpoints, it would only work if I add other stubs perhaps with mountebank, but not straightforward like thisMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Matías Cárdenas
05/09/2022, 8:38 AMYeah the example visits a web page, and the web page makes the API call, which is intercepted.oh I see, that is why! I wasn't able to stop the difference haha
Matías Cárdenas
05/09/2022, 8:38 AMMatías Cárdenas
05/09/2022, 8:38 AMMatt (pactflow.io / pact-js / pact-go)
Matías Cárdenas
05/09/2022, 8:41 AMMatt (pactflow.io / pact-js / pact-go)