Skip to content

Commit

Permalink
Merge pull request #12 from afgallo/feature/sqs-endpoint
Browse files Browse the repository at this point in the history
Add Endpoint Option for Local Development
  • Loading branch information
afgallo authored Jun 2, 2024
2 parents 91f95c1 + 1463d1a commit 2af971d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/adapter/queue-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ module.exports = exports = class QueueAdapter {
*/
constructor(options = {}) {
let credentials = null
let endpoint = null
options.region = options.region || 'us-east-1'

if (options.awsAccessKey && options.awsSecretKey) {
credentials = { accessKeyId: options.awsAccessKey, secretAccessKey: options.awsSecretKey }
}

this.#sqsClient = options.sqsClient || new SQSClient({ region: options.region || 'us-east-1', credentials })
if (options.endpoint) {
endpoint = options.endpoint
}

this.#sqsClient = options.sqsClient || new SQSClient({ region: options.region, credentials, endpoint })
}

#sqsClient

/**
* Get the SQS client instance. Used mainly for unit testing.
*
* @returns {SQSClient} The SQS client instance.
*/
get getSQSClient() {
return this.#sqsClient
}

/**
* Sends a message to the specified queue.
*
Expand Down
14 changes: 14 additions & 0 deletions test/queue-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,18 @@ describe('QueueAdapter', () => {

Sinon.assert.calledOnceWithExactly(consoleErrorStub, 'Failed to send message', error)
})

it('creates a new QueueAdapter with a custom endpoint', async () => {
const endpoint = 'http://localhost:4575' // LocalStack SNS endpoint for local testing
const customQueueAdapter = new QueueAdapter({ endpoint })

expect(customQueueAdapter).to.exist()
expect(customQueueAdapter).to.be.an.instanceof(QueueAdapter)

const client = customQueueAdapter.getSQSClient
const actualEndpoint = await client.config.endpoint()

expect(actualEndpoint.hostname).to.equal('localhost')
expect(actualEndpoint.port).to.equal(4575)
})
})

0 comments on commit 2af971d

Please sign in to comment.