Merge pull request #2031 from ing-bank/fix/empty-response

fix: avoid JSON.parsing empty responses
This commit is contained in:
Pascal Schilp 2023-07-06 12:02:43 +02:00 committed by GitHub
commit ecf63407ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"@lion/ajax": patch
---
fix: avoid JSON.parsing empty responses

View file

@ -193,9 +193,11 @@ export class Ajax {
/** @type {any} */ /** @type {any} */
let body = responseText; let body = responseText;
if ( if (
!response.headers.get('content-type') || body.length &&
response.headers.get('content-type')?.includes('json') (!response.headers.get('content-type') ||
response.headers.get('content-type')?.includes('json'))
) { ) {
try { try {
body = JSON.parse(responseText); body = JSON.parse(responseText);

View file

@ -221,6 +221,13 @@ describe('Ajax', () => {
} }
expect(thrown).to.be.true; expect(thrown).to.be.true;
}); });
it('doesnt throw on empty response', async () => {
fetchStub.returns(Promise.resolve(new Response('', responseInit())));
const { response } = await ajax.fetchJson('/foo');
expect(response.ok);
});
}); });
describe('request and response interceptors', () => { describe('request and response interceptors', () => {