From bf8a6dee7bf4992cb5db59aba3c8c1f84914c704 Mon Sep 17 00:00:00 2001 From: Markus Haack Date: Wed, 2 Jul 2025 18:29:30 +0200 Subject: [PATCH] fix: add more tests for getObject --- test/storage/object/get.test.js | 124 ++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 test/storage/object/get.test.js diff --git a/test/storage/object/get.test.js b/test/storage/object/get.test.js new file mode 100644 index 00000000..4f41c17c --- /dev/null +++ b/test/storage/object/get.test.js @@ -0,0 +1,124 @@ +/* + * Copyright 2025 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import assert from 'node:assert'; +import { + S3Client, + GetObjectCommand, + HeadObjectCommand, +} from '@aws-sdk/client-s3'; +import { mockClient } from 'aws-sdk-client-mock'; +import esmock from 'esmock'; + +const s3Mock = mockClient(S3Client); + +import getObject from '../../../src/storage/object/get.js'; + +const ORG = 'adobe'; +const KEY = 'wknd/index.html'; + +describe('Get Object', () => { + beforeEach(() => { + s3Mock.reset(); + }); + + it('gets object (head = false)', async () => { + const Body = Buffer.from('

hello world

'); + const ContentType = 'text/html'; + const ContentLength = Body.length; + const Metadata = { foo: 'bar' }; + const ETag = 'etag123'; + s3Mock + .on(GetObjectCommand, { + Bucket: `${ORG}-content`, + Key: KEY, + }) + .resolves({ + Body, + ContentType, + ContentLength, + Metadata, + ETag, + $metadata: { httpStatusCode: 200 }, + }); + const resp = await getObject({}, { org: ORG, key: KEY }, false); + assert.strictEqual(resp.status, 200); + assert.strictEqual(resp.contentType, ContentType); + assert.strictEqual(resp.contentLength, ContentLength); + assert.deepStrictEqual(resp.metadata, Metadata); + assert.strictEqual(resp.etag, ETag); + assert.deepStrictEqual(resp.body, Body); + }); + + it('gets object head (head = true)', async () => { + const fakeUrl = 'https://example.com/head'; + let called = false; + // esmock for getSignedUrl and fetch + const getObjectWithMocks = await esmock( + '../../../src/storage/object/get.js', + { + '@aws-sdk/s3-request-presigner': { + getSignedUrl: async () => fakeUrl, + }, + } + ); + + const savedFetch = globalThis.fetch; + try { + globalThis.fetch = async (url, opts) => { + called = true; + assert.strictEqual(url, fakeUrl); + assert.strictEqual(opts.method, 'HEAD'); + return { + status: 200, + headers: { + get: (name) => { + if (name === 'content-type') return 'text/html'; + if (name === 'content-length') return '123'; + if (name === 'etag') return 'etag456'; + return null; + }, + forEach: (cb) => { + cb('bar', 'x-amz-meta-foo'); + }, + }, + }; + }; + + const resp = await getObjectWithMocks({}, { org: ORG, key: KEY }, true); + assert.strictEqual(resp.status, 200); + assert.strictEqual(resp.contentType, 'text/html'); + assert.strictEqual(resp.contentLength, '123'); + assert.deepStrictEqual(resp.metadata, { foo: 'bar' }); + assert.strictEqual(resp.etag, 'etag456'); + assert.strictEqual(resp.body, ''); + assert(called, 'fetch should be called'); + } finally { + globalThis.fetch = savedFetch; + } + }); + + it('returns 404 when object not found (head = false)', async () => { + const error = new Error('Not found'); + error.$metadata = { httpStatusCode: 404 }; + s3Mock.on(GetObjectCommand, { + Bucket: `${ORG}-content`, + Key: KEY, + }).rejects(error); + // Import getObject directly for head=false (no esmock needed) + const getObject = (await import('../../../src/storage/object/get.js')).default; + const resp = await getObject({}, { org: ORG, key: KEY }, false); + assert.strictEqual(resp.status, 404); + assert.strictEqual(resp.body, ''); + assert.strictEqual(resp.contentLength, 0); + }); +});