diff --git a/lib/response.js b/lib/response.js index 44b0fea..e591ec2 100644 --- a/lib/response.js +++ b/lib/response.js @@ -144,7 +144,7 @@ function generatePayload (response) { if (res.headers['content-type'].indexOf('application/json') < 0) { throw new Error('The content-type of the response is not application/json') } - return JSON.parse(this.payload) + return JSON.parse(res.payload) } return res diff --git a/test/test.js b/test/test.js index e12a39c..b8b019b 100644 --- a/test/test.js +++ b/test/test.js @@ -1312,19 +1312,20 @@ test('chainable api: string url', (t) => { test('Response.json() should parse the JSON payload', (t) => { t.plan(2) - const json = { + const jsonData = { a: 1, b: '2' } const dispatch = function (req, res) { res.writeHead(200, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify(json)) + res.end(JSON.stringify(jsonData)) } inject(dispatch, { method: 'GET', path: 'http://example.com:8080/hello' }, (err, res) => { t.error(err) - t.same(res.json(), json) + const { json } = res + t.same(json(), jsonData) }) })