Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ describe('OAuthClientCredentialsClient', () => {
});

it('should call fetch with new init if init is not provided', async () => {
(<any>sut)._fetch.and.returnValue(Promise.resolve(fakeAuthorizeResponseBody));

await sut.fetch(route);

const mostRecentCallArgs = (<any>sut)._fetch.calls.mostRecent().args;
Expand All @@ -130,6 +132,17 @@ describe('OAuthClientCredentialsClient', () => {
it('should return result', () => {
expect(result).toEqual(fakeFetchResponseBody);
});

describe('error', () => {
it('should throw error with useful message if fetch returns 401', async () => {
(<any>sut)._fetch.and.resolveTo(createFakeResponseBody(401));

await expectAsync(sut.fetch(route)).toBeRejectedWithError(
Error,
/Could not authenticate/
);
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export class OAuthClientCredentialsClient implements ApiClient {

init.headers['Authorization'] = `${this._tokenType} ${this._accessToken}`;

return this._fetch(url.href, init);
const response = await this._fetch(url.href, init);

if (response.status === 401) {
throw new Error('Could not authenticate, check credentials and try again');
}

return response;
}
}