From 78f92544d4c2ecd2894241e80ed0bc64b2a73812 Mon Sep 17 00:00:00 2001 From: Stayman Hou Date: Fri, 22 Oct 2021 15:29:44 -0400 Subject: [PATCH] support object key with characters need to be URI encoded --- source/image-handler/image-request.js | 2 +- .../image-handler/test/image-request.spec.js | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 37b318c16..f314cf319 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -237,7 +237,7 @@ class ImageRequest { if (requestType === "Default") { // Decode the image request and return the image key const decoded = this.decodeRequest(event); - return decoded.key; + return decodeURIComponent(decoded.key); } if (requestType === "Thumbor" || requestType === "Custom") { diff --git a/source/image-handler/test/image-request.spec.js b/source/image-handler/test/image-request.spec.js index 2f706514c..7925844fb 100644 --- a/source/image-handler/test/image-request.spec.js +++ b/source/image-handler/test/image-request.spec.js @@ -533,6 +533,41 @@ describe('setup()', function() { expect(imageRequest).toEqual(expectedResult); }); }); + describe('010/defaultImageRequest/WithUriEncodedKey', function() { + it('Should pass when a default image request is provided and populate the ImageRequest object with the proper values and a URI encoded key', async function() { + // Arrange + const event = { + path : '/eyJidWNrZXQiOiJ2YWxpZEJ1Y2tldCIsImtleSI6IiVFNCVCOCVBRCVFNiU5NiU4NyIsImVkaXRzIjp7ImdyYXlzY2FsZSI6dHJ1ZX0sIm91dHB1dEZvcm1hdCI6ImpwZWcifQ==' + } + process.env = { + SOURCE_BUCKETS : "validBucket, validBucket2" + } + // Mock + mockAws.getObject.mockImplementationOnce(() => { + return { + promise() { + return Promise.resolve({ Body: Buffer.from('SampleImageContent\n') }); + } + }; + }); + // Act + const imageRequest = new ImageRequest(s3, secretsManager); + await imageRequest.setup(event); + const expectedResult = { + requestType: 'Default', + bucket: 'validBucket', + key: '中文', + edits: { grayscale: true }, + outputFormat: 'jpeg', + originalImage: Buffer.from('SampleImageContent\n'), + CacheControl: 'max-age=31536000,public', + ContentType: 'image/jpeg' + }; + // Assert + expect(mockAws.getObject).toHaveBeenCalledWith({ Bucket: 'validBucket', Key: '中文' }); + expect(imageRequest).toEqual(expectedResult); + }); + }); }); // ---------------------------------------------------------------------------- // getOriginalImage()