Skip to content

Commit

Permalink
fix: 馃悰 loadFromURL for application/octet-stream Content-Type (#69)
Browse files Browse the repository at this point in the history
* fix: 馃悰 loadFromURL for application/octet-stream Content-Type

* chore: 馃 update error message
  • Loading branch information
theashraf committed Apr 24, 2024
1 parent f1a4e63 commit a83b8f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/dotlottie-js/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,9 @@ export async function loadFromURL(src: string): Promise<Uint8Array> {

const contentType = response.headers.get('content-type');

if (!contentType?.includes('application/zip')) {
if (!['application/zip', 'application/octet-stream'].includes(contentType || '')) {
throw new DotLottieError(
'Invalid content type provided for .lottie file, expected application/zip',
`Invalid content type for .lottie file, expected application/zip or application/octet-stream, received ${contentType}`,
ErrorCodes.INVALID_DOTLOTTIE,
);
}
Expand Down
17 changes: 16 additions & 1 deletion packages/dotlottie-js/src/tests/utils-browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,27 @@ describe('loadFromUrl', () => {

await expectAsync(loadFromURL(dotLottieURL)).toBeRejectedWith(
new DotLottieError(
'Invalid content type provided for .lottie file, expected application/zip',
'Invalid content type for .lottie file, expected application/zip or application/octet-stream, received text/html',
ErrorCodes.INVALID_DOTLOTTIE,
),
);
});

it('loads a dotlottie from a url with content-type application/octet-stream', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/octet-stream' } })),
);

const dotLottieURL = 'https://lottiefiles.fake/animation/animation.lottie';

const dotLottie = await loadFromURL(dotLottieURL);

expect(dotLottie).toBeDefined();
expect(dotLottie).toBeInstanceOf(Uint8Array);

expect(fetchSpy).toHaveBeenCalledWith(dotLottieURL);
});

it('loads a dotlottie from a url', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/zip' } })),
Expand Down
17 changes: 16 additions & 1 deletion packages/dotlottie-js/src/tests/utils-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,27 @@ describe('loadFromUrl', () => {

await expectAsync(loadFromURL(dotLottieURL)).toBeRejectedWith(
new DotLottieError(
'Invalid content type provided for .lottie file, expected application/zip',
'Invalid content type for .lottie file, expected application/zip or application/octet-stream, received text/html',
ErrorCodes.INVALID_DOTLOTTIE,
),
);
});

it('loads a dotlottie from a url with content-type application/octet-stream', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/octet-stream' } })),
);

const dotLottieURL = 'https://lottiefiles.fake/animation/animation.lottie';

const dotLottie = await loadFromURL(dotLottieURL);

expect(dotLottie).toBeDefined();
expect(dotLottie).toBeInstanceOf(Uint8Array);

expect(fetchSpy).toHaveBeenCalledWith(dotLottieURL);
});

it('loads a dotlottie from a url', async () => {
const fetchSpy = spyOn(typeof window === 'undefined' ? global : window, 'fetch').and.returnValue(
Promise.resolve(new Response(dotLottieAnimation, { headers: { 'content-type': 'application/zip' } })),
Expand Down

0 comments on commit a83b8f5

Please sign in to comment.