Unverified Commit 9b880081 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #998 from hypothesis/replace-http-with-fetch-in-api-routes

Replace $http with fetch in api-routes service
parents a83852e0 090e0f05
...@@ -6,7 +6,7 @@ const { retryPromiseOperation } = require('../util/retry'); ...@@ -6,7 +6,7 @@ const { retryPromiseOperation } = require('../util/retry');
* A service which fetches and caches API route metadata. * A service which fetches and caches API route metadata.
*/ */
// @ngInject // @ngInject
function apiRoutes($http, settings) { function apiRoutes(settings) {
// Cache of route name => route metadata from API root. // Cache of route name => route metadata from API root.
let routeCache; let routeCache;
// Cache of links to pages on the service fetched from the API's "links" // Cache of links to pages on the service fetched from the API's "links"
...@@ -19,11 +19,11 @@ function apiRoutes($http, settings) { ...@@ -19,11 +19,11 @@ function apiRoutes($http, settings) {
'Hypothesis-Client-Version': '__VERSION__', // replaced by versionify 'Hypothesis-Client-Version': '__VERSION__', // replaced by versionify
}, },
}; };
return $http.get(url, config).then(({ status, data }) => { return fetch(url, config).then(response => {
if (status !== 200) { if (response.status !== 200) {
throw new Error(`Fetching ${url} failed`); throw new Error(`Fetching ${url} failed`);
} }
return data; return response.json();
}); });
} }
......
...@@ -38,24 +38,22 @@ const linksResponse = { ...@@ -38,24 +38,22 @@ const linksResponse = {
describe('sidebar.api-routes', () => { describe('sidebar.api-routes', () => {
let apiRoutes; let apiRoutes;
let fakeHttp;
let fakeSettings; let fakeSettings;
function httpResponse(status, data) { function httpResponse(status, data) {
return Promise.resolve({ status, data }); return Promise.resolve({ status, json: () => Promise.resolve(data) });
} }
beforeEach(() => { beforeEach(() => {
// Use a Sinon stub rather than Angular's fake $http service here to avoid // We use a simple sinon stub of `fetch` here rather than `fetch-mock`
// the hassles that come with mixing `$q` and regular promises. // because this service's usage of fetch is very simple and it makes it
fakeHttp = { // easier to mock the retry behavior.
get: sinon.stub(), const fetchStub = sinon.stub(window, 'fetch');
};
fakeHttp.get fetchStub
.withArgs('https://annotation.service/api/') .withArgs('https://annotation.service/api/')
.returns(httpResponse(200, apiIndexResponse)); .returns(httpResponse(200, apiIndexResponse));
fakeHttp.get fetchStub
.withArgs('https://annotation.service/api/links') .withArgs('https://annotation.service/api/links')
.returns(httpResponse(200, linksResponse)); .returns(httpResponse(200, linksResponse));
...@@ -63,7 +61,11 @@ describe('sidebar.api-routes', () => { ...@@ -63,7 +61,11 @@ describe('sidebar.api-routes', () => {
apiUrl: 'https://annotation.service/api/', apiUrl: 'https://annotation.service/api/',
}; };
apiRoutes = apiRoutesFactory(fakeHttp, fakeSettings); apiRoutes = apiRoutesFactory(fakeSettings);
});
afterEach(() => {
window.fetch.restore();
}); });
describe('#routes', () => { describe('#routes', () => {
...@@ -78,13 +80,13 @@ describe('sidebar.api-routes', () => { ...@@ -78,13 +80,13 @@ describe('sidebar.api-routes', () => {
return Promise.all([apiRoutes.routes(), apiRoutes.routes()]).then( return Promise.all([apiRoutes.routes(), apiRoutes.routes()]).then(
([routesA, routesB]) => { ([routesA, routesB]) => {
assert.equal(routesA, routesB); assert.equal(routesA, routesB);
assert.equal(fakeHttp.get.callCount, 1); assert.equal(window.fetch.callCount, 1);
} }
); );
}); });
it('retries the route fetch until it succeeds', () => { it('retries the route fetch until it succeeds', () => {
fakeHttp.get.onFirstCall().returns(httpResponse(500, null)); window.fetch.onFirstCall().returns(httpResponse(500, null));
return apiRoutes.routes().then(routes => { return apiRoutes.routes().then(routes => {
assert.deepEqual(routes, apiIndexResponse.links); assert.deepEqual(routes, apiIndexResponse.links);
}); });
...@@ -92,7 +94,7 @@ describe('sidebar.api-routes', () => { ...@@ -92,7 +94,7 @@ describe('sidebar.api-routes', () => {
it('sends client version custom request header', () => { it('sends client version custom request header', () => {
return apiRoutes.routes().then(() => { return apiRoutes.routes().then(() => {
assert.calledWith(fakeHttp.get, fakeSettings.apiUrl, { assert.calledWith(window.fetch, fakeSettings.apiUrl, {
headers: { 'Hypothesis-Client-Version': '__VERSION__' }, headers: { 'Hypothesis-Client-Version': '__VERSION__' },
}); });
}); });
...@@ -112,7 +114,7 @@ describe('sidebar.api-routes', () => { ...@@ -112,7 +114,7 @@ describe('sidebar.api-routes', () => {
return Promise.all([apiRoutes.links(), apiRoutes.links()]).then( return Promise.all([apiRoutes.links(), apiRoutes.links()]).then(
([linksA, linksB]) => { ([linksA, linksB]) => {
assert.equal(linksA, linksB); assert.equal(linksA, linksB);
assert.deepEqual(fakeHttp.get.callCount, 2); assert.deepEqual(window.fetch.callCount, 2);
} }
); );
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment