supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Content-Type header not being properly asserted

Open david-videau-ortega opened this issue 5 months ago • 1 comments

Hello everyone. Thanks for maintaining this awesome tool. I'm facing an issue while trying to test a simple endpoint headers. I would highly appreciate any help you could bring.

Describe the bug

Node.js version: v22.12.0

OS version: Fedora Linux 41

Description:

I just started using supertest today (in its latest version from NPM), with the goal of testing the following simple endpoint:

router.get('/health', (_, response) => {
    response.setHeader('Content-Type', 'text/plain');
    response.end('OK');
})

Actual behavior

Having written the following test:

it('Should return a 200 HTTP status code', async () => {
        const response = await request(application).get('/health');
        expect(response.headers['Content-Type']).toBe('text/plain');
    })

I encounter a failure, because of an unmet expectation on the Content-Type header, in the form of:

Expected: "text/plain"
Received: undefined

However, the following expectation works as expected:

expect(response.headers['content-type']).toBe('text/plain');

Expected behavior

Having verified through Postman that my API returns the correct header:

Image

I expect to be able to assert the Content-Type header, with that exact casing.

Code to reproduce

// My app file
const express = require('express');
const healthcheckRouter = require('./shared/healthcheck/router');


// Server setup

const application = express();

application.use('/health', healthcheckRouter);

module.exports = {
    application
}
// My router file
const express = require('express');

const healthcheckRouter = express.Router();

healthcheckRouter.get('/', (_, response) => {
    response.setHeader('Content-Type', 'text/plain');
    response.end('OK');
})

module.exports = healthcheckRouter;
// My test file
const request = require('supertest');
const { application } = require('../../application');

describe('Health check', () => {

    it('Should return a 200 HTTP status code', async () => {
        const response = await request(application).get('/health');
        expect(response.headers['Content-Type']).toBe('text/plain');
    })

})

As an additional note (knowing that it probably belongs to a separated issue), having verified with Postman that the endpoint provided effectively returns "OK", adding the following assertion:

expect(response.body).toBe('OK')

Fails with the following:

Expected: "OK"
Received: {}

Checklist

  • [ x ] I have searched through GitHub issues for similar issues.
  • [ x ] I have completely read through the README and documentation.
  • [ x ] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

david-videau-ortega avatar May 17 '25 15:05 david-videau-ortega