Skip to content

Commit

Permalink
feat: Echo back the url in res.url
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Krems committed Apr 19, 2016
1 parent 5546f04 commit bbb0e6d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/fetch.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function verifyAndExtendResponse(url, options, response) {
Object.defineProperties(response, {
statusCode: { value: response.status },
headers: { get: getCachedHeaders },
url: { value: url },
});

function generateStatusCodeError(code) {
Expand Down
4 changes: 3 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function buildFullUrl(options) {
function request_(options, resolve, reject) {
var hostname = options.hostname;
var setHost = options.setHost;
var fullUrl = buildFullUrl(options);
options.setHost = false;

var req_ = null;
Expand Down Expand Up @@ -142,7 +143,7 @@ function request_(options, resolve, reject) {
function generateStatusCodeError() {
var error = StatusCodeError.create(
res_.statusCode, options.minStatusCode, options.maxStatusCode,
res_.headers, options.method, buildFullUrl(options));
res_.headers, options.method, fullUrl);
res_.rawBody()
.then(parseErrorBody)
.then(null, noop)
Expand All @@ -159,6 +160,7 @@ function request_(options, resolve, reject) {
timing.headers = Date.now() - startTime;

res_ = Object.defineProperties(res, resProperties);
res_.url = fullUrl;
res_.on('error', failAndAbort);

if (!isAcceptableStatus(res.statusCode)) {
Expand Down
7 changes: 7 additions & 0 deletions test/fetch-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ describe('fetch: the basics', function () {
});
});

it('exposes the url on the response', function () {
return fetch(options.baseUrl + '/echo/foo/bar', { qs: { x: 42 } })
.then(function (res) {
assert.equal(options.baseUrl + '/echo/foo/bar?x=42', res.url);
});
});

it('can load using path and baseUrl option', function () {
return fetch('/text/path', { baseUrl: options.baseUrl })
.then(function (res) {
Expand Down

0 comments on commit bbb0e6d

Please sign in to comment.