Skip to content

Commit

Permalink
fix(adapter-fetch): Correctly handle key/value pairs headers
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored and jasonmit committed Mar 6, 2019
1 parent 3c10e74 commit dc0323d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
7 changes: 4 additions & 3 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export default class FetchAdapter extends Adapter {
);
}

this.assert('Fetch global not found.', !!(context && context.fetch));
this.assert('Response global not found.', !!(context && context.Response));
['fetch', 'Response', 'Headers'].forEach(key =>
this.assert(`${key} global not found.`, !!(context && context[key]))
);
this.assert(
'Running concurrent fetch adapters is unsupported, stop any running Polly instances.',
!context.fetch[IS_STUBBED]
Expand All @@ -39,7 +40,7 @@ export default class FetchAdapter extends Adapter {
const pollyRequest = await this.handleRequest({
url,
method: options.method || 'GET',
headers: serializeHeaders(options.headers),
headers: serializeHeaders(new context.Headers(options.headers)),
body: options.body,
requestArguments: { options }
});
Expand Down
58 changes: 51 additions & 7 deletions packages/@pollyjs/adapter-fetch/tests/integration/adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FetchAdapter from '../../src';
import pollyConfig from '../utils/polly-config';

class MockResponse {}
class MockHeaders {}

describe('Integration | Fetch Adapter', function() {
setupPolly.beforeEach(pollyConfig);
Expand All @@ -27,6 +28,25 @@ describe('Integration | Fetch Adapter', function() {

expect(res.status).to.equal(200);
});

it('should support array of key/value pair headers', async function() {
const { server } = this.polly;
let headers;

server
.any(this.recordUrl())
.on('request', req => {
headers = req.headers;
})
.intercept((_, res) => res.sendStatus(200));

const res = await this.fetchRecord({
headers: [['Content-Type', 'application/json']]
});

expect(res.status).to.equal(200);
expect(headers).to.deep.equal({ 'content-type': 'application/json' });
});
});

describe('Integration | Fetch Adapter | Init', function() {
Expand All @@ -35,7 +55,9 @@ describe('Integration | Fetch Adapter | Init', function() {
const polly = new Polly('context', { adapters: [] });
const fetch = () => {};
const adapterOptions = {
fetch: { context: { fetch, Response: MockResponse } }
fetch: {
context: { fetch, Response: MockResponse, Headers: MockHeaders }
}
};

polly.configure({
Expand All @@ -52,12 +74,12 @@ describe('Integration | Fetch Adapter | Init', function() {
polly.configure({
adapterOptions: { fetch: { context: {} } }
});
}).to.throw(/Fetch global not found/);
}).to.throw(/fetch global not found/);

await polly.stop();
});

it('should throw when context, fetch, and Response are undefined', async function() {
it('should throw when context, fetch, Response, and Headers are undefined', async function() {
const polly = new Polly('context', { adapters: [] });

polly.configure({
Expand All @@ -68,24 +90,46 @@ describe('Integration | Fetch Adapter | Init', function() {
polly.configure({
adapterOptions: { fetch: { context: undefined } }
});
}).to.throw(/Fetch global not found/);
}).to.throw(/fetch global not found/);

expect(function() {
polly.configure({
adapterOptions: {
fetch: { context: { fetch: undefined } }
fetch: {
context: {
fetch: undefined,
Response: MockResponse,
Headers: MockHeaders
}
}
}
});
}).to.throw(/Fetch global not found/);
}).to.throw(/fetch global not found/);

expect(function() {
polly.configure({
adapterOptions: {
fetch: { context: { fetch() {}, Response: undefined } }
fetch: {
context: { fetch() {}, Response: undefined, Headers: MockHeaders }
}
}
});
}).to.throw(/Response global not found/);

expect(function() {
polly.configure({
adapterOptions: {
fetch: {
context: {
fetch() {},
Response: MockResponse,
Headers: undefined
}
}
}
});
}).to.throw(/Headers global not found/);

await polly.stop();
});
});
Expand Down

0 comments on commit dc0323d

Please sign in to comment.