Skip to content

Commit b504346

Browse files
committedJul 25, 2018
Minor code style tweaks
1 parent 2f43923 commit b504346

34 files changed

+522
-502
lines changed
 

‎source/as-promise.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ module.exports = options => {
1717
cancelOnRequest = true;
1818
});
1919

20-
emitter.on('request', req => {
20+
emitter.on('request', request => {
2121
if (cancelOnRequest) {
22-
req.abort();
22+
request.abort();
2323
}
2424

25-
proxy.emit('request', req);
25+
proxy.emit('request', request);
2626

2727
const uploadComplete = () => {
28-
req.emit('upload-complete');
28+
request.emit('upload-complete');
2929
};
3030

3131
onCancel(() => {
32-
req.abort();
32+
request.abort();
3333
});
3434

3535
if (is.nodeStream(options.body)) {
3636
options.body.once('end', uploadComplete);
37-
options.body.pipe(req);
37+
options.body.pipe(request);
3838
options.body = undefined;
3939
return;
4040
}
4141

42-
req.end(options.body, uploadComplete);
42+
request.end(options.body, uploadComplete);
4343
});
4444

4545
emitter.on('response', async response => {

‎source/as-stream.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,30 @@ module.exports = options => {
2626

2727
const emitter = requestAsEventEmitter(options);
2828

29-
emitter.on('request', req => {
30-
proxy.emit('request', req);
29+
emitter.on('request', request => {
30+
proxy.emit('request', request);
3131
const uploadComplete = () => {
32-
req.emit('upload-complete');
32+
request.emit('upload-complete');
3333
};
3434

3535
if (is.nodeStream(options.body)) {
3636
options.body.once('end', uploadComplete);
37-
options.body.pipe(req);
37+
options.body.pipe(request);
3838
return;
3939
}
4040

4141
if (options.body) {
42-
req.end(options.body, uploadComplete);
42+
request.end(options.body, uploadComplete);
4343
return;
4444
}
4545

4646
if (options.method === 'POST' || options.method === 'PUT' || options.method === 'PATCH') {
4747
input.once('end', uploadComplete);
48-
input.pipe(req);
48+
input.pipe(request);
4949
return;
5050
}
5151

52-
req.end(uploadComplete);
52+
request.end(uploadComplete);
5353
});
5454

5555
emitter.on('response', response => {

‎source/assign-options.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ const extend = require('extend');
22
const is = require('@sindresorhus/is');
33

44
module.exports = (defaults, options = {}) => {
5-
const opts = extend(true, {}, defaults, options);
5+
const returnOptions = extend(true, {}, defaults, options);
66

77
if (Reflect.has(options, 'headers')) {
88
for (const [key, value] of Object.entries(options.headers)) {
99
if (is.nullOrUndefined(value)) {
10-
delete opts.headers[key];
10+
delete returnOptions.headers[key];
1111
}
1212
}
1313
}
1414

1515
// Override these arrays because we don't want to extend them
1616
if (is.object(options.retry)) {
1717
if (Reflect.has(options.retry, 'methods')) {
18-
opts.retry.methods = options.retry.methods;
18+
returnOptions.retry.methods = options.retry.methods;
1919
}
2020

2121
if (Reflect.has(options.retry, 'statusCodes')) {
22-
opts.retry.statusCodes = options.retry.statusCodes;
22+
returnOptions.retry.statusCodes = options.retry.statusCodes;
2323
}
2424
}
2525

26-
return opts;
26+
return returnOptions;
2727
};

‎source/create.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ const asPromise = require('./as-promise');
66
const normalizeArguments = require('./normalize-arguments');
77
const deepFreeze = require('./deep-freeze');
88

9-
const next = options => {
10-
if (options.stream) {
11-
return asStream(options);
12-
}
13-
14-
return asPromise(options);
15-
};
9+
const next = options => options.stream ? asStream(options) : asPromise(options);
1610

1711
const create = defaults => {
1812
if (!defaults.handler) {

‎source/deep-freeze.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22
const is = require('@sindresorhus/is');
33

4-
module.exports = function deepFreeze(obj) {
5-
for (const [key, value] of Object.entries(obj)) {
4+
module.exports = function deepFreeze(object) {
5+
for (const [key, value] of Object.entries(object)) {
66
if (is.object(value)) {
7-
deepFreeze(obj[key]);
7+
deepFreeze(object[key]);
88
}
99
}
1010

11-
return Object.freeze(obj);
11+
return Object.freeze(object);
1212
};

‎source/get-response.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (response, options, emitter, redirects) => {
1414

1515
const percent = downloadBodySize ? downloaded / downloadBodySize : 0;
1616

17-
// Let flush() be responsible for emitting the last event
17+
// Let `flush()` be responsible for emitting the last event
1818
if (percent < 1) {
1919
emitter.emit('downloadProgress', {
2020
percent,

‎source/index.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@ const defaults = {
1414
options: {
1515
retry: {
1616
retries: 2,
17-
methods: ['GET', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'],
18-
statusCodes: [408, 413, 429, 502, 503, 504]
17+
methods: [
18+
'GET',
19+
'PUT',
20+
'HEAD',
21+
'DELETE',
22+
'OPTIONS',
23+
'TRACE'
24+
],
25+
statusCodes: [
26+
408,
27+
413,
28+
429,
29+
502,
30+
503,
31+
504
32+
]
1933
},
2034
cache: false,
2135
decompress: true,

‎source/is-retry-on-network-error-allowed.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const WHITELIST = new Set([
99
'EPIPE'
1010
]);
1111

12-
module.exports = err => {
13-
if (err && WHITELIST.has(err.code)) {
12+
module.exports = error => {
13+
if (error && WHITELIST.has(error.code)) {
1414
return true;
1515
}
1616

‎source/normalize-arguments.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,13 @@ module.exports = (url, options, defaults) => {
204204
if (is.nullOrUndefined(hooks)) {
205205
options.hooks[hookEvent] = [];
206206
} else if (is.array(hooks)) {
207-
hooks.forEach(
208-
(hook, index) => {
209-
if (!is.function_(hook)) {
210-
throw new TypeError(
211-
`Parameter \`hooks.${hookEvent}[${index}]\` must be a function, not ${is(hook)}`
212-
);
213-
}
207+
for (const [index, hook] of hooks.entries()) {
208+
if (!is.function(hook)) {
209+
throw new TypeError(
210+
`Parameter \`hooks.${hookEvent}[${index}]\` must be a function, not ${is(hook)}`
211+
);
214212
}
215-
);
213+
}
216214
} else {
217215
throw new TypeError(`Parameter \`hooks.${hookEvent}\` must be an array, not ${is(hooks)}`);
218216
}

‎source/progress.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
module.exports = {
3-
upload(req, emitter, uploadBodySize) {
3+
upload(request, emitter, uploadBodySize) {
44
const uploadEventFrequency = 150;
55
let uploaded = 0;
66
let progressInterval;
@@ -11,11 +11,11 @@ module.exports = {
1111
total: uploadBodySize
1212
});
1313

14-
req.once('error', () => {
14+
request.once('error', () => {
1515
clearInterval(progressInterval);
1616
});
1717

18-
req.once('response', () => {
18+
request.once('response', () => {
1919
clearInterval(progressInterval);
2020

2121
emitter.emit('uploadProgress', {
@@ -25,7 +25,7 @@ module.exports = {
2525
});
2626
});
2727

28-
req.once('socket', socket => {
28+
request.once('socket', socket => {
2929
const onSocketConnect = () => {
3030
progressInterval = setInterval(() => {
3131
if (socket.destroyed) {
@@ -34,7 +34,7 @@ module.exports = {
3434
}
3535

3636
const lastUploaded = uploaded;
37-
const headersSize = req._header ? Buffer.byteLength(req._header) : 0;
37+
const headersSize = request._header ? Buffer.byteLength(request._header) : 0;
3838
uploaded = socket.bytesWritten - headersSize;
3939

4040
// Prevent the known issue of `bytesWritten` being larger than body size

‎source/request-as-event-emitter.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ module.exports = (options = {}) => {
106106
}
107107
});
108108

109-
cacheReq.once('request', req => {
109+
cacheReq.once('request', request => {
110110
let aborted = false;
111-
req.once('abort', _ => {
111+
request.once('abort', _ => {
112112
aborted = true;
113113
});
114114

115-
req.once('error', error => {
115+
request.once('error', error => {
116116
if (aborted) {
117117
return;
118118
}
@@ -127,13 +127,13 @@ module.exports = (options = {}) => {
127127
});
128128
});
129129

130-
progress.upload(req, emitter, uploadBodySize);
130+
progress.upload(request, emitter, uploadBodySize);
131131

132132
if (options.gotTimeout) {
133-
timedOut(req, options);
133+
timedOut(request, options);
134134
}
135135

136-
emitter.emit('request', req);
136+
emitter.emit('request', request);
137137
});
138138
};
139139

‎source/timed-out.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,25 @@ function addTimeout(delay, callback, ...args) {
2626
};
2727
}
2828

29-
module.exports = function (req, options) {
30-
if (req[reentry]) {
29+
module.exports = (request, options) => {
30+
if (request[reentry]) {
3131
return;
3232
}
33-
req[reentry] = true;
33+
34+
request[reentry] = true;
3435
const {gotTimeout: delays, host, hostname} = options;
3536
const timeoutHandler = (delay, event) => {
36-
req.abort();
37-
req.emit('error', new TimeoutError(delay, event, options));
37+
request.abort();
38+
request.emit('error', new TimeoutError(delay, event, options));
3839
};
40+
3941
const cancelers = [];
4042
const cancelTimeouts = () => {
4143
cancelers.forEach(cancelTimeout => cancelTimeout());
4244
};
4345

44-
req.on('error', cancelTimeouts);
45-
req.once('response', response => {
46+
request.on('error', cancelTimeouts);
47+
request.once('response', response => {
4648
response.once('end', cancelTimeouts);
4749
});
4850

@@ -54,16 +56,18 @@ module.exports = function (req, options) {
5456
);
5557
cancelers.push(cancelTimeout);
5658
}
59+
5760
if (delays.socket !== undefined) {
58-
req.setTimeout(
61+
request.setTimeout(
5962
delays.socket,
6063
() => {
6164
timeoutHandler(delays.socket, 'socket');
6265
}
6366
);
6467
}
65-
if (delays.lookup !== undefined && !req.socketPath && !net.isIP(hostname || host)) {
66-
req.once('socket', socket => {
68+
69+
if (delays.lookup !== undefined && !request.socketPath && !net.isIP(hostname || host)) {
70+
request.once('socket', socket => {
6771
if (socket.connecting) {
6872
const cancelTimeout = addTimeout(
6973
delays.lookup,
@@ -75,8 +79,9 @@ module.exports = function (req, options) {
7579
}
7680
});
7781
}
82+
7883
if (delays.connect !== undefined) {
79-
req.once('socket', socket => {
84+
request.once('socket', socket => {
8085
if (socket.connecting) {
8186
const timeConnect = () => {
8287
const cancelTimeout = addTimeout(
@@ -87,7 +92,8 @@ module.exports = function (req, options) {
8792
cancelers.push(cancelTimeout);
8893
return cancelTimeout;
8994
};
90-
if (req.socketPath || net.isIP(hostname || host)) {
95+
96+
if (request.socketPath || net.isIP(hostname || host)) {
9197
socket.once('connect', timeConnect());
9298
} else {
9399
socket.once('lookup', () => {
@@ -97,8 +103,9 @@ module.exports = function (req, options) {
97103
}
98104
});
99105
}
106+
100107
if (delays.send !== undefined) {
101-
req.once('socket', socket => {
108+
request.once('socket', socket => {
102109
const timeRequest = () => {
103110
const cancelTimeout = addTimeout(
104111
delays.send,
@@ -108,24 +115,26 @@ module.exports = function (req, options) {
108115
cancelers.push(cancelTimeout);
109116
return cancelTimeout;
110117
};
118+
111119
if (socket.connecting) {
112120
socket.once('connect', () => {
113-
req.once('upload-complete', timeRequest());
121+
request.once('upload-complete', timeRequest());
114122
});
115123
} else {
116-
req.once('upload-complete', timeRequest());
124+
request.once('upload-complete', timeRequest());
117125
}
118126
});
119127
}
128+
120129
if (delays.response !== undefined) {
121-
req.once('upload-complete', () => {
130+
request.once('upload-complete', () => {
122131
const cancelTimeout = addTimeout(
123132
delays.response,
124133
timeoutHandler,
125134
'response'
126135
);
127136
cancelers.push(cancelTimeout);
128-
req.once('response', cancelTimeout);
137+
request.once('response', cancelTimeout);
129138
});
130139
}
131140
};

‎test/agent.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,39 @@ test.before('setup', async () => {
4242

4343
// HTTPS Handlers
4444

45-
https.on('/', (req, res) => {
46-
res.end('https');
45+
https.on('/', (request, response) => {
46+
response.end('https');
4747
});
4848

49-
https.on('/httpsToHttp', (req, res) => {
50-
res.writeHead(302, {
49+
https.on('/httpsToHttp', (request, response) => {
50+
response.writeHead(302, {
5151
location: http.url
5252
});
53-
res.end();
53+
response.end();
5454
});
5555

5656
// HTTP Handlers
5757

58-
http.on('/', (req, res) => {
59-
res.end('http');
58+
http.on('/', (request, response) => {
59+
response.end('http');
6060
});
6161

62-
http.on('/httpToHttps', (req, res) => {
63-
res.writeHead(302, {
62+
http.on('/httpToHttps', (request, response) => {
63+
response.writeHead(302, {
6464
location: https.url
6565
});
66-
res.end();
66+
response.end();
6767
});
6868

6969
await http.listen(http.port);
7070
await https.listen(https.port);
7171
});
7272

73+
test.after('cleanup', async () => {
74+
await http.close();
75+
await https.close();
76+
});
77+
7378
const createAgentSpy = Cls => {
7479
const agent = new Cls({keepAlive: true});
7580
const spy = sinon.spy(agent, 'addRequest');
@@ -161,8 +166,3 @@ test('socket connect listener cleaned up after request', async t => {
161166
// Make sure to close all open sockets
162167
agent.destroy();
163168
});
164-
165-
test.after('cleanup', async () => {
166-
await http.close();
167-
await https.close();
168-
});

‎test/arguments.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,38 @@ let s;
99
test.before('setup', async () => {
1010
s = await createServer();
1111

12-
s.on('/', (req, res) => {
13-
res.statusCode = 404;
14-
res.end();
12+
s.on('/', (request, response) => {
13+
response.statusCode = 404;
14+
response.end();
1515
});
1616

17-
s.on('/test', (req, res) => {
18-
res.end(req.url);
17+
s.on('/test', (request, response) => {
18+
response.end(request.url);
1919
});
2020

21-
s.on('/?test=wow', (req, res) => {
22-
res.end(req.url);
21+
s.on('/?test=wow', (request, response) => {
22+
response.end(request.url);
2323
});
2424

25-
s.on('/stream', (req, res) => {
26-
res.end('ok');
25+
s.on('/stream', (request, response) => {
26+
response.end('ok');
2727
});
2828

2929
await s.listen(s.port);
3030
});
3131

32+
test.after('cleanup', async () => {
33+
await s.close();
34+
});
35+
3236
test('url is required', async t => {
33-
const err = await t.throws(got());
34-
t.regex(err.message, /Parameter `url` must be a string or object, not undefined/);
37+
const error = await t.throws(got());
38+
t.regex(error.message, /Parameter `url` must be a string or object, not undefined/);
3539
});
3640

3741
test('url should be utf-8 encoded', async t => {
38-
const err = await t.throws(got(`${s.url}/%D2%E0%EB%EB%E8%ED`));
39-
t.regex(err.message, /Parameter `url` must contain valid UTF-8 character sequences/);
42+
const error = await t.throws(got(`${s.url}/%D2%E0%EB%EB%E8%ED`));
43+
t.regex(error.message, /Parameter `url` must contain valid UTF-8 character sequences/);
4044
});
4145

4246
test('options are optional', async t => {
@@ -78,8 +82,8 @@ test('overrides querystring from opts', async t => {
7882
});
7983

8084
test('should throw with auth in url string', async t => {
81-
const err = await t.throws(got('https://test:45d3ps453@account.myservice.com/api/token'));
82-
t.regex(err.message, /Basic authentication must be done with the `auth` option/);
85+
const error = await t.throws(got('https://test:45d3ps453@account.myservice.com/api/token'));
86+
t.regex(error.message, /Basic authentication must be done with the `auth` option/);
8387
});
8488

8589
test('does not throw with auth in url object', async t => {
@@ -148,7 +152,3 @@ test('throws TypeError when known `hooks` array item is not a function', async t
148152
test('allows extra keys in `hooks`', async t => {
149153
await t.notThrows(() => got(`${s.url}/test`, {hooks: {extra: {}}}));
150154
});
151-
152-
test.after('cleanup', async () => {
153-
await s.close();
154-
});

‎test/cache.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,48 @@ test.before('setup', async () => {
88
s = await createServer();
99

1010
let noStoreIndex = 0;
11-
s.on('/no-store', (req, res) => {
12-
res.setHeader('Cache-Control', 'public, no-cache, no-store');
13-
res.end(noStoreIndex.toString());
11+
s.on('/no-store', (request, response) => {
12+
response.setHeader('Cache-Control', 'public, no-cache, no-store');
13+
response.end(noStoreIndex.toString());
1414
noStoreIndex++;
1515
});
1616

1717
let cacheIndex = 0;
18-
s.on('/cache', (req, res) => {
19-
res.setHeader('Cache-Control', 'public, max-age=60');
20-
res.end(cacheIndex.toString());
18+
s.on('/cache', (request, response) => {
19+
response.setHeader('Cache-Control', 'public, max-age=60');
20+
response.end(cacheIndex.toString());
2121
cacheIndex++;
2222
});
2323

2424
let status301Index = 0;
25-
s.on('/301', (req, res) => {
25+
s.on('/301', (request, response) => {
2626
if (status301Index === 0) {
27-
res.setHeader('Cache-Control', 'public, max-age=60');
28-
res.setHeader('Location', s.url + '/302');
29-
res.statusCode = 301;
27+
response.setHeader('Cache-Control', 'public, max-age=60');
28+
response.setHeader('Location', `${s.url}/302`);
29+
response.statusCode = 301;
3030
}
31-
res.end();
31+
response.end();
3232
status301Index++;
3333
});
3434

3535
let status302Index = 0;
36-
s.on('/302', (req, res) => {
36+
s.on('/302', (request, response) => {
3737
if (status302Index === 0) {
38-
res.setHeader('Cache-Control', 'public, max-age=60');
39-
res.setHeader('Location', s.url + '/cache');
40-
res.statusCode = 302;
38+
response.setHeader('Cache-Control', 'public, max-age=60');
39+
response.setHeader('Location', `${s.url}/cache`);
40+
response.statusCode = 302;
4141
}
42-
res.end();
42+
response.end();
4343
status302Index++;
4444
});
4545

4646
await s.listen(s.port);
4747
});
4848

49+
test.after('cleanup', async () => {
50+
await s.close();
51+
});
52+
4953
test('Non cacheable responses are not cached', async t => {
5054
const endpoint = '/no-store';
5155
const cache = new Map();
@@ -98,10 +102,6 @@ test('Cache error throws got.CacheError', async t => {
98102
const endpoint = '/no-store';
99103
const cache = {};
100104

101-
const err = await t.throws(got(s.url + endpoint, {cache}));
102-
t.is(err.name, 'CacheError');
103-
});
104-
105-
test.after('cleanup', async () => {
106-
await s.close();
105+
const error = await t.throws(got(s.url + endpoint, {cache}));
106+
t.is(error.name, 'CacheError');
107107
});

‎test/cancel.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ async function createAbortServer() {
1010
const s = await createServer();
1111
const ee = new EventEmitter();
1212
ee.aborted = new Promise((resolve, reject) => {
13-
s.on('/abort', async (req, res) => {
13+
s.on('/abort', async (request, response) => {
1414
ee.emit('connection');
15-
req.on('aborted', resolve);
16-
res.on('finish', reject.bind(null, new Error('Request finished instead of aborting.')));
15+
request.on('aborted', resolve);
16+
response.on('finish', reject.bind(null, new Error('Request finished instead of aborting.')));
1717

18-
await getStream(req);
19-
res.end();
18+
await getStream(request);
19+
response.end();
2020
});
2121

22-
s.on('/redirect', (req, res) => {
23-
res.writeHead(302, {
22+
s.on('/redirect', (request, response) => {
23+
response.writeHead(302, {
2424
location: `${s.url}/abort`
2525
});
26-
res.end();
26+
response.end();
2727

2828
ee.emit('sentRedirect');
2929

@@ -100,9 +100,9 @@ test('cancel immediately', async t => {
100100
const aborted = new Promise((resolve, reject) => {
101101
// We won't get an abort or even a connection
102102
// We assume no request within 1000ms equals a (client side) aborted request
103-
s.on('/abort', (req, res) => {
104-
res.on('finish', reject.bind(this, new Error('Request finished instead of aborting.')));
105-
res.end();
103+
s.on('/abort', (request, response) => {
104+
response.on('finish', reject.bind(this, new Error('Request finished instead of aborting.')));
105+
response.end();
106106
});
107107
setTimeout(resolve, 1000);
108108
});

‎test/create.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ let s;
77
test.before('setup', async () => {
88
s = await createServer();
99

10-
s.on('/', (req, res) => {
11-
req.resume();
12-
res.end(JSON.stringify(req.headers));
10+
s.on('/', (request, response) => {
11+
request.resume();
12+
response.end(JSON.stringify(request.headers));
1313
});
1414

1515
await s.listen(s.port);

‎test/error.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,55 @@ let s;
1111
test.before('setup', async () => {
1212
s = await createServer();
1313

14-
s.on('/', (req, res) => {
15-
res.statusCode = 404;
16-
res.end('not');
14+
s.on('/', (request, response) => {
15+
response.statusCode = 404;
16+
response.end('not');
1717
});
1818

19-
s.on('/default-status-message', (req, res) => {
20-
res.statusCode = 400;
21-
res.end('body');
19+
s.on('/default-status-message', (request, response) => {
20+
response.statusCode = 400;
21+
response.end('body');
2222
});
2323

24-
s.on('/custom-status-message', (req, res) => {
25-
res.statusCode = 400;
26-
res.statusMessage = 'Something Exploded';
27-
res.end('body');
24+
s.on('/custom-status-message', (request, response) => {
25+
response.statusCode = 400;
26+
response.statusMessage = 'Something Exploded';
27+
response.end('body');
2828
});
2929

30-
s.on('/body', async (req, res) => {
31-
const body = await getStream(req);
32-
res.end(body);
30+
s.on('/body', async (request, response) => {
31+
const body = await getStream(request);
32+
response.end(body);
3333
});
3434

3535
await s.listen(s.port);
3636
});
3737

38+
test.after('cleanup', async () => {
39+
await s.close();
40+
});
41+
3842
test('properties', async t => {
39-
const err = await t.throws(got(s.url));
40-
t.truthy(err);
41-
t.truthy(err.response);
42-
t.false({}.propertyIsEnumerable.call(err, 'response'));
43-
t.false({}.hasOwnProperty.call(err, 'code'));
44-
t.is(err.message, 'Response code 404 (Not Found)');
45-
t.is(err.host, `${s.host}:${s.port}`);
46-
t.is(err.method, 'GET');
47-
t.is(err.protocol, 'http:');
48-
t.is(err.url, err.response.requestUrl);
49-
t.is(err.headers.connection, 'close');
50-
t.is(err.response.body, 'not');
43+
const error = await t.throws(got(s.url));
44+
t.truthy(error);
45+
t.truthy(error.response);
46+
t.false({}.propertyIsEnumerable.call(error, 'response'));
47+
t.false({}.hasOwnProperty.call(error, 'code'));
48+
t.is(error.message, 'Response code 404 (Not Found)');
49+
t.is(error.host, `${s.host}:${s.port}`);
50+
t.is(error.method, 'GET');
51+
t.is(error.protocol, 'http:');
52+
t.is(error.url, error.response.requestUrl);
53+
t.is(error.headers.connection, 'close');
54+
t.is(error.response.body, 'not');
5155
});
5256

5357
test('dns message', async t => {
54-
const err = await t.throws(got('.com', {retry: 0}));
55-
t.truthy(err);
56-
t.regex(err.message, /getaddrinfo ENOTFOUND/);
57-
t.is(err.host, '.com');
58-
t.is(err.method, 'GET');
58+
const error = await t.throws(got('.com', {retry: 0}));
59+
t.truthy(error);
60+
t.regex(error.message, /getaddrinfo ENOTFOUND/);
61+
t.is(error.host, '.com');
62+
t.is(error.method, 'GET');
5963
});
6064

6165
test('options.body error message', async t => {
@@ -87,15 +91,15 @@ test('no plain object restriction on body', async t => {
8791
});
8892

8993
test('default status message', async t => {
90-
const err = await t.throws(got(`${s.url}/default-status-message`));
91-
t.is(err.statusCode, 400);
92-
t.is(err.statusMessage, 'Bad Request');
94+
const error = await t.throws(got(`${s.url}/default-status-message`));
95+
t.is(error.statusCode, 400);
96+
t.is(error.statusMessage, 'Bad Request');
9397
});
9498

9599
test('custom status message', async t => {
96-
const err = await t.throws(got(`${s.url}/custom-status-message`));
97-
t.is(err.statusCode, 400);
98-
t.is(err.statusMessage, 'Something Exploded');
100+
const error = await t.throws(got(`${s.url}/custom-status-message`));
101+
t.is(error.statusCode, 400);
102+
t.is(error.statusMessage, 'Something Exploded');
99103
});
100104

101105
test.serial('http.request error', async t => {
@@ -118,7 +122,3 @@ test.serial('catch error in mimicResponse', async t => {
118122

119123
await t.throws(proxiedGot(s.url), {message: 'Error in mimic-response'});
120124
});
121-
122-
test.after('cleanup', async () => {
123-
await s.close();
124-
});

‎test/gzip.js

+36-36
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,46 @@ test.before('setup', async () => {
1515
s = await createServer();
1616
gzipData = await util.promisify(zlib.gzip)(testContent);
1717

18-
s.on('/', (req, res) => {
19-
res.statusCode = 200;
20-
res.setHeader('Content-Type', 'text/plain');
21-
res.setHeader('Content-Encoding', 'gzip');
18+
s.on('/', (request, response) => {
19+
response.statusCode = 200;
20+
response.setHeader('Content-Type', 'text/plain');
21+
response.setHeader('Content-Encoding', 'gzip');
2222

23-
if (req.method === 'HEAD') {
24-
res.end();
23+
if (request.method === 'HEAD') {
24+
response.end();
2525
return;
2626
}
2727

28-
res.end(gzipData);
28+
response.end(gzipData);
2929
});
3030

31-
s.on('/corrupted', (req, res) => {
32-
res.statusCode = 200;
33-
res.setHeader('Content-Type', 'text/plain');
34-
res.setHeader('Content-Encoding', 'gzip');
35-
res.end('Not gzipped content');
31+
s.on('/corrupted', (request, response) => {
32+
response.statusCode = 200;
33+
response.setHeader('Content-Type', 'text/plain');
34+
response.setHeader('Content-Encoding', 'gzip');
35+
response.end('Not gzipped content');
3636
});
3737

38-
s.on('/missing-data', (req, res) => {
39-
res.statusCode = 200;
40-
res.setHeader('Content-Type', 'text/plain');
41-
res.setHeader('Content-Encoding', 'gzip');
42-
res.end(gzipData.slice(0, -1));
38+
s.on('/missing-data', (request, response) => {
39+
response.statusCode = 200;
40+
response.setHeader('Content-Type', 'text/plain');
41+
response.setHeader('Content-Encoding', 'gzip');
42+
response.end(gzipData.slice(0, -1));
4343
});
4444

45-
s.on('/uncompressed', (req, res) => {
46-
res.statusCode = 200;
47-
res.setHeader('Content-Type', 'text/plain');
48-
res.end(testContentUncompressed);
45+
s.on('/uncompressed', (request, response) => {
46+
response.statusCode = 200;
47+
response.setHeader('Content-Type', 'text/plain');
48+
response.end(testContentUncompressed);
4949
});
5050

5151
await s.listen(s.port);
5252
});
5353

54+
test.after('cleanup', async () => {
55+
await s.close();
56+
});
57+
5458
test('decompress content', async t => {
5559
t.is((await got(s.url)).body, testContent);
5660
});
@@ -60,17 +64,17 @@ test('decompress content - stream', async t => {
6064
});
6165

6266
test('handles gzip error', async t => {
63-
const err = await t.throws(got(`${s.url}/corrupted`));
64-
t.is(err.message, 'incorrect header check');
65-
t.is(err.path, '/corrupted');
66-
t.is(err.name, 'ReadError');
67+
const error = await t.throws(got(`${s.url}/corrupted`));
68+
t.is(error.message, 'incorrect header check');
69+
t.is(error.path, '/corrupted');
70+
t.is(error.name, 'ReadError');
6771
});
6872

6973
test('handles gzip error - stream', async t => {
70-
const err = await t.throws(getStream(got.stream(`${s.url}/corrupted`)));
71-
t.is(err.message, 'incorrect header check');
72-
t.is(err.path, '/corrupted');
73-
t.is(err.name, 'ReadError');
74+
const error = await t.throws(getStream(got.stream(`${s.url}/corrupted`)));
75+
t.is(error.message, 'incorrect header check');
76+
t.is(error.path, '/corrupted');
77+
t.is(error.name, 'ReadError');
7478
});
7579

7680
test('decompress option opts out of decompressing', async t => {
@@ -96,11 +100,7 @@ test('ignore missing data', async t => {
96100
});
97101

98102
test('has url and requestUrl properties', async t => {
99-
const res = await got(s.url);
100-
t.truthy(res.url);
101-
t.truthy(res.requestUrl);
102-
});
103-
104-
test.after('cleanup', async () => {
105-
await s.close();
103+
const response = await got(s.url);
104+
t.truthy(response.url);
105+
t.truthy(response.requestUrl);
106106
});

‎test/headers.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ let s;
1212
test.before('setup', async () => {
1313
s = await createServer();
1414

15-
s.on('/', (req, res) => {
16-
req.resume();
17-
res.end(JSON.stringify(req.headers));
15+
s.on('/', (request, response) => {
16+
request.resume();
17+
response.end(JSON.stringify(request.headers));
1818
});
1919

2020
await s.listen(s.port);
2121
});
2222

23+
test.after('cleanup', async () => {
24+
await s.close();
25+
});
26+
2327
test('user-agent', async t => {
2428
const headers = (await got(s.url, {json: true})).body;
2529
t.is(headers['user-agent'], `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`);
@@ -147,7 +151,3 @@ test('remove undefined value headers', async t => {
147151
const headers = JSON.parse(body);
148152
t.false(Reflect.has(headers, 'user-agent'));
149153
});
150-
151-
test.after('cleanup', async () => {
152-
await s.close();
153-
});

‎test/helpers.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ let s;
77
test.before('setup', async () => {
88
s = await createServer();
99

10-
s.on('/', (req, res) => {
11-
res.end('ok');
10+
s.on('/', (request, response) => {
11+
response.end('ok');
1212
});
1313

14-
s.on('/404', (req, res) => {
15-
res.statusCode = 404;
16-
res.end('not found');
14+
s.on('/404', (request, response) => {
15+
response.statusCode = 404;
16+
response.end('not found');
1717
});
1818

1919
await s.listen(s.port);
2020
});
2121

22+
test.after('cleanup', async () => {
23+
await s.close();
24+
});
25+
2226
test('promise mode', async t => {
2327
t.is((await got.get(s.url)).body, 'ok');
2428

25-
const err = await t.throws(got.get(`${s.url}/404`));
26-
t.is(err.response.body, 'not found');
29+
const error = await t.throws(got.get(`${s.url}/404`));
30+
t.is(error.response.body, 'not found');
2731

28-
const err2 = await t.throws(got.get('.com', {retry: 0}));
29-
t.truthy(err2);
30-
});
31-
32-
test.after('cleanup', async () => {
33-
await s.close();
32+
const error2 = await t.throws(got.get('.com', {retry: 0}));
33+
t.truthy(error2);
3434
});

‎test/hooks.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ let s;
77

88
test.before('setup', async () => {
99
s = await createServer();
10-
const echoHeaders = (req, res) => {
11-
res.statusCode = 200;
12-
res.write(JSON.stringify(req.headers));
13-
res.end();
10+
const echoHeaders = (request, response) => {
11+
response.statusCode = 200;
12+
response.write(JSON.stringify(request.headers));
13+
response.end();
1414
};
1515
s.on('/', echoHeaders);
1616
await s.listen(s.port);
1717
});
1818

19+
test.after('cleanup', async () => {
20+
await s.close();
21+
});
22+
1923
test('beforeRequest receives normalized options', async t => {
2024
await got(
2125
s.url,
@@ -34,7 +38,7 @@ test('beforeRequest receives normalized options', async t => {
3438
});
3539

3640
test('beforeRequest allows modifications', async t => {
37-
const res = await got(
41+
const response = await got(
3842
s.url,
3943
{
4044
json: true,
@@ -47,11 +51,11 @@ test('beforeRequest allows modifications', async t => {
4751
}
4852
}
4953
);
50-
t.is(res.body.foo, 'bar');
54+
t.is(response.body.foo, 'bar');
5155
});
5256

5357
test('beforeRequest awaits async function', async t => {
54-
const res = await got(
58+
const response = await got(
5559
s.url,
5660
{
5761
json: true,
@@ -65,7 +69,7 @@ test('beforeRequest awaits async function', async t => {
6569
}
6670
}
6771
);
68-
t.is(res.body.foo, 'bar');
72+
t.is(response.body.foo, 'bar');
6973
});
7074

7175
test('beforeRequest rejects when beforeRequest throws', async t => {
@@ -99,7 +103,3 @@ test('beforeRequest rejects when beforeRequest rejects', async t => {
99103
}
100104
);
101105
});
102-
103-
test.after('cleanup', async () => {
104-
await s.close();
105-
});

‎test/http.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@ let s;
88
test.before('setup', async () => {
99
s = await createServer();
1010

11-
s.on('/', (req, res) => {
12-
res.end('ok');
11+
s.on('/', (request, response) => {
12+
response.end('ok');
1313
});
1414

15-
s.on('/empty', (req, res) => {
16-
res.end();
15+
s.on('/empty', (request, response) => {
16+
response.end();
1717
});
1818

19-
s.on('/304', (req, res) => {
20-
res.statusCode = 304;
21-
res.end();
19+
s.on('/304', (request, response) => {
20+
response.statusCode = 304;
21+
response.end();
2222
});
2323

24-
s.on('/404', (req, res) => {
25-
res.statusCode = 404;
26-
res.end('not');
24+
s.on('/404', (request, response) => {
25+
response.statusCode = 404;
26+
response.end('not');
2727
});
2828

29-
s.on('/?recent=true', (req, res) => {
30-
res.end('recent');
29+
s.on('/?recent=true', (request, response) => {
30+
response.end('recent');
3131
});
3232

3333
await s.listen(s.port);
3434
});
3535

36+
test.after('cleanup', async () => {
37+
await s.close();
38+
});
39+
3640
test('simple request', async t => {
3741
t.is((await got(s.url)).body, 'ok');
3842
});
@@ -47,9 +51,9 @@ test('requestUrl response', async t => {
4751
});
4852

4953
test('error with code', async t => {
50-
const err = await t.throws(got(`${s.url}/404`));
51-
t.is(err.statusCode, 404);
52-
t.is(err.response.body, 'not');
54+
const error = await t.throws(got(`${s.url}/404`));
55+
t.is(error.statusCode, 404);
56+
t.is(error.response.body, 'not');
5357
});
5458

5559
test('status code 304 doesn\'t throw', async t => {
@@ -65,8 +69,8 @@ test('doesn\'t throw on throwHttpErrors === false', async t => {
6569
});
6670

6771
test('invalid protocol throws', async t => {
68-
const err = await t.throws(got('c:/nope.com', {json: true}));
69-
t.is(err.constructor, got.UnsupportedProtocolError);
72+
const error = await t.throws(got('c:/nope.com', {json: true}));
73+
t.is(error.constructor, got.UnsupportedProtocolError);
7074
});
7175

7276
test('buffer on encoding === null', async t => {
@@ -87,7 +91,3 @@ test('requestUrl response when sending url as param', async t => {
8791
test('response contains url', async t => {
8892
t.is((await got(s.url)).url, `${s.url}/`);
8993
});
90-
91-
test.after('cleanup', async () => {
92-
await s.close();
93-
});

‎test/https.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ test.before('setup', async () => {
3636

3737
s = await createSSLServer({key, cert});
3838

39-
s.on('/', (req, res) => res.end('ok'));
39+
s.on('/', (request, response) => response.end('ok'));
4040

4141
await s.listen(s.port);
4242
});
4343

44+
test.after('cleanup', async () => {
45+
await s.close();
46+
});
47+
4448
test('make request to https server without ca', async t => {
4549
t.truthy((await got(s.url, {rejectUnauthorized: false})).body);
4650
});
@@ -62,6 +66,3 @@ test('protocol-less URLs default to HTTPS', async t => {
6266
t.true(requestUrl.startsWith('https://'));
6367
});
6468

65-
test.after('cleanup', async () => {
66-
await s.close();
67-
});

‎test/json-parse.js

+33-33
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,40 @@ let s;
77
test.before('setup', async () => {
88
s = await createServer();
99

10-
s.on('/', (req, res) => {
11-
res.end('{"data":"dog"}');
10+
s.on('/', (request, response) => {
11+
response.end('{"data":"dog"}');
1212
});
1313

14-
s.on('/invalid', (req, res) => {
15-
res.end('/');
14+
s.on('/invalid', (request, response) => {
15+
response.end('/');
1616
});
1717

18-
s.on('/no-body', (req, res) => {
19-
res.statusCode = 200;
20-
res.end();
18+
s.on('/no-body', (request, response) => {
19+
response.statusCode = 200;
20+
response.end();
2121
});
2222

23-
s.on('/non200', (req, res) => {
24-
res.statusCode = 500;
25-
res.end('{"data":"dog"}');
23+
s.on('/non200', (request, response) => {
24+
response.statusCode = 500;
25+
response.end('{"data":"dog"}');
2626
});
2727

28-
s.on('/non200-invalid', (req, res) => {
29-
res.statusCode = 500;
30-
res.end('Internal error');
28+
s.on('/non200-invalid', (request, response) => {
29+
response.statusCode = 500;
30+
response.end('Internal error');
3131
});
3232

33-
s.on('/headers', (req, res) => {
34-
res.end(JSON.stringify(req.headers));
33+
s.on('/headers', (request, response) => {
34+
response.end(JSON.stringify(request.headers));
3535
});
3636

3737
await s.listen(s.port);
3838
});
3939

40+
test.after('cleanup', async () => {
41+
await s.close();
42+
});
43+
4044
test('parses response', async t => {
4145
t.deepEqual((await got(s.url, {json: true})).body, {data: 'dog'});
4246
});
@@ -47,36 +51,32 @@ test('not parses responses without a body', async t => {
4751
});
4852

4953
test('wraps parsing errors', async t => {
50-
const err = await t.throws(got(`${s.url}/invalid`, {json: true}));
51-
t.regex(err.message, /Unexpected token/);
52-
t.true(err.message.includes(err.hostname), err.message);
53-
t.is(err.path, '/invalid');
54+
const error = await t.throws(got(`${s.url}/invalid`, {json: true}));
55+
t.regex(error.message, /Unexpected token/);
56+
t.true(error.message.includes(error.hostname), error.message);
57+
t.is(error.path, '/invalid');
5458
});
5559

5660
test('parses non-200 responses', async t => {
57-
const err = await t.throws(got(`${s.url}/non200`, {json: true}));
58-
t.deepEqual(err.response.body, {data: 'dog'});
61+
const error = await t.throws(got(`${s.url}/non200`, {json: true}));
62+
t.deepEqual(error.response.body, {data: 'dog'});
5963
});
6064

6165
test('ignores errors on invalid non-200 responses', async t => {
62-
const err = await t.throws(got(`${s.url}/non200-invalid`, {json: true}));
63-
t.is(err.message, 'Response code 500 (Internal Server Error)');
64-
t.is(err.response.body, 'Internal error');
65-
t.is(err.path, '/non200-invalid');
66+
const error = await t.throws(got(`${s.url}/non200-invalid`, {json: true}));
67+
t.is(error.message, 'Response code 500 (Internal Server Error)');
68+
t.is(error.response.body, 'Internal error');
69+
t.is(error.path, '/non200-invalid');
6670
});
6771

68-
test('should have statusCode in err', async t => {
69-
const err = await t.throws(got(`${s.url}/invalid`, {json: true}));
70-
t.is(err.constructor, got.ParseError);
71-
t.is(err.statusCode, 200);
72+
test('should have statusCode in error', async t => {
73+
const error = await t.throws(got(`${s.url}/invalid`, {json: true}));
74+
t.is(error.constructor, got.ParseError);
75+
t.is(error.statusCode, 200);
7276
});
7377

7478
test('should set correct headers', async t => {
7579
const {body: headers} = await got(`${s.url}/headers`, {json: true, body: {}});
7680
t.is(headers['content-type'], 'application/json');
7781
t.is(headers.accept, 'application/json');
7882
});
79-
80-
test.after('cleanup', async () => {
81-
await s.close();
82-
});

‎test/post.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@ let s;
88
test.before('setup', async () => {
99
s = await createServer();
1010

11-
s.on('/', (req, res) => {
12-
res.setHeader('method', req.method);
13-
req.pipe(res);
11+
s.on('/', (request, response) => {
12+
response.setHeader('method', request.method);
13+
request.pipe(response);
1414
});
1515

16-
s.on('/headers', (req, res) => {
17-
res.end(JSON.stringify(req.headers));
16+
s.on('/headers', (request, response) => {
17+
response.end(JSON.stringify(request.headers));
1818
});
1919

20-
s.on('/empty', (req, res) => {
21-
res.end();
20+
s.on('/empty', (request, response) => {
21+
response.end();
2222
});
2323

2424
await s.listen(s.port);
2525
});
2626

27+
test.after('cleanup', async () => {
28+
await s.close();
29+
});
30+
2731
test('GET can have body', async t => {
2832
const {body, headers} = await got.get(s.url, {body: 'hi'});
2933
t.is(body, 'hi');
@@ -143,7 +147,3 @@ test('throws when json body is not a plain object or array', async t => {
143147
test('throws when form body is not a plain object or array', async t => {
144148
await t.throws(got(`${s.url}`, {body: 'such=wow', form: true}), TypeError);
145149
});
146-
147-
test.after('cleanup', async () => {
148-
await s.close();
149-
});

‎test/progress.js

+29-29
Original file line numberDiff line numberDiff line change
@@ -42,42 +42,46 @@ let s;
4242
test.before('setup', async () => {
4343
s = await createServer();
4444

45-
s.on('/download', (req, res) => {
46-
res.setHeader('content-length', file.length);
45+
s.on('/download', (request, response) => {
46+
response.setHeader('content-length', file.length);
4747

4848
toReadableStream(file)
4949
.pipe(new SlowStream({maxWriteInterval: 50}))
50-
.pipe(res);
50+
.pipe(response);
5151
});
5252

53-
s.on('/download/no-total', (req, res) => {
54-
res.write('hello');
55-
res.end();
53+
s.on('/download/no-total', (request, response) => {
54+
response.write('hello');
55+
response.end();
5656
});
5757

58-
s.on('/upload', (req, res) => {
59-
req
58+
s.on('/upload', (request, response) => {
59+
request
6060
.pipe(new SlowStream({maxWriteInterval: 100}))
61-
.on('end', () => res.end());
61+
.on('end', () => response.end());
6262
});
6363

6464
await s.listen(s.port);
6565
});
6666

67+
test.after('cleanup', async () => {
68+
await s.close();
69+
});
70+
6771
test('download progress', async t => {
6872
const events = [];
6973

70-
const res = await got(`${s.url}/download`, {encoding: null})
71-
.on('downloadProgress', e => events.push(e));
74+
const response = await got(`${s.url}/download`, {encoding: null})
75+
.on('downloadProgress', event => events.push(event));
7276

73-
checkEvents(t, events, res.body.length);
77+
checkEvents(t, events, response.body.length);
7478
});
7579

7680
test('download progress - missing total size', async t => {
7781
const events = [];
7882

7983
await got(`${s.url}/download/no-total`)
80-
.on('downloadProgress', e => events.push(e));
84+
.on('downloadProgress', event => events.push(event));
8185

8286
checkEvents(t, events);
8387
});
@@ -86,7 +90,7 @@ test('download progress - stream', async t => {
8690
const events = [];
8791

8892
const stream = got.stream(`${s.url}/download`, {encoding: null})
89-
.on('downloadProgress', e => events.push(e));
93+
.on('downloadProgress', event => events.push(event));
9094

9195
await getStream(stream);
9296

@@ -97,7 +101,7 @@ test('upload progress - file', async t => {
97101
const events = [];
98102

99103
await got.post(`${s.url}/upload`, {body: file})
100-
.on('uploadProgress', e => events.push(e));
104+
.on('uploadProgress', event => events.push(event));
101105

102106
checkEvents(t, events, file.length);
103107
});
@@ -109,7 +113,7 @@ test('upload progress - file stream', async t => {
109113
const events = [];
110114

111115
await got.post(`${s.url}/upload`, {body: fs.createReadStream(path)})
112-
.on('uploadProgress', e => events.push(e));
116+
.on('uploadProgress', event => events.push(event));
113117

114118
checkEvents(t, events, file.length);
115119
});
@@ -124,7 +128,7 @@ test('upload progress - form data', async t => {
124128
const size = await util.promisify(body.getLength.bind(body))();
125129

126130
await got.post(`${s.url}/upload`, {body})
127-
.on('uploadProgress', e => events.push(e));
131+
.on('uploadProgress', event => events.push(event));
128132

129133
checkEvents(t, events, size);
130134
});
@@ -135,7 +139,7 @@ test('upload progress - json', async t => {
135139
const events = [];
136140

137141
await got.post(`${s.url}/upload`, {body})
138-
.on('uploadProgress', e => events.push(e));
142+
.on('uploadProgress', event => events.push(event));
139143

140144
checkEvents(t, events, size);
141145
});
@@ -146,21 +150,21 @@ test('upload progress - stream with known body size', async t => {
146150
headers: {'content-length': file.length}
147151
};
148152

149-
const req = got.stream.post(`${s.url}/upload`, options)
150-
.on('uploadProgress', e => events.push(e));
153+
const request = got.stream.post(`${s.url}/upload`, options)
154+
.on('uploadProgress', event => events.push(event));
151155

152-
await getStream(toReadableStream(file).pipe(req));
156+
await getStream(toReadableStream(file).pipe(request));
153157

154158
checkEvents(t, events, file.length);
155159
});
156160

157161
test('upload progress - stream with unknown body size', async t => {
158162
const events = [];
159163

160-
const req = got.stream.post(`${s.url}/upload`)
161-
.on('uploadProgress', e => events.push(e));
164+
const request = got.stream.post(`${s.url}/upload`)
165+
.on('uploadProgress', event => events.push(event));
162166

163-
await getStream(toReadableStream(file).pipe(req));
167+
await getStream(toReadableStream(file).pipe(request));
164168

165169
checkEvents(t, events);
166170
});
@@ -169,7 +173,7 @@ test('upload progress - no body', async t => {
169173
const events = [];
170174

171175
await got.post(`${s.url}/upload`)
172-
.on('uploadProgress', e => events.push(e));
176+
.on('uploadProgress', event => events.push(event));
173177

174178
t.deepEqual(events, [
175179
{
@@ -184,7 +188,3 @@ test('upload progress - no body', async t => {
184188
}
185189
]);
186190
});
187-
188-
test.after('cleanup', async () => {
189-
await s.close();
190-
});

‎test/promise.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ let s;
88

99
test.before('setup', async () => {
1010
s = await createServer();
11-
s.on('/', (req, res) => {
12-
res.statusCode = 200;
13-
res.end();
11+
s.on('/', (request, response) => {
12+
response.statusCode = 200;
13+
response.end();
1414
});
1515
await s.listen(s.port);
1616
});
1717

18+
test.after('cleanup', async () => {
19+
await s.close();
20+
});
21+
1822
test('should emit request event as promise', async t => {
19-
await got(s.url, {json: true}).on('request', req => {
20-
t.true(req instanceof ClientRequest);
23+
await got(s.url, {json: true}).on('request', request => {
24+
t.true(request instanceof ClientRequest);
2125
});
2226
});
2327

2428
test('should emit response event as promise', async t => {
25-
await got(s.url, {json: true}).on('response', res => {
26-
t.true(res instanceof Transform);
27-
t.true(res.readable);
28-
t.is(res.statusCode, 200);
29+
await got(s.url, {json: true}).on('response', response => {
30+
t.true(response instanceof Transform);
31+
t.true(response.readable);
32+
t.is(response.statusCode, 200);
2933
});
3034
});
31-
32-
test.after('cleanup', async () => {
33-
await s.close();
34-
});

‎test/redirects.js

+48-48
Original file line numberDiff line numberDiff line change
@@ -40,95 +40,95 @@ test.before('setup', async () => {
4040

4141
// HTTPS Handlers
4242

43-
https.on('/', (req, res) => {
44-
res.end('https');
43+
https.on('/', (request, response) => {
44+
response.end('https');
4545
});
4646

47-
https.on('/httpsToHttp', (req, res) => {
48-
res.writeHead(302, {
47+
https.on('/httpsToHttp', (request, response) => {
48+
response.writeHead(302, {
4949
location: http.url
5050
});
51-
res.end();
51+
response.end();
5252
});
5353

5454
// HTTP Handlers
5555

56-
http.on('/', (req, res) => {
57-
res.end('reached');
56+
http.on('/', (request, response) => {
57+
response.end('reached');
5858
});
5959

60-
http.on('/finite', (req, res) => {
61-
res.writeHead(302, {
60+
http.on('/finite', (request, response) => {
61+
response.writeHead(302, {
6262
location: `${http.url}/`
6363
});
64-
res.end();
64+
response.end();
6565
});
6666

67-
http.on('/utf8-url-áé', (req, res) => {
68-
res.end('reached');
67+
http.on('/utf8-url-áé', (request, response) => {
68+
response.end('reached');
6969
});
7070

71-
http.on('/redirect-with-utf8-binary', (req, res) => {
72-
res.writeHead(302, {
71+
http.on('/redirect-with-utf8-binary', (request, response) => {
72+
response.writeHead(302, {
7373
location: Buffer.from((new URL('/utf8-url-áé', http.url)).toString(), 'utf8').toString('binary')
7474
});
75-
res.end();
75+
response.end();
7676
});
7777

78-
http.on('/endless', (req, res) => {
79-
res.writeHead(302, {
78+
http.on('/endless', (request, response) => {
79+
response.writeHead(302, {
8080
location: `${http.url}/endless`
8181
});
82-
res.end();
82+
response.end();
8383
});
8484

85-
http.on('/relative', (req, res) => {
86-
res.writeHead(302, {
85+
http.on('/relative', (request, response) => {
86+
response.writeHead(302, {
8787
location: '/'
8888
});
89-
res.end();
89+
response.end();
9090
});
9191

92-
http.on('/seeOther', (req, res) => {
93-
res.writeHead(303, {
92+
http.on('/seeOther', (request, response) => {
93+
response.writeHead(303, {
9494
location: '/'
9595
});
96-
res.end();
96+
response.end();
9797
});
9898

99-
http.on('/temporary', (req, res) => {
100-
res.writeHead(307, {
99+
http.on('/temporary', (request, response) => {
100+
response.writeHead(307, {
101101
location: '/'
102102
});
103-
res.end();
103+
response.end();
104104
});
105105

106-
http.on('/permanent', (req, res) => {
107-
res.writeHead(308, {
106+
http.on('/permanent', (request, response) => {
107+
response.writeHead(308, {
108108
location: '/'
109109
});
110-
res.end();
110+
response.end();
111111
});
112112

113-
http.on('/relativeQuery?bang', (req, res) => {
114-
res.writeHead(302, {
113+
http.on('/relativeQuery?bang', (request, response) => {
114+
response.writeHead(302, {
115115
location: '/'
116116
});
117-
res.end();
117+
response.end();
118118
});
119119

120-
http.on('/httpToHttps', (req, res) => {
121-
res.writeHead(302, {
120+
http.on('/httpToHttps', (request, response) => {
121+
response.writeHead(302, {
122122
location: https.url
123123
});
124-
res.end();
124+
response.end();
125125
});
126126

127-
http.on('/malformedRedirect', (req, res) => {
128-
res.writeHead(302, {
127+
http.on('/malformedRedirect', (request, response) => {
128+
response.writeHead(302, {
129129
location: '/%D8'
130130
});
131-
res.end();
131+
response.end();
132132
});
133133

134134
await http.listen(http.port);
@@ -163,9 +163,9 @@ test('relative redirect works', async t => {
163163
});
164164

165165
test('throws on endless redirect', async t => {
166-
const err = await t.throws(got(`${http.url}/endless`));
167-
t.is(err.message, 'Redirected 10 times. Aborting.');
168-
t.deepEqual(err.redirectUrls, new Array(10).fill(`${http.url}/endless`));
166+
const error = await t.throws(got(`${http.url}/endless`));
167+
t.is(error.message, 'Redirected 10 times. Aborting.');
168+
t.deepEqual(error.redirectUrls, new Array(10).fill(`${http.url}/endless`));
169169
});
170170

171171
test('query in options are not breaking redirects', async t => {
@@ -180,10 +180,10 @@ test('hostname+path in options are not breaking redirects', async t => {
180180
});
181181

182182
test('redirect only GET and HEAD requests', async t => {
183-
const err = await t.throws(got(`${http.url}/relative`, {body: 'wow'}));
184-
t.is(err.message, 'Response code 302 (Found)');
185-
t.is(err.path, '/relative');
186-
t.is(err.statusCode, 302);
183+
const error = await t.throws(got(`${http.url}/relative`, {body: 'wow'}));
184+
t.is(error.message, 'Response code 302 (Found)');
185+
t.is(error.path, '/relative');
186+
t.is(error.statusCode, 302);
187187
});
188188

189189
test('redirect on 303 response even with post, put, delete', async t => {
@@ -220,6 +220,6 @@ test('redirect response contains utf8 with binary encoding', async t => {
220220
});
221221

222222
test('throws on malformed redirect URI', async t => {
223-
const err = await t.throws(got(`${http.url}/malformedRedirect`));
224-
t.is(err.name, 'URIError');
223+
const error = await t.throws(got(`${http.url}/malformedRedirect`));
224+
t.is(error.name, 'URIError');
225225
});

‎test/retry.js

+33-33
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,70 @@ test.before('setup', async () => {
1717

1818
s.on('/long', () => {});
1919

20-
s.on('/knock-twice', (req, res) => {
20+
s.on('/knock-twice', (request, response) => {
2121
if (knocks++ === 1) {
22-
res.end('who`s there?');
22+
response.end('who`s there?');
2323
}
2424
});
2525

2626
s.on('/try-me', () => {
2727
trys++;
2828
});
2929

30-
s.on('/fifth', (req, res) => {
30+
s.on('/fifth', (request, response) => {
3131
if (fifth++ === 5) {
32-
res.end('who`s there?');
32+
response.end('who`s there?');
3333
}
3434
});
3535

36-
s.on('/500', (req, res) => {
37-
res.statusCode = 500;
38-
res.end();
36+
s.on('/500', (request, response) => {
37+
response.statusCode = 500;
38+
response.end();
3939
});
4040

41-
s.on('/measure413', (req, res) => {
42-
res.writeHead(413, {
41+
s.on('/measure413', (request, response) => {
42+
response.writeHead(413, {
4343
'Retry-After': retryAfterOn413
4444
});
45-
res.end((Date.now() - lastTried413access).toString());
45+
response.end((Date.now() - lastTried413access).toString());
4646

4747
lastTried413access = Date.now();
4848
});
4949

50-
s.on('/413', (req, res) => {
51-
res.writeHead(413, {
50+
s.on('/413', (request, response) => {
51+
response.writeHead(413, {
5252
'Retry-After': retryAfterOn413
5353
});
54-
res.end();
54+
response.end();
5555
});
5656

57-
s.on('/413withoutRetryAfter', (req, res) => {
58-
res.statusCode = 413;
59-
res.end();
57+
s.on('/413withoutRetryAfter', (request, response) => {
58+
response.statusCode = 413;
59+
response.end();
6060
});
6161

62-
s.on('/503', (req, res) => {
63-
res.statusCode = 503;
64-
res.end();
62+
s.on('/503', (request, response) => {
63+
response.statusCode = 503;
64+
response.end();
6565
});
6666

6767
await s.listen(s.port);
6868
});
6969

70+
test.after('cleanup', async () => {
71+
await s.close();
72+
});
73+
7074
test('works on timeout error', async t => {
7175
t.is((await got(`${s.url}/knock-twice`, {timeout: {socket: socketTimeout}})).body, 'who`s there?');
7276
});
7377

7478
test('can be disabled with option', async t => {
75-
const err = await t.throws(got(`${s.url}/try-me`, {
79+
const error = await t.throws(got(`${s.url}/try-me`, {
7680
timeout: {socket: socketTimeout},
7781
retry: 0
7882
}));
79-
t.truthy(err);
83+
t.truthy(error);
8084
t.is(trys, 1);
8185
});
8286

@@ -91,31 +95,31 @@ test('function gets iter count', async t => {
9195
});
9296

9397
test('falsy value prevents retries', async t => {
94-
const err = await t.throws(got(`${s.url}/long`, {
98+
const error = await t.throws(got(`${s.url}/long`, {
9599
timeout: {socket: socketTimeout},
96100
retry: {
97101
retries: () => 0
98102
}
99103
}));
100-
t.truthy(err);
104+
t.truthy(error);
101105
});
102106

103107
test('falsy value prevents retries #2', async t => {
104-
const err = await t.throws(got(`${s.url}/long`, {
108+
const error = await t.throws(got(`${s.url}/long`, {
105109
timeout: {socket: socketTimeout},
106110
retry: {
107-
retries: (iter, err) => {
108-
t.truthy(err);
111+
retries: (iter, error) => {
112+
t.truthy(error);
109113
return false;
110114
}
111115
}
112116
}));
113-
t.truthy(err);
117+
t.truthy(error);
114118
});
115119

116120
test('custom retries', async t => {
117121
let tried = false;
118-
const err = await t.throws(got(`${s.url}/500`, {
122+
const error = await t.throws(got(`${s.url}/500`, {
119123
throwHttpErrors: true,
120124
retry: {
121125
retries: iter => {
@@ -132,7 +136,7 @@ test('custom retries', async t => {
132136
]
133137
}
134138
}));
135-
t.is(err.statusCode, 500);
139+
t.is(error.statusCode, 500);
136140
t.true(tried);
137141
});
138142

@@ -203,7 +207,3 @@ test('doesn\'t retry if Retry-After header is greater than maxRetryAfter', async
203207
});
204208
t.is(retryCount, 0);
205209
});
206-
207-
test.after('cleanup', async () => {
208-
await s.close();
209-
});

‎test/socket-destroyed.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import got from '../source';
44
const {Timer} = process.binding('timer_wrap');
55

66
test.serial('clear the progressInterval if the socket has been destroyed', async t => {
7-
const err = await t.throws(got(`http://127.0.0.1:55555`, {retry: 0}));
7+
const error = await t.throws(got(`http://127.0.0.1:55555`, {retry: 0}));
88
const progressIntervalTimer = process._getActiveHandles().filter(handle => {
99
// Check if the handle is a Timer that matches the `uploadEventFrequency` interval
1010
return handle instanceof Timer && handle._list.msecs === 150;
1111
});
1212
t.is(progressIntervalTimer.length, 0);
13-
t.is(err.code, 'ECONNREFUSED');
13+
t.is(error.code, 'ECONNREFUSED');
1414
});

‎test/stream.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,36 @@ let s;
1111
test.before('setup', async () => {
1212
s = await createServer();
1313

14-
s.on('/', (req, res) => {
15-
res.writeHead(200, {
14+
s.on('/', (request, response) => {
15+
response.writeHead(200, {
1616
unicorn: 'rainbow'
1717
});
18-
res.end('ok');
18+
response.end('ok');
1919
});
2020

21-
s.on('/post', (req, res) => {
22-
req.pipe(res);
21+
s.on('/post', (request, response) => {
22+
request.pipe(response);
2323
});
2424

25-
s.on('/redirect', (req, res) => {
26-
res.writeHead(302, {
25+
s.on('/redirect', (request, response) => {
26+
response.writeHead(302, {
2727
location: s.url
2828
});
29-
res.end();
29+
response.end();
3030
});
3131

32-
s.on('/error', (req, res) => {
33-
res.statusCode = 404;
34-
res.end();
32+
s.on('/error', (request, response) => {
33+
response.statusCode = 404;
34+
response.end();
3535
});
3636

3737
await s.listen(s.port);
3838
});
3939

40+
test.after('cleanup', async () => {
41+
await s.close();
42+
});
43+
4044
test('option.json can not be used', t => {
4145
t.throws(() => {
4246
got.stream(s.url, {json: true});
@@ -117,8 +121,8 @@ test('piping works', async t => {
117121
test('proxying headers works', async t => {
118122
const server = await createServer();
119123

120-
server.on('/', (req, res) => {
121-
got.stream(s.url).pipe(res);
124+
server.on('/', (request, response) => {
125+
got.stream(s.url).pipe(response);
122126
});
123127

124128
await server.listen(server.port);
@@ -128,7 +132,3 @@ test('proxying headers works', async t => {
128132

129133
await server.close();
130134
});
131-
132-
test.after('cleanup', async () => {
133-
await s.close();
134-
});

‎test/timeout.js

+38-34
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import got from '../source';
99
import {createServer} from './helpers/server';
1010

1111
let s;
12-
const reqDelay = 250;
12+
1313
const slowDataStream = () => {
1414
const slowStream = new stream.PassThrough();
1515
let count = 0;
@@ -22,36 +22,40 @@ const slowDataStream = () => {
2222
}, 100);
2323
return slowStream;
2424
};
25-
const reqTimeout = reqDelay - 10;
25+
26+
const requestDelay = 250;
27+
const requestTimeout = requestDelay - 10;
28+
2629
const errorMatcher = {
2730
instanceOf: got.TimeoutError,
2831
code: 'ETIMEDOUT'
2932
};
33+
3034
const keepAliveAgent = new http.Agent({
3135
keepAlive: true
3236
});
3337

3438
test.before('setup', async () => {
3539
s = await createServer();
3640

37-
s.on('/', async (req, res) => {
38-
req.on('data', () => {});
39-
req.on('end', async () => {
40-
await delay(reqDelay);
41-
res.end('OK');
41+
s.on('/', async (request, response) => {
42+
request.on('data', () => {});
43+
request.on('end', async () => {
44+
await delay(requestDelay);
45+
response.end('OK');
4246
});
4347
});
4448

45-
s.on('/download', async (req, res) => {
46-
res.writeHead(200, {
49+
s.on('/download', async (request, response) => {
50+
response.writeHead(200, {
4751
'transfer-encoding': 'chunked'
4852
});
49-
res.flushHeaders();
50-
slowDataStream().pipe(res);
53+
response.flushHeaders();
54+
slowDataStream().pipe(response);
5155
});
5256

53-
s.on('/prime', (req, res) => {
54-
res.end('OK');
57+
s.on('/prime', (request, response) => {
58+
response.end('OK');
5559
});
5660

5761
await s.listen(s.port);
@@ -73,7 +77,7 @@ test('timeout option (ETIMEDOUT)', async t => {
7377
test('timeout option as object (ETIMEDOUT)', async t => {
7478
await t.throws(
7579
got(s.url, {
76-
timeout: {socket: reqDelay * 2.5, request: 1},
80+
timeout: {socket: requestDelay * 2.5, request: 1},
7781
retry: 0
7882
}),
7983
{
@@ -86,13 +90,13 @@ test('timeout option as object (ETIMEDOUT)', async t => {
8690
test('socket timeout', async t => {
8791
await t.throws(
8892
got(s.url, {
89-
timeout: {socket: reqTimeout},
93+
timeout: {socket: requestTimeout},
9094
retry: 0
9195
}),
9296
{
9397
instanceOf: got.TimeoutError,
9498
code: 'ETIMEDOUT',
95-
message: `Timeout awaiting 'socket' for ${reqTimeout}ms`
99+
message: `Timeout awaiting 'socket' for ${requestTimeout}ms`
96100
}
97101
);
98102
});
@@ -118,8 +122,8 @@ test('send timeout (keepalive)', async t => {
118122
timeout: {send: 1},
119123
retry: 0,
120124
body: slowDataStream()
121-
}).on('request', req => {
122-
req.once('socket', socket => {
125+
}).on('request', request => {
126+
request.once('socket', socket => {
123127
t.false(socket.connecting);
124128
socket.once('connect', () => {
125129
t.fail(`'connect' event fired, invalidating test`);
@@ -148,15 +152,15 @@ test('response timeout', async t => {
148152

149153
test('response timeout unaffected by slow upload', async t => {
150154
await got(s.url, {
151-
timeout: {response: reqDelay * 2},
155+
timeout: {response: requestDelay * 2},
152156
retry: 0,
153157
body: slowDataStream()
154158
}).on('request', request => {
155159
request.on('error', error => {
156160
t.fail(`unexpected error: ${error}`);
157161
});
158162
});
159-
await delay(reqDelay * 3);
163+
await delay(requestDelay * 3);
160164
t.pass('no error emitted');
161165
});
162166

@@ -169,7 +173,7 @@ test('response timeout unaffected by slow download', async t => {
169173
t.fail(`unexpected error: ${error}`);
170174
});
171175
});
172-
await delay(reqDelay * 3);
176+
await delay(requestDelay * 3);
173177
t.pass('no error emitted');
174178
});
175179

@@ -180,8 +184,8 @@ test('response timeout (keepalive)', async t => {
180184
agent: keepAliveAgent,
181185
timeout: {response: 1},
182186
retry: 0
183-
}).on('request', req => {
184-
req.once('socket', socket => {
187+
}).on('request', request => {
188+
request.once('socket', socket => {
185189
t.false(socket.connecting);
186190
socket.once('connect', () => {
187191
t.fail(`'connect' event fired, invalidating test`);
@@ -298,12 +302,12 @@ test('lookup timeout no error (keepalive)', async t => {
298302
test('request timeout', async t => {
299303
await t.throws(
300304
got(s.url, {
301-
timeout: {request: reqTimeout},
305+
timeout: {request: requestTimeout},
302306
retry: 0
303307
}),
304308
{
305309
...errorMatcher,
306-
message: `Timeout awaiting 'request' for ${reqTimeout}ms`
310+
message: `Timeout awaiting 'request' for ${requestTimeout}ms`
307311
}
308312
);
309313
});
@@ -312,7 +316,7 @@ test('retries on timeout (ESOCKETTIMEDOUT)', async t => {
312316
let tried = false;
313317

314318
await t.throws(got(s.url, {
315-
timeout: reqTimeout,
319+
timeout: requestTimeout,
316320
retry: {
317321
retries: () => {
318322
if (tried) {
@@ -325,7 +329,7 @@ test('retries on timeout (ESOCKETTIMEDOUT)', async t => {
325329
}
326330
}), {
327331
...errorMatcher,
328-
message: `Timeout awaiting 'request' for ${reqTimeout}ms`
332+
message: `Timeout awaiting 'request' for ${requestTimeout}ms`
329333
});
330334

331335
t.true(tried);
@@ -363,30 +367,30 @@ test('no error emitted when timeout is not breached (stream)', async t => {
363367
const stream = got.stream(s.url, {
364368
retry: 0,
365369
timeout: {
366-
request: reqDelay * 2
370+
request: requestDelay * 2
367371
}
368372
});
369373
stream.on('error', err => {
370374
t.fail(`error was emitted: ${err}`);
371375
});
372376
await getStream(stream);
373-
await delay(reqDelay * 3);
377+
await delay(requestDelay * 3);
374378
t.pass();
375379
});
376380

377381
test('no error emitted when timeout is not breached (promise)', async t => {
378382
await got(s.url, {
379383
retry: 0,
380384
timeout: {
381-
request: reqDelay * 2
385+
request: requestDelay * 2
382386
}
383-
}).on('request', req => {
387+
}).on('request', request => {
384388
// 'error' events are not emitted by the Promise interface, so attach
385389
// directly to the request object
386-
req.on('error', err => {
387-
t.fail(`error was emitted: ${err}`);
390+
request.on('error', error => {
391+
t.fail(`error was emitted: ${error}`);
388392
});
389393
});
390-
await delay(reqDelay * 3);
394+
await delay(requestDelay * 3);
391395
t.pass();
392396
});

‎test/unix-socket.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ if (process.platform !== 'win32') {
1212
test.before('setup', async () => {
1313
s = await createServer();
1414

15-
s.on('/', (req, res) => {
16-
res.end('ok');
15+
s.on('/', (request, response) => {
16+
response.end('ok');
1717
});
1818

19-
s.on('/foo:bar', (req, res) => {
20-
res.end('ok');
19+
s.on('/foo:bar', (request, response) => {
20+
response.end('ok');
2121
});
2222

2323
await s.listen(socketPath);
2424
});
2525

26+
test.after('cleanup', async () => {
27+
await s.close();
28+
});
29+
2630
test('works', async t => {
2731
const url = format('http://unix:%s:%s', socketPath, '/');
2832
t.is((await got(url)).body, 'ok');
@@ -37,8 +41,4 @@ if (process.platform !== 'win32') {
3741
const url = format('unix:%s:%s', socketPath, '/foo:bar');
3842
t.is((await got(url)).body, 'ok');
3943
});
40-
41-
test.after('cleanup', async () => {
42-
await s.close();
43-
});
4444
}

0 commit comments

Comments
 (0)
Please sign in to comment.