Skip to content

Commit

Permalink
Increase coverage (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Aug 3, 2018
1 parent 10d22b7 commit 5c3adba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions 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');
Expand Down
4 changes: 3 additions & 1 deletion source/progress.js
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion 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');
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions source/timed-out.js
Expand Up @@ -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();
}
},
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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());
Expand Down
19 changes: 18 additions & 1 deletion test/stream.js
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});

0 comments on commit 5c3adba

Please sign in to comment.