Skip to content

Commit

Permalink
refactor: Remove unnecessary conditional, add test for undefined data
Browse files Browse the repository at this point in the history
See #1404.
  • Loading branch information
RichardLitt authored and gr2m committed Sep 4, 2019
1 parent 98dfe07 commit a8c4ae2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,15 @@ function record(rec_options) {

const oldWrite = req.write
req.write = function(data, encoding) {
// TODO-coverage: Is this for parity with native node `req.write()`? If
// so, reference the native impelementation or its docs. If not, fail
// loudly with an error.
if (typeof data !== 'undefined') {
if (data) {
debug(thisRecordingId, 'new', proto, 'body chunk')
if (!Buffer.isBuffer(data)) {
data = Buffer.from(data, encoding)
}
bodyChunks.push(data)
debug(thisRecordingId, 'new', proto, 'body chunk')
if (!Buffer.isBuffer(data)) {
data = Buffer.from(data, encoding)
}
bodyChunks.push(data)
oldWrite.apply(req, arguments)
} else {
throw new Error('Data was undefined.')
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/test_recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ test('checks if callback is specified', t => {
})
})

test('checks that data is specified', t => {
nock.restore()
nock.recorder.clear()
nock.recorder.rec(true)

const req = http.request({
method: 'POST',
host: 'localhost',
path: '/',
port: '80',
body: undefined,
})

t.throws(() => req.write(), { message: 'Data was undefined.' })
req.abort()
t.end()
})

test('when request body is json, it goes unstringified', t => {
const server = http.createServer((request, response) => response.end())
t.once('end', () => server.close())
Expand Down

0 comments on commit a8c4ae2

Please sign in to comment.