Skip to content

Commit 6ff4f78

Browse files
feat: Allow SQS port number to be passed as environmental configuration (#660)
* Allow SQS port number to be passed as environmental configuration * Updating SQS.service.js unit tests, to test local mode host and port * Adding documentation about using LAMBDA_WRAPPER_OFFLINE_SQS_PORT * Fixing linting * Fixing unit test failures upon passing env. variables as LAMBDA_WRAPPER_OFFLINE_SQS_PORT=1234 and LAMBDA_WRAPPER_OFFLINE_SQS_HOST=custom1-host
1 parent 60c29dd commit 6ff4f78

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The URL is likely to be your localhost URL and the next available port from the
8282
Use this mode by setting `LAMBDA_WRAPPER_OFFLINE_SQS_MODE=local`. Messages will still be sent to an SQS queue, but using a locally simulated version instead of AWS. This allows you to test your service using a tool like Localstack.
8383

8484
By default, messages will be sent to a SQS service running on `localhost:4576`. If you need to change the hostname, you can set `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST`.
85+
Also, if you need to change the port, you can set `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT`.
8586

8687
### AWS SQS mode
8788

src/Service/SQS.service.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default class SQSService extends DependencyAwareClass {
6868

6969
const {
7070
LAMBDA_WRAPPER_OFFLINE_SQS_HOST: offlineHost = 'localhost',
71+
LAMBDA_WRAPPER_OFFLINE_SQS_PORT: offlinePort = '4576',
7172
LAMBDA_WRAPPER_OFFLINE_SQS_MODE: offlineMode = SQS_OFFLINE_MODES.DIRECT,
7273
AWS_ACCOUNT_ID,
7374
REGION,
@@ -93,7 +94,7 @@ export default class SQSService extends DependencyAwareClass {
9394
Object.keys(queues).forEach((queueDefinition) => {
9495
if (container.isOffline && offlineMode === SQS_OFFLINE_MODES.LOCAL) {
9596
// custom URL when using an offline SQS service such as Localstack
96-
this.queues[queueDefinition] = `http://${offlineHost}:4576/queue/${queues[queueDefinition]}`;
97+
this.queues[queueDefinition] = `http://${offlineHost}:${offlinePort}/queue/${queues[queueDefinition]}`;
9798
} else {
9899
// default AWS queue URL
99100
this.queues[queueDefinition] = `https://sqs.${REGION}.amazonaws.com/${accountId}/${queues[queueDefinition]}`;

tests/unit/Service/SQS.service.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,23 @@ const getService = ({ sendMessage = null, invoke = null } = {}, isOffline = fals
4949
describe('Service/SQS', () => {
5050
let envAccountId;
5151
let envOfflineSqsMode;
52+
let envOfflineSqsHost;
53+
let envOfflineSqsPort;
5254
let envRegion;
5355

5456
beforeAll(() => {
5557
envAccountId = process.env.AWS_ACCOUNT_ID;
5658
envOfflineSqsMode = process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE;
59+
envOfflineSqsHost = process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST;
60+
envOfflineSqsPort = process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT;
5761
envRegion = process.env.REGION;
5862
});
5963

6064
afterAll(() => {
6165
process.env.AWS_ACCOUNT_ID = envAccountId;
6266
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE = envOfflineSqsMode;
67+
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST = envOfflineSqsHost;
68+
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT = envOfflineSqsPort;
6369
process.env.REGION = envRegion;
6470
});
6571

@@ -113,6 +119,8 @@ describe('Service/SQS', () => {
113119
});
114120

115121
it('sends a local SQS request in "local" mode', async () => {
122+
delete process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST;
123+
delete process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT;
116124
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE = 'local';
117125
const service = getService({}, true);
118126

@@ -123,6 +131,7 @@ describe('Service/SQS', () => {
123131

124132
const params = service.sqs.sendMessage.mock.calls[0][0];
125133
expect(params.QueueUrl).toContain('localhost');
134+
expect(params.QueueUrl).toContain('4576');
126135
});
127136

128137
it('sends a normal SQS request in "aws" mode', async () => {
@@ -136,6 +145,7 @@ describe('Service/SQS', () => {
136145

137146
const params = service.sqs.sendMessage.mock.calls[0][0];
138147
expect(params.QueueUrl).not.toContain('localhost');
148+
expect(params.QueueUrl).not.toContain('4576');
139149
});
140150

141151
it('throws an error for any other mode', async () => {
@@ -160,6 +170,8 @@ describe('Service/SQS', () => {
160170

161171
describe('when container.isOffline === true', () => {
162172
it('should use a LocalStack URL in "local" mode', async () => {
173+
delete process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST;
174+
delete process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT;
163175
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE = 'local';
164176
const service = getService({}, true);
165177

@@ -169,6 +181,30 @@ describe('Service/SQS', () => {
169181
expect(params.QueueUrl).toEqual('http://localhost:4576/queue/QueueName');
170182
});
171183

184+
it('should use a custom host in "local" mode', async () => {
185+
delete process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT;
186+
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE = 'local';
187+
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST = 'custom-host';
188+
const service = getService({}, true);
189+
190+
await service.publish(TEST_QUEUE, { test: 1 });
191+
192+
const params = service.sqs.sendMessage.mock.calls[0][0];
193+
expect(params.QueueUrl).toEqual('http://custom-host:4576/queue/QueueName');
194+
});
195+
196+
it('should use a custom port in "local" mode', async () => {
197+
delete process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST;
198+
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE = 'local';
199+
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_PORT = '4566';
200+
const service = getService({}, true);
201+
202+
await service.publish(TEST_QUEUE, { test: 1 });
203+
204+
const params = service.sqs.sendMessage.mock.calls[0][0];
205+
expect(params.QueueUrl).toEqual('http://localhost:4566/queue/QueueName');
206+
});
207+
172208
it('should use a correctly formed AWS queue URL in "aws" mode', async () => {
173209
// `AWS_ACCOUNT_ID` and `REGION` need to be set for this to work
174210
process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE = 'aws';

0 commit comments

Comments
 (0)