Body is empty
See original GitHub issueHi,There I did some testing on my demo app, find some strange result. My demo app has a healthcheck, it returns with a ‘ack’ in the response (suppose ‘ack’ should be in response’s body). code fragment shows below app.get(‘/demoapp/healthcheck’, function (request, response) { response.writeHead(200, {‘Content-Type’: ‘text/plain’}); response.end(‘ack’); });
Then my test code shows below var should = require(‘should’); var assert = require(‘assert’); var request = require(‘supertest’);
describe(‘Routing’, function() { var url = ‘http://localhost:8080’;
describe(‘demoservice’, function() { it(‘do health check’, function(done) {
request(url)
.get('/demoapp/healthcheck')
.expect(200,function(){
console.log('expect status');
}) //Status code
.end(function(err, res) {
if (err) {
throw err;
}
console.log(res);
console.log(res.text);
console.log(res.body);
res.text.should.have.match(/^ack/);
done();
});
});
}); });
In the console, output is so weird, res shows it’s ‘body’ is {}, but it has ‘text’ ‘ackack’ (not typo,it’s real two ack), is there anybody can tell me why? Thanks in advance.
Issue Analytics
- State:
- Created 10 years ago
- Reactions:2
- Comments:14 (2 by maintainers)
Top GitHub Comments
In my case it turned out that I didn’t set the Content-Type header in responses.
Previously I was calling
res.send(object)
, so the header was set toapplication/json
automatically, but after changing the code to useres.write(...)
andres.end(...)
I had to set it myself. After that, superagent would read the whole chunked response correctly.Looks like a bug when you provide a callback to
expect()
and also.end()
. It ends up calling.end()
internally twice, which is what creates the “ackack” instead of just “ack”.I suggest don’t provide a callback to .
end()
for now.