Skip to content

Commit

Permalink
[TECH] Améliorer la récupération du timestamp d'une requête (PIX-16871)…
Browse files Browse the repository at this point in the history
… (#11579)
  • Loading branch information
yannbertrand authored Mar 7, 2025
2 parents 93b63ec + cc24a3e commit dbeeebf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function extractLocaleFromRequest(request) {
}

function extractTimestampFromRequest(request) {
return request.headers?.['X-Request-Start'] ?? new Date().getTime();
return request.info.received;
}

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
extractTimestampFromRequest,
extractUserIdFromRequest,
} from '../../../../../src/shared/infrastructure/utils/request-response-utils.js';
import { expect, generateAuthenticatedUserRequestHeaders, sinon } from '../../../../test-helper.js';
import { expect, generateAuthenticatedUserRequestHeaders } from '../../../../test-helper.js';

const { ENGLISH_SPOKEN, FRENCH_FRANCE, FRENCH_SPOKEN } = LOCALE;

Expand Down Expand Up @@ -84,46 +84,20 @@ describe('Unit | Utils | Request Utils', function () {
});

describe('#extractTimestampFromRequest', function () {
context('when "X-Request-Start" header exist', function () {
it('returns the value of the header', function () {
// given
const startDateTimestamp = new Date('2025-01-01').getTime();
const request = {
headers: {
'X-Request-Start': startDateTimestamp,
},
};

// when
const timestamp = extractTimestampFromRequest(request);

// then
expect(timestamp).to.equal(startDateTimestamp);
});
});

context('when "X-Request-Start" header does not exist', function () {
let clock, now;

beforeEach(function () {
now = new Date('2023-09-12');
clock = sinon.useFakeTimers({ now, toFake: ['Date'] });
});

afterEach(function () {
clock.restore();
});

it('returns a new date in timestamp', function () {
// given
const request = {};
it('returns the value of attribute "request.info.received"', function () {
// given
const startDateTimestamp = new Date('2025-01-01').getTime();
const request = {
info: {
received: startDateTimestamp,
},
};

// when
const timestamp = extractTimestampFromRequest(request);
// when
const timestamp = extractTimestampFromRequest(request);

// then
expect(timestamp).to.equal(now.getTime());
});
// then
expect(timestamp).to.equal(startDateTimestamp);
});
});
});

0 comments on commit dbeeebf

Please sign in to comment.