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)
+1 always getting “text/html; charset=utf-8” no matter what I do.
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~
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:
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}) })