chai-http
chai-http copied to clipboard
Response bodies for anything other than a status 200 is coming back empty
Hi,
I'm trying to test an API that returns an error message with different status codes when different things go wrong. The problem I have encountered is that when the API response is being set with a valid body such as res.status(422).json({error: "Some message here"}).end();
, the response body still becomes {}
and any test with the following pattern fails.
it('should fail', (done) => {
chai.request(url)
.put('/api/endpoint')
.end((err, res) => {
res.should.have.status(422);
res.body.should.exist && res.body.error.should.exist;
done();
});
});
Is there anything I must do specifically to get res.body
populated in these cases?
this is particularly curious on Redirects, which show status 200 but empty response.body :( :( :(
same issue. I found that response body is hidden in res.response.body
instead of res.body
. Haven't tried with redirects, testing 4xx, 5xx responses.