Skip to content

Commit 33b838f

Browse files
michaltkszmarczak
authored andcommittedDec 9, 2018
Add Got options onto responses and errors (#663)
1 parent 8848a7a commit 33b838f

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed
 

‎readme.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,14 @@ const instance = got.extend({
452452

453453
The response object will typically be a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage), however, if returned from the cache it will be a [response-like object](https://github.com/lukechilds/responselike) which behaves in the same way.
454454

455+
##### request
456+
457+
Type: `Object`
458+
459+
**Note:** This is not a [http.ClientRequest](https://nodejs.org/api/http.html#http_class_http_clientrequest).
460+
461+
- `gotOptions` - The options that were set on this request.
462+
455463
##### body
456464

457465
Type: `string` `Object` *(depending on `options.json`)*
@@ -662,7 +670,7 @@ The default Got options.
662670

663671
## Errors
664672

665-
Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier.
673+
Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol`, `url`, and `gotOptions` properties to make debugging easier.
666674

667675
In Promise mode, the `response` is attached to the error.
668676

‎source/errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class GotError extends Error {
2121
path: options.path,
2222
socketPath: options.socketPath,
2323
protocol: options.protocol,
24-
url: options.href
24+
url: options.href,
25+
gotOptions: options
2526
});
2627
}
2728
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ module.exports = (options, input) => {
9292
response.retryCount = retryCount;
9393
response.timings = timings;
9494
response.redirectUrls = redirects;
95+
response.request = {
96+
gotOptions: options
97+
};
9598

9699
const rawCookies = response.headers['set-cookie'];
97100
if (options.cookieJar && rawCookies) {

‎test/cache.js

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ test('Redirects are cached and re-used internally', async t => {
110110
t.is(firstResponse.body, secondResponse.body);
111111
});
112112

113+
test('Cached response should have got options', async t => {
114+
const endpoint = '/cache';
115+
const cache = new Map();
116+
const options = {
117+
url: s.url + endpoint,
118+
auth: 'foo:bar',
119+
cache
120+
};
121+
122+
await got(options);
123+
const secondResponse = await got(options);
124+
125+
t.is(secondResponse.request.gotOptions.auth, options.auth);
126+
});
127+
113128
test('Cache error throws got.CacheError', async t => {
114129
const endpoint = '/no-store';
115130
const cache = {};

‎test/error.js

+10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ test('custom body', async t => {
112112
t.is(error.body, 'not');
113113
});
114114

115+
test('contains Got options', async t => {
116+
const options = {
117+
url: s.url,
118+
auth: 'foo:bar'
119+
};
120+
121+
const error = await t.throwsAsync(got(options));
122+
t.is(error.gotOptions.auth, options.auth);
123+
});
124+
115125
test('no status message is overriden by the default one', async t => {
116126
const error = await t.throwsAsync(got(`${s.url}/no-status-message`));
117127
t.is(error.statusCode, 400);

‎test/http.js

+9
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,12 @@ test('requestUrl response when sending url as param', async t => {
9191
test('response contains url', async t => {
9292
t.is((await got(s.url)).url, `${s.url}/`);
9393
});
94+
95+
test('response contains got options', async t => {
96+
const options = {
97+
url: s.url,
98+
auth: 'foo:bar'
99+
};
100+
101+
t.is((await got(options)).request.gotOptions.auth, options.auth);
102+
});

0 commit comments

Comments
 (0)
Please sign in to comment.