Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase coverage #543

Merged
merged 5 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/normalize-arguments.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
});