Skip to content

Commit

Permalink
chore(request_overrider): Update coverage TODOs
Browse files Browse the repository at this point in the history
I worked on this file quite a bit since this comment was written. At this point the remaining work can be broken out into separate, manageable-seeming todos.

Ref #1404
  • Loading branch information
paulmelnikow authored and gr2m committed Sep 4, 2019
1 parent b378b79 commit 92a677a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

// TODO-coverage: This file is puzzling. It would be good to investigate why
// this works the way it does and add more comments. And coverage too!

const { EventEmitter } = require('events')
const { IncomingMessage, ClientRequest } = require('http')
const propagate = require('propagate')
Expand Down Expand Up @@ -289,6 +286,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
timers.setTimeout(emitError, interceptor.getTotalDelay(), error)
return
}
// TODO-coverage: Either add a test or remove `|| 200` if it's not reachable.
response.statusCode = Number(interceptor.statusCode) || 200

// Clone headers/rawHeaders to not override them when evaluating later
Expand Down Expand Up @@ -329,6 +327,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
!common.isStream(interceptor.body)
) {
if (interceptor.delayInMs) {
// TODO-coverage: Add a test of this error case.
emitError(
new Error(
'Response delay is currently not supported with content-encoded responses.'
Expand All @@ -355,6 +354,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
try {
responseBody = Buffer.from(responseBody, 'hex')
} catch (err) {
// TODO-coverage: Add a test of this error case.
debug(
'exception during Buffer construction from hex data:',
responseBody,
Expand All @@ -378,6 +378,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
return continueWithResponseBody(null, responseBody)

function continueWithResponseBody(err, responseBody) {
// TODO-coverage: Try to find out when this happens and add a test.
if (continued) {
return
}
Expand All @@ -397,6 +398,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
if (Array.isArray(responseBody)) {
debug('response body is array: %j', responseBody)

// TODO-coverage: else, throw an error. Add a test.
if (!isNaN(Number(responseBody[0]))) {
response.statusCode = Number(responseBody[0])
}
Expand Down Expand Up @@ -437,6 +439,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
response.push(null)
})
responseBody.on('error', function(err) {
// TODO-coverage: Add a test.
response.emit('error', err)
})
} else if (responseBody && !Buffer.isBuffer(responseBody)) {
Expand Down Expand Up @@ -525,11 +528,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
debug('emitting response')

if (typeof cb === 'function') {
// TODO-coverage: Find out when this happens and add a test.
debug('callback with response')
cb(response)
}

if (req.aborted) {
// TODO-coverage: Add a test of this case.
emitError(new Error('Request aborted'))
} else {
req.emit('response', response)
Expand Down

0 comments on commit 92a677a

Please sign in to comment.