chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

Response bodies for anything other than a status 200 is coming back empty

Open fabianTMC opened this issue 7 years ago • 2 comments

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?

fabianTMC avatar Apr 14 '17 06:04 fabianTMC

this is particularly curious on Redirects, which show status 200 but empty response.body :( :( :(

barraponto avatar May 31 '17 18:05 barraponto

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.

liesislukas avatar Oct 13 '17 10:10 liesislukas