0

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.

asedsami
  • 609
  • 7
  • 26

2 Answers2

1

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, '')
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • can i have body without content? i mean the second that you typed in the answer will be enough and there is no need to the custom assertion, right? – asedsami Mar 29 '16 at 04:17
1

When you send an HTTP 204 status code back from a server, there should not be any content in the body. Most server implementations, ie. express, restify will not include a Content-Type header for this status code.

For your tests using supertest you should just check the HTTP status code using an expect. ie:

.expect(204)

Mikelax
  • 572
  • 3
  • 10