Skip to content

Commit

Permalink
test(interceptor): Cover encoding error
Browse files Browse the repository at this point in the history
Ref #1404
  • Loading branch information
paulmelnikow committed Feb 2, 2019
1 parent ca00a8c commit 78f3670
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 50 deletions.
1 change: 0 additions & 1 deletion lib/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ Interceptor.prototype.reply = function reply(statusCode, body, rawHeaders) {
this.headers['content-length'] = body.length
}
} catch (err) {
// TODO-coverage: Add a test covering this case.
throw new Error('Error encoding response body into JSON')
}
}
Expand Down
89 changes: 40 additions & 49 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,33 +1180,23 @@ test('two scopes with the same request are consumed', async t => {
scope2.done()
})

test('JSON encoded replies set the content-type header', t => {
const scope = nock('http://localhost')
test('JSON encoded replies set the content-type header', async t => {
const scope = nock('http://example.test')
.get('/')
.reply(200, {
A: 'b',
})

function done(res) {
scope.done()
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/json')
t.end()
}
t.equal(
(await got('http://example.test/')).headers['content-type'],
'application/json'
)

http
.request(
{
host: 'localhost',
path: '/',
},
done
)
.end()
scope.done()
})

test('JSON encoded replies does not overwrite existing content-type header', t => {
const scope = nock('http://localhost')
test('JSON encoded replies does not overwrite existing content-type header', async t => {
const scope = nock('http://example.test')
.get('/')
.reply(
200,
Expand All @@ -1218,44 +1208,45 @@ test('JSON encoded replies does not overwrite existing content-type header', t =
}
)

function done(res) {
scope.done()
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'unicorns')
t.end()
}
t.equal(
(await got('http://example.test/')).headers['content-type'],
'unicorns'
)

http
.request(
{
host: 'localhost',
path: '/',
},
done
)
.end()
scope.done()
})

test("blank response doesn't have content-type application/json attached to it", t => {
nock('http://localhost')
test("blank response doesn't have content-type application/json attached to it", async t => {
const scope = nock('http://example.test')
.get('/')
.reply(200)

function done(res) {
t.equal(res.statusCode, 200)
t.notEqual(res.headers['content-type'], 'application/json')
t.end()
t.equal(
(await got('http://example.test/')).headers['content-type'],
undefined
)

scope.done()
})

test('unencodable object throws the expected error', t => {
const unencodableObject = {
toJSON() {
throw Error('bad!')
},
}

http
.request(
{
host: 'localhost',
path: '/',
},
done
)
.end()
t.throws(
() =>
nock('http://localhost')
.get('/')
.reply(200, unencodableObject),
{
message: 'Error encoding response body into JSON',
}
)

t.end()
})

test('clean all works', t => {
Expand Down

0 comments on commit 78f3670

Please sign in to comment.