From 4b60c963801374f1b8289b38932dfe34666ff6cf Mon Sep 17 00:00:00 2001 From: ZenMasterJacob20011 Date: Mon, 3 Jun 2024 10:55:30 -0500 Subject: [PATCH 1/2] fallback to passing response in argument if response.body is undefined --- src/sdk/Formio.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sdk/Formio.ts b/src/sdk/Formio.ts index 82858c03..e7f90e35 100644 --- a/src/sdk/Formio.ts +++ b/src/sdk/Formio.ts @@ -1566,13 +1566,13 @@ export class Formio { if (!response.ok) { if (response.status === 440) { Formio.setToken(null, opts); - Formio.events.emit('formio.sessionExpired', response.body); + Formio.events.emit('formio.sessionExpired', response.body || response); } else if (response.status === 401) { - Formio.events.emit('formio.unauthorized', response.body); + Formio.events.emit('formio.unauthorized', response.body || response); } else if (response.status === 416) { - Formio.events.emit('formio.rangeIsNotSatisfiable', response.body); + Formio.events.emit('formio.rangeIsNotSatisfiable', response.body || response); } else if (response.status === 504) { return Promise.reject(new Error('Network request failed')); From e0a24b6a9f811fb7bf66be86d4ec39d24e9dc316 Mon Sep 17 00:00:00 2001 From: ZenMasterJacob20011 Date: Thu, 6 Jun 2024 16:39:13 -0500 Subject: [PATCH 2/2] Added tests --- src/sdk/__tests__/Formio.test.ts | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/sdk/__tests__/Formio.test.ts b/src/sdk/__tests__/Formio.test.ts index 8b01cc2f..e49612ac 100644 --- a/src/sdk/__tests__/Formio.test.ts +++ b/src/sdk/__tests__/Formio.test.ts @@ -2173,4 +2173,60 @@ describe('Formio.js Tests', () => { assert.ok(plugin.wrapStaticRequestPromise.calledOnce, 'wrapStaticRequestPromise should be called once'); }); }); + describe('Formio.request', () => { + it('should emit a formio.sessionExpired event when the response status is 440 and the response object should exist', (done) => { + let eventFired = false + let responseNotUndefined = false + setTimeout(()=>{ + assert(eventFired, 'formio.sessionExpired event was not called'); + assert(responseNotUndefined, 'a response was not passed into the event'); + fetchMock.restore() + done() + },200) + Formio.events.on('formio.sessionExpired', (response: any) => { + eventFired = true + if (response){ + responseNotUndefined = true + } + }) + fetchMock.mock('http://localhost:8080/test', 440); + Formio.request('http://localhost:8080/test'); + }); + it('should emit a formio.unauthorized event when the response status is 401', (done) => { + let eventFired = false + let responseNotUndefined = false + setTimeout(()=>{ + assert(eventFired, 'formio.unauthorized event was not called'); + assert(responseNotUndefined, 'a response was not passed into the event'); + fetchMock.restore() + done() + },200); + Formio.events.on('formio.unauthorized', (response: any) => { + eventFired = true; + if (response){ + responseNotUndefined = true; + } + }) + fetchMock.mock('http://localhost:8080/test', 401); + Formio.request('http://localhost:8080/test'); + }); + it('should emit a formio.rangeIsNotSatisfiable event when the response status is 416', (done) => { + let eventFired = false; + let responseNotUndefined = false; + setTimeout(()=>{ + assert(eventFired, 'formio.rangeIsNotSatisfiable event was not called'); + assert(responseNotUndefined, 'a response was not passed into the event'); + fetchMock.restore() + done() + },200); + Formio.events.on('formio.rangeIsNotSatisfiable', (response) => { + eventFired = true; + if (response) { + responseNotUndefined = true; + } + }) + fetchMock.mock('http://localhost:8080/test', 416); + Formio.request('http://localhost:8080/test'); + }); + }); });