cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.09k stars 3.19k forks source link

Empty header value not sent in HTTP Request when using Cypress cy.intercept #30588

Open NicholasMantovani opened 2 weeks ago

NicholasMantovani commented 2 weeks ago

Current behavior

I'm working on an end-to-end (E2E) test using Cypress to intercept an HTTP call with the following command:

cy.intercept('GET', 'https://url:port/').as('intercept');

In my Angular application, there’s a requirement to send a specific HTTP header with an empty value. For example:

curl 'http://url:port/' \
  -H 'Currencycode;'

Note: The header (Currencycode) has a semicolon (;) after its name, indicating an empty value.

Issue: When I use Cypress to intercept the request, the header isn't sent. However, if I don't use cy.intercept, the header is sent as expected.

In the dev-tool the header is shown but it's actually not sent.

Desired behavior

The headers should be sent correctly, this seems to be working when using cypress 6.0 that is the first version where intercept is introduced. We also tested the version 12.11 and the issue is present.

Test code to reproduce

Server

const express = require('express');
const fs = require('fs');
const cors = require('cors');
const app = express();
const port = 3000;

app.use(cors());
app.use(express.json());

app.post('/api/test', (req, res) => {
  // Log headers in a file
  fs.appendFileSync('headers.log', JSON.stringify(req.headers, null, 2) + '\n');

  // Response
  res.status(200).json({ message: 'Success' });
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

Html page

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Title</title>
</head>
<body>
  <script>
    const myHeaders = new Headers();
    myHeaders.append("emptyHeader", "");

    const requestOptions = {
        method: "POST",
        headers: myHeaders,
        redirect: "follow"
    };
    fetch('http://localhost:3000/api/test', requestOptions)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error('Error:', error));

  </script>
</body>
</html>

Cypress test

it('test', () => {
    cy.intercept('POST', 'http://localhost:3000/api/test')

    cy.visit('../page.html');
  })

Cypress Version

13.15.0

Node version

v22.9.2

Operating System

Windows 11 Pro 23H2 build: 22631.4317

Debug Logs

No response

Other

No response