Skip to content

Commit

Permalink
feat(FN-3611): updated and renamed cache middlewear and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zain Kassam committed Jan 27, 2025
1 parent bbb1a5c commit 752ae10
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion libs/common/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './feature-flags';
export * from './create-validation-middleware-for-schema';
export * from './set-no-store-cache-control';
export * from './set-cache-control';
21 changes: 21 additions & 0 deletions libs/common/src/middleware/set-cache-control.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import request from 'supertest';
import express from 'express';
import { HttpStatusCode } from 'axios';
import { setCacheControl } from './set-cache-control';

const app = express();
app.use(setCacheControl);
app.get('/', (_req, res) => res.send());

afterEach(() => {
jest.resetAllMocks();
});

describe('set-no-store-cache-control', () => {
it('should set Cache-Control headers correctly', async () => {
const response = await request(app).get('/');

expect(response.headers['cache-control']).toBe('no-cache, no-store, must-revalidate, max-age=0');
expect(response.status).toBe(HttpStatusCode.Ok);
});
});
15 changes: 15 additions & 0 deletions libs/common/src/middleware/set-cache-control.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Request, Response, NextFunction } from 'express';

/**
* middleware to set Cache-Control to no-store, no-cache, must-revalidate and max-age=0
* stops cached version of a page being rendered when going back to a page (using the browser back button)
* ensures the get controller for the page is called
* @param _req
* @param res
* @param next
*/
export const setCacheControl = (_req: Request, res: Response, next: NextFunction) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0');

next();
};
14 changes: 0 additions & 14 deletions libs/common/src/middleware/set-no-store-cache-control.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import express from 'express';
import { PDC_TEAM_IDS, validateFeeRecordCorrectionFeatureFlagIsEnabled, setNoStoreCacheControl } from '@ukef/dtfs2-common';
import { PDC_TEAM_IDS, validateFeeRecordCorrectionFeatureFlagIsEnabled, setCacheControl } from '@ukef/dtfs2-common';
import { getUtilisationReports } from '../../controllers/utilisation-reports';
import { updateUtilisationReportStatus } from '../../controllers/utilisation-reports/update-utilisation-report-status';
import {
Expand Down Expand Up @@ -162,7 +162,7 @@ utilisationReportsRoutes.post(
utilisationReportsRoutes.get(
'/:reportId/create-record-correction-request/:feeRecordId',
validateFeeRecordCorrectionFeatureFlagIsEnabled,
setNoStoreCacheControl,
setCacheControl,
validateUserTeam([PDC_TEAM_IDS.PDC_RECONCILE]),
validateSqlId('reportId'),
validateSqlId('feeRecordId'),
Expand All @@ -180,7 +180,7 @@ utilisationReportsRoutes.post(

utilisationReportsRoutes.get(
'/:reportId/create-record-correction-request/:feeRecordId/check-the-information',
setNoStoreCacheControl,
setCacheControl,
validateFeeRecordCorrectionFeatureFlagIsEnabled,
validateUserTeam([PDC_TEAM_IDS.PDC_RECONCILE]),
validateSqlId('reportId'),
Expand Down

0 comments on commit 752ae10

Please sign in to comment.