Skip to content

Commit

Permalink
fix: use correct object when calling json (#185)
Browse files Browse the repository at this point in the history
* fix: use correct object when calling `json`

* add test
  • Loading branch information
SimenB authored Mar 24, 2022
1 parent d121de2 commit fb3cf9a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down

0 comments on commit fb3cf9a

Please sign in to comment.