From 4ffb4e2df459eac425eaa5c87e067ddd51c57c18 Mon Sep 17 00:00:00 2001 From: Julien Fraichot Date: Mon, 23 Jan 2023 13:40:33 +0100 Subject: [PATCH 1/2] fix(Headers): headers should be set when request is open --- src/services/request.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/request.ts b/src/services/request.ts index ddbd097..8bf5a4b 100644 --- a/src/services/request.ts +++ b/src/services/request.ts @@ -25,10 +25,6 @@ export default async function request (obj: IRequestParameters): Promise { const XHR = typeof XMLHttpRequest === 'undefined' ? xhrPolyfill : XMLHttpRequest; const request: XMLHttpRequest = new XHR(); - if (obj['bearer-token']) { - request.setRequestHeader('Authorization', `Bearer ${obj['bearer-token']}`); - } - request.onload = () => { if (request.status >= 200 && request.status < 300) { resolve(request.responseText); @@ -56,6 +52,10 @@ export default async function request (obj: IRequestParameters): Promise { request.open(obj.method || 'GET', url); + if (obj['bearer-token']) { + request.setRequestHeader('Authorization', `Bearer ${obj['bearer-token']}`); + } + if (obj.body) { request.send(JSON.stringify(obj.body)); } else { From 309b4a62b5c4a8b950cd08c41a51290a2b522613 Mon Sep 17 00:00:00 2001 From: Julien Fraichot Date: Mon, 23 Jan 2023 17:34:30 +0100 Subject: [PATCH 2/2] feat(Cache): allow disabling cache on request (default behavior) --- src/services/request.ts | 5 ++++ tests/services/request.test.ts | 50 +++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/services/request.ts b/src/services/request.ts index 8bf5a4b..5e0205b 100644 --- a/src/services/request.ts +++ b/src/services/request.ts @@ -6,6 +6,7 @@ export interface IRequestParameters { body?: any; forceHttp?: boolean; 'bearer-token'?: string; + 'allow-cache'?: boolean; // default false } export default async function request (obj: IRequestParameters): Promise { @@ -56,6 +57,10 @@ export default async function request (obj: IRequestParameters): Promise { request.setRequestHeader('Authorization', `Bearer ${obj['bearer-token']}`); } + if (!obj['allow-cache']) { + request.setRequestHeader('Cache-Control', 'no-cache, no-store, max-age=0'); + } + if (obj.body) { request.send(JSON.stringify(obj.body)); } else { diff --git a/tests/services/request.test.ts b/tests/services/request.test.ts index 5208c67..9b9b95c 100644 --- a/tests/services/request.test.ts +++ b/tests/services/request.test.ts @@ -34,7 +34,9 @@ function MockXMLHttpRequestFactory ({ isSuccessCase }: { isSuccessCase: boolean onload (): any {} // eslint-disable-next-line @typescript-eslint/no-empty-function - setRequestHeader (name: string, value: string): any {} + setRequestHeader (name: string, value: string): any { + this.headers[name] = value; + } }; } @@ -99,6 +101,52 @@ describe('Services Request test suite', function () { }); }); + describe('given the allow-cache option is undefined', function () { + it('should set the Cache-Control header to disallow caching', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com' + }); + expect(setRequestHeaderStub.getCall(0).args).toEqual(['Cache-Control', 'no-cache, no-store, max-age=0']); + }); + }); + + describe('given the allow-cache option is false', function () { + it('should set the Cache-Control header to disallow caching', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com', + 'allow-cache': false + }); + expect(setRequestHeaderStub.getCall(0).args).toEqual(['Cache-Control', 'no-cache, no-store, max-age=0']); + }); + }); + + describe('given the allow-cache option is true', function () { + it('should not set the Cache-Control header', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com', + 'allow-cache': true + }); + expect(setRequestHeaderStub.calledOnce).toBe(false); + }); + }); + + describe('given the allow-cache option is false', function () { + it('should set the Cache-Control header to disallow caching', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com' + }); + expect(setRequestHeaderStub.getCall(0).args).toEqual(['Cache-Control', 'no-cache, no-store, max-age=0']); + }); + }); + describe('method option', function () { let openStub;