diff --git a/readme.md b/readme.md index a27072883..713ac98ce 100644 --- a/readme.md +++ b/readme.md @@ -452,6 +452,14 @@ const instance = got.extend({ 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. +##### request + +Type: `Object` + +**Note:** This is not a [http.ClientRequest](https://nodejs.org/api/http.html#http_class_http_clientrequest). + +- `gotOptions` - The options that were set on this request. + ##### body Type: `string` `Object` *(depending on `options.json`)* @@ -662,7 +670,7 @@ The default Got options. ## Errors -Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier. +Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol`, `url`, and `gotOptions` properties to make debugging easier. In Promise mode, the `response` is attached to the error. diff --git a/source/errors.js b/source/errors.js index 8030ee270..fcf954802 100644 --- a/source/errors.js +++ b/source/errors.js @@ -21,7 +21,8 @@ class GotError extends Error { path: options.path, socketPath: options.socketPath, protocol: options.protocol, - url: options.href + url: options.href, + gotOptions: options }); } } diff --git a/source/request-as-event-emitter.js b/source/request-as-event-emitter.js index d13d793cd..71046271a 100644 --- a/source/request-as-event-emitter.js +++ b/source/request-as-event-emitter.js @@ -92,6 +92,9 @@ module.exports = (options, input) => { response.retryCount = retryCount; response.timings = timings; response.redirectUrls = redirects; + response.request = { + gotOptions: options + }; const rawCookies = response.headers['set-cookie']; if (options.cookieJar && rawCookies) { diff --git a/test/cache.js b/test/cache.js index 0c2dc556d..979f1023a 100644 --- a/test/cache.js +++ b/test/cache.js @@ -110,6 +110,21 @@ test('Redirects are cached and re-used internally', async t => { t.is(firstResponse.body, secondResponse.body); }); +test('Cached response should have got options', async t => { + const endpoint = '/cache'; + const cache = new Map(); + const options = { + url: s.url + endpoint, + auth: 'foo:bar', + cache + }; + + await got(options); + const secondResponse = await got(options); + + t.is(secondResponse.request.gotOptions.auth, options.auth); +}); + test('Cache error throws got.CacheError', async t => { const endpoint = '/no-store'; const cache = {}; diff --git a/test/error.js b/test/error.js index a0bc2edfd..cb81312aa 100644 --- a/test/error.js +++ b/test/error.js @@ -112,6 +112,16 @@ test('custom body', async t => { t.is(error.body, 'not'); }); +test('contains Got options', async t => { + const options = { + url: s.url, + auth: 'foo:bar' + }; + + const error = await t.throwsAsync(got(options)); + t.is(error.gotOptions.auth, options.auth); +}); + test('no status message is overriden by the default one', async t => { const error = await t.throwsAsync(got(`${s.url}/no-status-message`)); t.is(error.statusCode, 400); diff --git a/test/http.js b/test/http.js index f0d64827d..1b86ae998 100644 --- a/test/http.js +++ b/test/http.js @@ -91,3 +91,12 @@ test('requestUrl response when sending url as param', async t => { test('response contains url', async t => { t.is((await got(s.url)).url, `${s.url}/`); }); + +test('response contains got options', async t => { + const options = { + url: s.url, + auth: 'foo:bar' + }; + + t.is((await got(options)).request.gotOptions.auth, options.auth); +});