Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
fix(api): correctly handle responses with missing content-type header
Browse files Browse the repository at this point in the history
Also prevents non .tgz requests from being handled as tgz requests — the previous if condition was incorrect
  • Loading branch information
Emelia Smith committed Aug 8, 2019
1 parent 40a25a2 commit 2049022
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/utils/api.test.ts
Expand Up @@ -11,6 +11,22 @@ describe('api', () => {
};

describe('handleResponseType', () => {
test('should handle missing Content-Type', async () => {
const response: Response = {
url: 'http://localhost:8080/-/packages',
ok: false,
// @ts-ignore
headers: {
get: () => null,
} as Headers,
} as Response;

const handled = await handleResponseType(response);

// Should this actually return [false, null] ?
expect(handled).toBeUndefined();
});

test('should test tgz scenario', async () => {
const blob = new Blob(['foo']);
const blobPromise = Promise.resolve<Blob>(blob);
Expand Down
10 changes: 5 additions & 5 deletions src/utils/api.ts
Expand Up @@ -8,20 +8,20 @@ import '../../types';
*/
export function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
if (response.headers) {
const contentType = response.headers.get('Content-Type') as string;
if (contentType.includes('application/pdf')) {
const contentType = response.headers.get('Content-Type');
if (contentType && contentType.includes('application/pdf')) {
return Promise.all([response.ok, response.blob()]);
}
if (contentType.includes('application/json')) {
if (contentType && contentType.includes('application/json')) {
return Promise.all([response.ok, response.json()]);
}
// it includes all text types
if (contentType.includes('text/')) {
if (contentType && contentType.includes('text/')) {
return Promise.all([response.ok, response.text()]);
}

// unfortunatelly on download files there is no header available
if (response.url && response.url.endsWith('.tgz') !== null) {
if (response.url && response.url.endsWith('.tgz') === true) {
return Promise.all([response.ok, response.blob()]);
}
}
Expand Down

0 comments on commit 2049022

Please sign in to comment.