From 40398107184d60e95dd3f9b02e7e2e412c44268b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Borgesen?= Date: Fri, 5 Jan 2024 15:29:56 +0100 Subject: [PATCH] Fix replacing host from environment variable AWS_ENDPOINT_URL (#238) --- spec/unit/index.spec.js | 23 ++++++++++++++++++++++- src/index.js | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spec/unit/index.spec.js b/spec/unit/index.spec.js index 2a064d7..62efb7a 100644 --- a/spec/unit/index.spec.js +++ b/spec/unit/index.spec.js @@ -177,7 +177,28 @@ describe("LocalstackPlugin", () => { expect(request.called).to.be.true; let templateUrl = request.firstCall.args[2].TemplateURL; // url should either start with 'http://localhost' or 'http://127.0.0.1 - expect(templateUrl).to.satisfy((url) => url.startsWith(`${config.host}`) || url.startsWith('http://127.0.0.1')); + expect(templateUrl).to.satisfy((url) => url === `${config.host}:4566/path/to/template` || url === 'http://127.0.0.1:4566/path/to/template'); + }); + + it('should overwrite the S3 hostname with the value from environment variable', async () => { + // Set environment variable to overwrite the default host + process.env.AWS_ENDPOINT_URL = 'http://localstack:4566'; + + const pathToTemplate = 'https://s3.amazonaws.com/path/to/template'; + const request = sinon.stub(awsProvider, 'request'); + instance = new LocalstackPlugin(serverless, defaultPluginState); + await simulateBeforeDeployHooks(instance); + + await awsProvider.request('s3', 'foo', { + TemplateURL: pathToTemplate + }); + + // Remove the environment variable again to not affect other tests + delete process.env.AWS_ENDPOINT_URL + + expect(request.called).to.be.true; + let templateUrl = request.firstCall.args[2].TemplateURL; + expect(templateUrl).to.equal('http://localstack:4566/path/to/template'); }); it('should not send validateTemplate calls to localstack', async () => { diff --git a/src/index.js b/src/index.js index e47f20d..c51e6f1 100644 --- a/src/index.js +++ b/src/index.js @@ -784,7 +784,7 @@ class LocalstackPlugin { if (hostname && url.hostname === 'localhost') { url.hostname = hostname; } - return url.href; + return url.origin; } log(msg) {