Search code examples
node.jsmocha.jssupertest

how test for no content, while we have 204 NO CONTENT status, as there is no 'Content-Type' header at all?


It's easy to test Content-Type when having a 200 OK status:

it('should list All permissions on /permissions GET', (done)=> {
    supertest(app)
        .get('/permissions')
        .expect('Content-Type', /json/)
        .end( (err, res)=> {
            // SOME CODE HERE
            done(err)
        })
})

So .expect('Content-Type', /json/) does the job for us.
Now when we have a DELETE request, we want no content to be returned. A status of 204 NO CONTENT without any content.

When there is no content, the Content-Type does not exist. Should we check for the existence of the Content-Type header? How to do that using supertest?

Thanks in advance for any help you're able to provide.


Solution

  • You could use the generic function-based expect() to do a custom assertion if you want to check that a header doesn't exist:

    .expect(function(res) {
      if (res.headers['Content-Type'])
        return new Error('Unexpected content-type!');
    })
    

    If you want to also check that there is no body:

    .expect(204, '')