Skip to content

Commit

Permalink
fix(core): Set url on the fetch Response object (#44)
Browse files Browse the repository at this point in the history
Resolved #43.
  • Loading branch information
offirgolan authored Jun 27, 2018
1 parent 02a4767 commit f5980cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 187 deletions.
20 changes: 18 additions & 2 deletions packages/@pollyjs/core/src/adapters/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Adapter from '@pollyjs/adapter';
import serializeHeaders from './utils/serialize-headers';
import URL from 'url-parse';

const nativeFetch = self.fetch;
const { defineProperty } = Object;

export default class FetchAdapter extends Adapter {
onConnect() {
Expand Down Expand Up @@ -68,12 +70,26 @@ export default class FetchAdapter extends Adapter {
async respond(pollyRequest, status, headers, body) {
await pollyRequest.respond(status, headers, body);

const { response } = pollyRequest;
const { url, response } = pollyRequest;

return new Response(response.body, {
const fetchResponse = new Response(response.body, {
status: response.statusCode,
headers: response.headers
});

/*
Response does not allow `url` to be set manually (either via the
constructor or assignment) so force the url property via `defineProperty`.
*/
defineProperty(fetchResponse, 'url', {
/*
Since `url` can be a relative url and Response always has an absolute
one, use URL to attach the host if necessary.
*/
value: new URL(url).href
});

return fetchResponse;
}

toString() {
Expand Down
Loading

0 comments on commit f5980cf

Please sign in to comment.