supertest: Supertest fails on Content-Type

Supertest says the content-type is text/plain, but when I use Postman or Chrome devtools and check the network tab, it correctly shows the content-type as text/html.

describe('Core controller unit tests:', function() {
    before(function(done) {
        request = request('http://localhost:3001');

        done();
    });

    describe('Loading the homepage', function() {
        it('should return 200 from GET /', function(done) {
            request
                .get('/')
                .expect('Content-Type', /html/)
                .expect(200, done);
        });
    });

    after(function(done) {
        done();
    });
});

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 3
  • Comments: 22 (3 by maintainers)

Most upvoted comments

+1 always getting “text/html; charset=utf-8” no matter what I do.

    it('should respond with a 200 if the posted body is valid', function(done) {
        request(app)
            .post('/user/', {
                json: true,
                body: helpers.getUser()
            })
            .set('Accept', 'application/json')
            .expect('Content-Type', /json/)
            .expect(function(response) {
                debugger;
                response.res.json.id.should.eql('123');
            })
            .expect(200)
            .end(done);
    });

app.post('/user/', validate({body: helpers.getUserSchema()}), function(req, res) {
   res.set('Content-Type', 'applicaton/json'); 
   res.json({
        id: '1234'
    });
});

Had this error in an older project using https://www.npmjs.com/package/express-force-https … not a very popular package, but it will cause Express to always return 302 with html/text when using Supertest. With the tests expecting Content-Type of /json/ of course. Easy fix was to not use this package in test env. Commenting here in the off chance it saves someone else several hours of googling and debugging.

it is work to return as a json~

describe('Loading the homepage', function() {  
      it('should return 200 from GET /', function(done) {  
            request  
            .get('/')  
            .set('Content-Type', 'application/json')  
            .expect(200, done);  
       });  
});

You could use a regexp to match your expected content type. /text/html/ would pass your test.

On Tue, Mar 28, 2017 at 2:18 PM, Simo notifications@github.com wrote:

`request(app) .get(‘/’) .expect(‘Content-Type’,‘text/html; charset=utf-8’) .end(done);

});

while work perfectly whereasrequest(app) .get(‘/’) .expect(‘Content-Type’,‘text/html’) .end(done);

});`

won’t work because the http header received adds an encoding of utf-8

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/visionmedia/supertest/issues/187#issuecomment-289738784, or mute the thread https://github.com/notifications/unsubscribe-auth/ABzKA4SGG70kSVqw3EBhJRtGNTpkvr37ks5rqOyVgaJpZM4DFFa6 .

Had the same problem. Just need a little bit of code refactoring.

What solved the problem for me was: Test didn’t pass: notesRouter.get('/', (request, response) => { response.send('<h1>Hello World!</h1>') })

Test passed: notesRouter.get('/', (request, response) => { const message = '<h1>Hello World</h1>' response.json({message}) })