Skip to content

Commit

Permalink
Merge pull request #764 from snyk/fix/make-primary-srv-request-to-rig…
Browse files Browse the repository at this point in the history
…ht-place

fix: secondary to primary calls to right endpoint
  • Loading branch information
aarlaud authored May 10, 2024
2 parents f9399dd + ee9c9b2 commit afc4f45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/server/routesHandlers/httpRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const overloadHttpRequestWithConnectionDetailsMiddleware = async (
localHostname.match(regex)
) {
const url = new URL(`http://${req.hostname}${req.url}`);
url.hostname = req.hostname.replace(/-[0-9]{1,2}$/, '');
url.searchParams.append('connection_role', 'primary');
logger.debug({}, 'Making request to primary');
const postFilterPreparedRequest: PostFilterPreparedRequest = {
Expand Down
20 changes: 11 additions & 9 deletions test/unit/old-client-redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jest.mock('node:os', () => {

describe('Testing older clients specific logic', () => {
it('Testing the old client redirected to primary from secondary pods', async () => {
nock(`http://127.0.0.1`)
nock(`http://my-server-name`)
.persist()
.get(
'/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/path?connection_role=primary',
Expand All @@ -49,14 +49,15 @@ describe('Testing older clients specific logic', () => {
overloadHttpRequestWithConnectionDetailsMiddleware,
);

const response = await request(app).get(
'/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/path',
);
const response = await request(app)
.get('/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/path')
.set('Host', 'my-server-name-1');

expect(response.status).toEqual(200);
expect(response.body).toEqual({ test: 'value' });
});
it('Testing the old client redirected to primary from secondary pods - POST request', async () => {
nock(`http://127.0.0.1`)
nock(`http://my-server-name`)
.persist()
.post(
'/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/path?connection_role=primary',
Expand All @@ -73,6 +74,7 @@ describe('Testing older clients specific logic', () => {

const response = await request(app)
.post('/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/path')
.set('Host', 'my-server-name-1')
.send({ test: 'value2' });

expect(response.status).toEqual(200);
Expand All @@ -82,7 +84,7 @@ describe('Testing older clients specific logic', () => {
const fileJson = JSON.parse(
readFileSync(`${fixtures}/accept/ghe.json`).toString(),
);
nock(`http://127.0.0.1`)
nock(`http://my-server-name`)
.persist()
.get(
'/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/file?connection_role=primary',
Expand All @@ -97,9 +99,9 @@ describe('Testing older clients specific logic', () => {
overloadHttpRequestWithConnectionDetailsMiddleware,
);

const response = await request(app).get(
'/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/file',
);
const response = await request(app)
.get('/broker/7fe7a57b-aa0d-416a-97fc-472061737e25/file')
.set('Host', 'my-server-name-1');

expect(response.status).toEqual(200);
expect(response.body).toEqual(fileJson);
Expand Down

0 comments on commit afc4f45

Please sign in to comment.