Skip to content

Commit

Permalink
fix(bus): httpCommandDispatcher headers processing
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanReznichenko committed Apr 25, 2022
1 parent c22e8ab commit ed0a5e6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
64 changes: 64 additions & 0 deletions packages/bus/src/dispatchers/HttpCommandDispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,70 @@ describe('HttpCommandDispatcher', () => {
expect(scope.isDone()).toBe(true);
});

it('should not override an authorization header', async () => {
// arrange
const command = new HttpRequest({
payload: { foo: 'bar' },
url: '/api/test',
method: 'POST',
expectReply: false,
headers: { authorization: 'overridden header' }
});
const scope = nock(baseUrl)
.post('/api/test')
.matchHeader('authorization', `api-key ${options.token}`)
.reply(204);

// act
await axiosDispatcher.execute(command);

// assert
expect(scope.isDone()).toBe(true);
});

it('should not override an correlation ID header', async () => {
// arrange
const command = new HttpRequest({
payload: { foo: 'bar' },
url: '/api/test',
method: 'POST',
expectReply: false,
headers: { 'x-correlation-id': 'overridden correlation ID' }
});
const scope = nock(baseUrl)
.post('/api/test')
.matchHeader('x-correlation-id', command.correlationId)
.reply(204);

// act
await axiosDispatcher.execute(command);

// assert
expect(scope.isDone()).toBe(true);
});

it('should not override an date header', async () => {
// arrange
const command = new HttpRequest({
payload: { foo: 'bar' },
url: '/api/test',
method: 'POST',
expectReply: false,
createdAt: new Date(0),
headers: { date: 'overridden date' }
});
const scope = nock(baseUrl)
.post('/api/test')
.matchHeader('date', command.createdAt.toISOString())
.reply(204);

// act
await axiosDispatcher.execute(command);

// assert
expect(scope.isDone()).toBe(true);
});

it('should get a reply', async () => {
// arrange
const command = new HttpRequest({
Expand Down
7 changes: 5 additions & 2 deletions packages/bus/src/dispatchers/HttpCommandDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ export class HttpCommandDispatcher implements CommandDispatcher {
ttl: timeout
} = command;

delete headers?.authorization;

return {
url,
method,
data,
timeout,
params,
responseType: 'text',
headers: {
...headers,
'x-correlation-id': correlationId,
'date': createdAt.toISOString(),
...headers
'date': createdAt.toISOString()
},
...(!expectReply ? { responseType: 'stream' } : {})
};
Expand Down

0 comments on commit ed0a5e6

Please sign in to comment.