Skip to content

Commit

Permalink
feat(core): allow any json subtypes in API response (#7040)
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry committed May 21, 2019
1 parent b55e3b8 commit 2268217
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/scripts/modules/core/src/api/ApiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ describe('API Service', function() {
expect(rejected).toBe(true);
});

it('application/foo+json is fine', () => {
spyOn(AuthenticationInitializer, 'reauthenticateUser').and.callFake(noop);
$httpBackend
.expectGET(`${baseUrl}/bad`)
.respond(200, '{"good":"job"}', { 'content-type': 'application/foo+json' });

let rejected = false;
API.one('bad')
.get()
.then(noop, () => (rejected = true));

$httpBackend.flush();
expect((AuthenticationInitializer.reauthenticateUser as Spy).calls.count()).toBe(0);
expect(rejected).toBe(false);
});

it('string responses starting with <html should trigger a reauthentication request and reject', function() {
spyOn(AuthenticationInitializer, 'reauthenticateUser').and.callFake(noop);
$httpBackend.expectGET(`${baseUrl}/fine`).respond(200, 'this is fine');
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/modules/core/src/api/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class API {
return $q((resolve, reject) => {
const contentType = result.headers('content-type');
if (contentType) {
const isJson = contentType.includes('application/json');
const isJson = contentType.match(/application\/(.+\+)?json/); // e.g application/json, application/hal+json
const isZeroLengthHtml = contentType.includes('text/html') && result.data === '';
const isZeroLengthText = contentType.includes('text/plain') && result.data === '';
if (!(isJson || isZeroLengthHtml || isZeroLengthText)) {
Expand Down

0 comments on commit 2268217

Please sign in to comment.