Skip to content

Commit

Permalink
feat(core): allow yaml responses from ApiService (#7789)
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry committed Jan 17, 2020
1 parent 6815bc0 commit c2c3b7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/scripts/modules/core/src/api/ApiService.spec.ts
Expand Up @@ -86,6 +86,22 @@ describe('API Service', function() {
expect(rejected).toBe(false);
});

it('application/x-yaml;charset=utf-8 is fine, too', () => {
spyOn(AuthenticationInitializer, 'reauthenticateUser').and.callFake(noop);
$httpBackend
.expectGET(`${baseUrl}/yaml`)
.respond(200, '---\nfoo: bar', { 'content-type': 'application/x-yaml;charset=utf-8' });

let rejected = false;
API.one('yaml')
.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
4 changes: 3 additions & 1 deletion app/scripts/modules/core/src/api/ApiService.ts
Expand Up @@ -42,9 +42,11 @@ export class API {
const contentType = result.headers('content-type');
if (contentType) {
const isJson = contentType.match(/application\/(.+\+)?json/); // e.g application/json, application/hal+json
// e.g. application/yaml, application/x-yaml; it's regex, let's not get too fancy
const isYaml = contentType.match(/application\/(.+-)?yaml/);
const isZeroLengthHtml = contentType.includes('text/html') && result.data === '';
const isZeroLengthText = contentType.includes('text/plain') && result.data === '';
if (!(isJson || isZeroLengthHtml || isZeroLengthText)) {
if (!(isJson || isYaml || isZeroLengthHtml || isZeroLengthText)) {
AuthenticationInitializer.reauthenticateUser();
reject(new InvalidAPIResponse(API.invalidContentMessage, result));
}
Expand Down

0 comments on commit c2c3b7d

Please sign in to comment.