1

One of my ideas would be to overwrite the request command, but I don't know how to handle the response object. A snippet I already have:

Cypress.Commands.overwrite(
    'request',
    (
        originalFn: Cypress.CommandOriginalFn<'request'>,
        options: Partial<Cypress.RequestOptions>
    ): void | Cypress.Chainable<Cypress.Response<unknown>> => {
        return originalFn(options);
    }
);

My other idea would be to intercept all requests, but there are already interceptors added and you can not have two for one request.

beforeEach(() => {
    cy.intercept(
        {
            url: '*/**',
        },
        req => {
            // tried with 'after:response' too
            req.on('response', res => {
                cy.log(`${res.headers['x-custom-header']}`);
            });
        }
    );
});

Is there any other way to log a custom header value for all request?

meightythree
  • 308
  • 2
  • 16

1 Answers1

1

My final working solution was to add this code to /support/index.ts

beforeEach(() => {
    cy.intercept({ url: '*', middleware: true }, req => {
        req.on('after:response', (res => {
            const customHeaderKey = 'x-custom-header';
            const customHeaderValue = res.headers[customHeaderKey];
            if (customHeaderValue) {
                const message = JSON.stringify({ [customHeaderKey]: customHeaderValue });
                Cypress.log({ message }).finish();
            }
        }));
    });
});
meightythree
  • 308
  • 2
  • 16