Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Ensure all arguments are passed to base fetch function #611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/fetch-handler.js
Expand Up @@ -31,11 +31,11 @@ const patchNativeFetchForSafari = (nativeFetch) => {
return nativeFetch;
}
// It seems the code is working on Safari thus patch native fetch to avoid the error.
return async (request) => {
return async (request, ...args) => {
const { method } = request;
if (!['POST', 'PUT', 'PATCH'].includes(method)) {
// No patch is required in this case
return nativeFetch(request);
return nativeFetch(request, ...args);
}
const body = await request.clone().text();
const {
Expand Down
41 changes: 41 additions & 0 deletions test/specs/config/safari.test.js
@@ -0,0 +1,41 @@
const chai = require('chai');
const expect = chai.expect;

const { fetchMock, theGlobal } = testGlobals;

describe('Safari override', () => {
beforeEach(() => {
fetchMock.createInstance();
});

it('passes all GET arguments to next function when not under Safari', async () => {
theGlobal.fetch = async (...args) => {
expect(args[0]).to.equal('http://mocked.com/');
expect(args[1]).to.deep.equal({ method: 'GET' });
return { status: 202 };
};
fetchMock.spy('http://mocked.com');
const res = await fetchMock.fetchHandler('http://mocked.com', {
method: 'GET',
});
expect(res.status).to.equal(202);
fetchMock.restore();
delete theGlobal.fetch;
});

it('passes all GET arguments to next function under Safari', async () => {
theGlobal.navigator = { vendor: 'Apple Computer, Inc.' };
theGlobal.fetch = async (...args) => {
expect(args[0]).to.equal('http://mocked.com/');
expect(args[1]).to.deep.equal({ method: 'GET' });
return { status: 202 };
};
fetchMock.spy('http://mocked.com');
const res = await fetchMock.fetchHandler('http://mocked.com', {
method: 'GET',
});
expect(res.status).to.equal(202);
fetchMock.restore();
delete theGlobal.fetch;
});
});