Skip to content

Commit

Permalink
feat(Cache): allow disabling cache on request (default behavior)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemoustachiste committed Jan 23, 2023
1 parent 4ffb4e2 commit 309b4a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/services/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
Expand Down Expand Up @@ -56,6 +57,10 @@ export default async function request (obj: IRequestParameters): Promise<any> {
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 {
Expand Down
50 changes: 49 additions & 1 deletion tests/services/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 309b4a6

Please sign in to comment.