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

use native fetch (if available) #585

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 4.6.0

### Features

- [#585](https://github.com/okta/okta-auth-js/pull/585) Uses native fetch, if available

### Other

- [#583](https://github.com/okta/okta-auth-js/pull/583) Better error handling for redirect flows: if redirect URI contains `error` or `error_description` then `isLoginRedirect` will return true and `parseFromUrl` will throw `OAuthError`
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/fetchRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

import fetch from 'cross-fetch';
import crossFetch from 'cross-fetch';
import { FetchOptions, FetchResponse, HttpResponse } from '../types';

function readData(response: FetchResponse): Promise<object | string> {
Expand Down Expand Up @@ -52,7 +52,7 @@ function fetchRequest(method: string, url: string, args: FetchOptions) {
if (contentType === 'application/json' && body && typeof body !== 'string') {
body = JSON.stringify(body);
}

var fetch = global.fetch || crossFetch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test to make sure global fetch is called if fetch is defined else crossFetch is called.

var fetchPromise = fetch(url, {
method: method,
headers: args.headers,
Expand Down
4 changes: 4 additions & 0 deletions test/karma/spec/loginFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Complete login flow', function() {
var _history;
var _location;
var $app;
var fetch;

beforeEach(function() {
_history = {
Expand All @@ -60,12 +61,15 @@ describe('Complete login flow', function() {
date.setTime(ASSUMED_TIME * 1000);
jasmine.clock().mockDate(date);
jasmine.Ajax.install();
fetch = window.fetch;
window.fetch = null; // disable native fetch, force XHR
});

afterEach(function() {
document.body.removeChild(document.getElementById('root'));
jasmine.clock().uninstall();
jasmine.Ajax.uninstall();
window.fetch = fetch;
});

async function bootstrap(config, pathname) {
Expand Down
5 changes: 4 additions & 1 deletion test/karma/spec/renewToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,24 @@ describe('Renew token', function() {
const JWKS_URI = 'http://myfake.jwks.local';

var sdk;

var fetch;

beforeEach(function() {
document.body.insertAdjacentHTML('beforeend', '<div id="root"></div>');
var date = new Date();
date.setTime(ASSUMED_TIME * 1000);
jasmine.clock().mockDate(date);
jasmine.Ajax.install();
fetch = window.fetch;
window.fetch = null; // disable native fetch, force XHR
});

afterEach(function() {
document.body.removeChild(document.getElementById('root'));
jasmine.clock().uninstall();
jasmine.Ajax.uninstall();
token.$imports.$restore();
window.fetch = fetch;
});

function bootstrap(config) {
Expand Down
29 changes: 29 additions & 0 deletions test/spec/fetch-request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global window */
describe('fetchRequest', function () {
let fetchSpy;

Expand Down Expand Up @@ -45,6 +46,34 @@ describe('fetchRequest', function () {
requestUrl = 'http://fakey.local';
});

describe('fetch implementation', () => {
let defaultFetch;
beforeEach(() => {
defaultFetch = window.fetch;
window.fetch = null;
});
afterEach(() => {
window.fetch = defaultFetch;
});
it('uses cross-fetch if no native fetch', () => {
return fetchRequest(requestMethod, requestUrl, {})
.then(() => {
expect(fetchSpy).toHaveBeenCalled();
});
});
it('uses native fetch if available', () => {
const globalFetch = jest.fn(() => {
return Promise.resolve(response);
});
window.fetch = globalFetch;
return fetchRequest(requestMethod, requestUrl, {})
.then(() => {
expect(globalFetch).toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
});

describe('request', () => {
it('JSON encodes request body if request header Content-Type is application/json', function() {
const requestJSON = {
Expand Down