From 5c3adba473f4830235f3e6add485b3972c97347f Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Fri, 3 Aug 2018 13:47:32 +0200 Subject: [PATCH] Increase coverage (#543) --- source/normalize-arguments.js | 2 ++ source/progress.js | 4 +++- source/request-as-event-emitter.js | 4 +++- source/timed-out.js | 8 ++++++-- test/stream.js | 19 ++++++++++++++++++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/source/normalize-arguments.js b/source/normalize-arguments.js index bf5ba52c3..7614e6b15 100644 --- a/source/normalize-arguments.js +++ b/source/normalize-arguments.js @@ -1,5 +1,7 @@ 'use strict'; +/* istanbul ignore next: compatibility reason */ const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; // TODO: Use the `URL` global when targeting Node.js 10 +/* istanbul ignore next: compatibility reason */ const URLSearchParamsGlobal = typeof URLSearchParams === 'undefined' ? require('url').URLSearchParams : URLSearchParams; const is = require('@sindresorhus/is'); const toReadableStream = require('to-readable-stream'); diff --git a/source/progress.js b/source/progress.js index cf1821f74..5253925c0 100644 --- a/source/progress.js +++ b/source/progress.js @@ -28,16 +28,18 @@ module.exports = { request.once('socket', socket => { const onSocketConnect = () => { progressInterval = setInterval(() => { + /* istanbul ignore next: hard to test */ if (socket.destroyed) { clearInterval(progressInterval); return; } const lastUploaded = uploaded; + /* istanbul ignore next: see #490 (occurs randomly!) */ const headersSize = request._header ? Buffer.byteLength(request._header) : 0; uploaded = socket.bytesWritten - headersSize; - // Prevent the known issue of `bytesWritten` being larger than body size + /* istanbul ignore next: see https://github.com/sindresorhus/got/pull/322#pullrequestreview-51647813 (no proof) */ if (uploadBodySize && uploaded > uploadBodySize) { uploaded = uploadBodySize; } diff --git a/source/request-as-event-emitter.js b/source/request-as-event-emitter.js index ace016dd7..c8454d36d 100644 --- a/source/request-as-event-emitter.js +++ b/source/request-as-event-emitter.js @@ -1,8 +1,9 @@ 'use strict'; +/* istanbul ignore next: compatibility reason */ +const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; // TODO: Use the `URL` global when targeting Node.js 10 const EventEmitter = require('events'); const http = require('http'); const https = require('https'); -const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; // TODO: Use the `URL` global when targeting Node.js 10 const urlLib = require('url'); const CacheableRequest = require('cacheable-request'); const is = require('@sindresorhus/is'); @@ -38,6 +39,7 @@ module.exports = options => { options.agent = agents[protocolName] || options.agent; } + /* istanbul ignore next: electron.net is broken */ if (options.useElectronNet && process.versions.electron) { const electron = global['require']('electron'); // eslint-disable-line dot-notation fn = electron.net || electron.remote.net; diff --git a/source/timed-out.js b/source/timed-out.js index 1b5e4aa20..89847c9da 100644 --- a/source/timed-out.js +++ b/source/timed-out.js @@ -12,8 +12,8 @@ function addTimeout(delay, callback, ...args) { const timeout = setTimeout( () => { immediate = setImmediate(callback, delay, ...args); + /* istanbul ignore next: added in node v9.7.0 */ if (immediate.unref) { - // Added in node v9.7.0 immediate.unref(); } }, @@ -27,6 +27,7 @@ function addTimeout(delay, callback, ...args) { } module.exports = (request, options) => { + /* istanbul ignore next: this makes sure timed-out isn't called twice */ if (request[reentry]) { return; } @@ -68,6 +69,7 @@ module.exports = (request, options) => { if (delays.lookup !== undefined && !request.socketPath && !net.isIP(hostname || host)) { request.once('socket', socket => { + /* istanbul ignore next: hard to test */ if (socket.connecting) { const cancelTimeout = addTimeout( delays.lookup, @@ -82,6 +84,7 @@ module.exports = (request, options) => { if (delays.connect !== undefined) { request.once('socket', socket => { + /* istanbul ignore next: hard to test */ if (socket.connecting) { const timeConnect = () => { const cancelTimeout = addTimeout( @@ -106,6 +109,7 @@ module.exports = (request, options) => { if (delays.secureConnect !== undefined && options.protocol === 'https:') { request.once('socket', socket => { + /* istanbul ignore next: hard to test */ if (socket.connecting) { socket.once('connect', () => { const cancelTimeout = addTimeout( @@ -131,7 +135,7 @@ module.exports = (request, options) => { cancelers.push(cancelTimeout); return cancelTimeout; }; - + /* istanbul ignore next: hard to test */ if (socket.connecting) { socket.once('connect', () => { request.once('upload-complete', timeRequest()); diff --git a/test/stream.js b/test/stream.js index 6e38f921d..293d477ef 100644 --- a/test/stream.js +++ b/test/stream.js @@ -127,9 +127,10 @@ test('proxying headers works', async t => { await server.listen(server.port); - const {headers} = await got(server.url); + const {headers, body} = await got(server.url); t.is(headers.unicorn, 'rainbow'); t.is(headers['content-encoding'], undefined); + t.is(body, 'ok'); await server.close(); }); @@ -164,3 +165,19 @@ test('throws when trying to proxy through a closed stream', async t => { await got(server.url); await server.close(); }); + +test('proxies content-encoding header when options.decompress is false', async t => { + const server = await createServer(); + + server.on('/', (request, response) => { + got.stream(s.url, {decompress: false}).pipe(response); + }); + + await server.listen(server.port); + + const {headers} = await got(server.url); + t.is(headers.unicorn, 'rainbow'); + t.is(headers['content-encoding'], 'gzip'); + + await server.close(); +});