Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
9b880081
Unverified
Commit
9b880081
authored
Mar 13, 2019
by
Robert Knight
Committed by
GitHub
Mar 13, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #998 from hypothesis/replace-http-with-fetch-in-api-routes
Replace $http with fetch in api-routes service
parents
a83852e0
090e0f05
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
18 deletions
+20
-18
api-routes.js
src/sidebar/services/api-routes.js
+4
-4
api-routes-test.js
src/sidebar/services/test/api-routes-test.js
+16
-14
No files found.
src/sidebar/services/api-routes.js
View file @
9b880081
...
@@ -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
()
;
});
});
}
}
...
...
src/sidebar/services/test/api-routes-test.js
View file @
9b880081
...
@@ -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'
);
};
f
akeHttp
.
get
f
etchStub
.
withArgs
(
'https://annotation.service/api/'
)
.
withArgs
(
'https://annotation.service/api/'
)
.
returns
(
httpResponse
(
200
,
apiIndexResponse
));
.
returns
(
httpResponse
(
200
,
apiIndexResponse
));
f
akeHttp
.
get
f
etchStub
.
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
);
}
}
);
);
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment