1
0
mirror of https://github.com/SomboChea/ui synced 2024-11-28 08:54:27 +07:00

Merge pull request #112 from ThisIsMissEm/master

fix(api): correctly handle responses with missing content-type header
This commit is contained in:
Juan Picado @jotadeveloper 2019-08-10 22:22:19 +02:00 committed by GitHub
commit 1446c8e5fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -11,6 +11,22 @@ describe('api', () => {
}; };
describe('handleResponseType', () => { 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 () => { test('should test tgz scenario', async () => {
const blob = new Blob(['foo']); const blob = new Blob(['foo']);
const blobPromise = Promise.resolve<Blob>(blob); const blobPromise = Promise.resolve<Blob>(blob);

View File

@ -8,20 +8,20 @@ import '../../types';
*/ */
export function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> { export function handleResponseType(response: Response): Promise<[boolean, Blob | string]> | Promise<void> {
if (response.headers) { if (response.headers) {
const contentType = response.headers.get('Content-Type') as string; const contentType = response.headers.get('Content-Type');
if (contentType.includes('application/pdf')) { if (contentType && contentType.includes('application/pdf')) {
return Promise.all([response.ok, response.blob()]); 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()]); return Promise.all([response.ok, response.json()]);
} }
// it includes all text types // it includes all text types
if (contentType.includes('text/')) { if (contentType && contentType.includes('text/')) {
return Promise.all([response.ok, response.text()]); return Promise.all([response.ok, response.text()]);
} }
// unfortunatelly on download files there is no header available // 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()]); return Promise.all([response.ok, response.blob()]);
} }
} }