xxxxxxxxxx
cy.intercept('POST', '/api/some-endpoint', (req) => {
// Modify the request headers
req.headers['Authorization'] = 'Bearer updatedToken';
// Modify the request body
req.body = {
req.body,
newKey: 'New Value',
};
}).as('postRequest');
// Trigger the POST request
cy.request({
method: 'POST',
url: '/api/some-endpoint',
body: {
existingKey: 'Existing Value',
},
});
// Wait for the intercepted request to complete
cy.wait('@postRequest').then((interception) => {
// You can make assertions on the intercepted request
const request = interception.request;
expect(request.headers['Authorization']).to.equal('Bearer updatedToken');
expect(request.body).to.deep.equal({
existingKey: 'Existing Value',
newKey: 'New Value',
});
});
xxxxxxxxxx
cy.intercept('GET', '/api/some-endpoint', (req, res) => {
// Modify the response status code
res.statusCode = 200;
// Modify the response body
res.body = {
key: 'Updated Value',
};
}).as('getRequest');
// Trigger the GET request
cy.request({
method: 'GET',
url: '/api/some-endpoint',
});
// Wait for the intercepted request to complete
cy.wait('@getRequest').then((interception) => {
// You can make assertions on the intercepted response
const response = interception.response;
expect(response.statusCode).to.equal(200);
expect(response.body).to.deep.equal({
key: 'Updated Value',
});
});