-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #852 from InseeFr/feat/display-better-error-message
feat: improve error message when downloading message
- Loading branch information
Showing
5 changed files
with
81 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { saveFileFromHttpResponse } from './files'; | ||
import FileSaver from 'file-saver'; | ||
|
||
jest.mock('file-saver', () => ({ | ||
saveAs: jest.fn(), | ||
})); | ||
|
||
describe('saveFileFromHttpResponse', () => { | ||
beforeEach(() => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
it('should reject if Content-Disposition header is missing', async () => { | ||
const response = new Response(null, { | ||
headers: new Headers({}), | ||
}); | ||
|
||
await expect(saveFileFromHttpResponse(response)).rejects.toBeUndefined(); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'Unable to download the File due to a missing Content-Disposition header' | ||
); | ||
}); | ||
|
||
it('should reject if Content-Disposition header is invalid', async () => { | ||
const response = new Response(null, { | ||
headers: new Headers({ | ||
'Content-Disposition': 'invalid-header', | ||
}), | ||
}); | ||
|
||
await expect(saveFileFromHttpResponse(response)).rejects.toBeUndefined(); | ||
expect(console.error).toHaveBeenCalledWith( | ||
'Unable to parse the Content-Disposition header' | ||
); | ||
}); | ||
|
||
it('should save file with correct file name from Content-Disposition header', async () => { | ||
const blob = new Blob(['test content'], { type: 'text/plain' }); | ||
const response = new Response(blob, { | ||
headers: new Headers({ | ||
'Content-Disposition': 'attachment; filename="testfile.txt"', | ||
}), | ||
}); | ||
|
||
await saveFileFromHttpResponse(response); | ||
|
||
expect(FileSaver.saveAs).toHaveBeenCalledWith(blob, 'testfile.txt'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import FileSaver from 'file-saver'; | ||
|
||
const CONTENT_DISPOSITION_FILENAME_REGEXP = | ||
/filename[^;\n=]*="((['"]).*?\2|[^;\n]*)"/; | ||
|
||
export const saveFileFromHttpResponse = (response: Response) => { | ||
const contentDisposition = response.headers.get('Content-Disposition'); | ||
|
||
if (contentDisposition === null) { | ||
console.error( | ||
'Unable to download the File due to a missing Content-Disposition header' | ||
); | ||
return Promise.reject(); | ||
} | ||
|
||
const matches = contentDisposition.match(CONTENT_DISPOSITION_FILENAME_REGEXP); | ||
|
||
if (matches === null) { | ||
console.error('Unable to parse the Content-Disposition header'); | ||
return Promise.reject(); | ||
} | ||
|
||
const fileName = matches[1]; | ||
|
||
return response.blob().then((blob) => FileSaver.saveAs(blob, fileName)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters