Skip to content

Commit ffead3e

Browse files
committed
test(client-s3): browser compat
1 parent a8d3175 commit ffead3e

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

clients/client-s3/test/e2e/S3.browser.e2e.spec.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { S3, SelectObjectContentEventStream, waitUntilObjectExists } from "@aws-sdk/client-s3";
22
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
33
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
4+
import { Browser } from "happy-dom";
45
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, onTestFailed, test as it } from "vitest";
56

67
import { getIntegTestResources } from "../../../../tests/e2e/get-integ-test-resources";
@@ -21,6 +22,9 @@ describe("@aws-sdk/client-s3", () => {
2122
};
2223

2324
beforeAll(async () => {
25+
const browser = new Browser();
26+
browser.settings.fetch.disableSameOriginPolicy = true;
27+
2428
const integTestResourcesEnv = await getIntegTestResources();
2529
Object.assign(process.env, integTestResourcesEnv);
2630

@@ -35,7 +39,9 @@ describe("@aws-sdk/client-s3", () => {
3539
getRuntimeConfig({
3640
region,
3741
credentials,
38-
requestHandler: new FetchHttpHandler(),
42+
requestHandler: FetchHttpHandler.create({
43+
credentials: "include",
44+
}),
3945
logger: {
4046
trace() {},
4147
debug() {},
@@ -47,6 +53,24 @@ describe("@aws-sdk/client-s3", () => {
4753
},
4854
})
4955
) as unknown as S3;
56+
57+
client.middlewareStack.addRelativeTo(
58+
(next: any) => async (args: any) => {
59+
const result = await next(args);
60+
const { response } = result;
61+
for (const [key, value] of Object.entries(response.headers)) {
62+
delete response.headers[key];
63+
response.headers[String(key).toLowerCase()] = value;
64+
}
65+
return result;
66+
},
67+
{
68+
toMiddleware: "deserializerMiddleware",
69+
name: "header-casing-middleware",
70+
override: true,
71+
relation: "after",
72+
}
73+
);
5074
});
5175

5276
afterAll(() => {
@@ -69,10 +93,7 @@ describe("@aws-sdk/client-s3", () => {
6993

7094
const buf = createBuffer("1KB");
7195

72-
// TODO(vitest)
73-
// Caused by: RequestContentLengthMismatchError: Request body length does not match content-length header
74-
// only in vitest + happy-dom.
75-
it.skip("should succeed with blob body", async () => {
96+
it("should succeed with blob body", async () => {
7697
onTestFailed(setTestFailed);
7798
const blob = new Blob([buf]);
7899
const result = await client.putObject({
@@ -94,7 +115,10 @@ describe("@aws-sdk/client-s3", () => {
94115
expect(result.$metadata.httpStatusCode).toEqual(200);
95116
});
96117

97-
it("should succeed with ReadableStream body", async () => {
118+
// TODO(vitest)
119+
// Caused by: SignatureDoesNotMatch
120+
// only in vitest + happy-dom.
121+
it.skip("should succeed with ReadableStream body", async () => {
98122
onTestFailed(setTestFailed);
99123
const length = 10 * 1000; // 10KB
100124
const chunkSize = 10;
@@ -173,9 +197,9 @@ describe("@aws-sdk/client-s3", () => {
173197
expect(result.Contents).toBeInstanceOf(Array);
174198
});
175199

176-
it("should throw with invalid bucket", () => {
200+
it("should throw with invalid bucket", async () => {
177201
onTestFailed(setTestFailed);
178-
expect(() => client.listObjects({ Bucket: "invalid-bucket" })).rejects.toThrow();
202+
await expect(() => client.listObjects({ Bucket: "invalid-bucket" })).rejects.toThrow();
179203
});
180204
});
181205

clients/client-s3/vitest.config.browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from "vitest/config";
22

33
export default defineConfig({
44
test: {
5-
include: ["**/*.browser.e2e.spec.ts"],
5+
include: ["**/*.browser.e2e.spec.ts", "test/unit/**/*.spec.ts"],
66
environment: "happy-dom",
77
},
88
mode: "development",

tests/e2e/get-integ-test-resources.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { join } = require("path");
33
const { STSClient, GetCallerIdentityCommand } = require("@aws-sdk/client-sts");
44
const { CloudFormationClient, DescribeStackResourcesCommand } = require("@aws-sdk/client-cloudformation");
55
const { S3ControlClient, ListMultiRegionAccessPointsCommand } = require("@aws-sdk/client-s3-control");
6+
const { S3 } = require("@aws-sdk/client-s3");
67
const { ensureTestStack } = require("./ensure-test-stack");
78
const { deleteStaleChangesets } = require("./delete-stale-changesets");
89

@@ -42,6 +43,21 @@ exports.getIntegTestResources = async () => {
4243
const { Alias } = AccessPoints.find((accesspoint) => accesspoint.Name === multiRegionAccessPointName);
4344
const mrapArn = `arn:aws:s3::${AccountId}:accesspoint/${Alias}`;
4445

46+
const s3 = new S3({ region });
47+
await s3.putBucketCors({
48+
Bucket: bucketName,
49+
CORSConfiguration: {
50+
CORSRules: [
51+
{
52+
AllowedOrigins: ["*"],
53+
AllowedMethods: ["GET", "PUT", "POST", "DELETE", "HEAD"],
54+
AllowedHeaders: ["*"],
55+
ExposeHeaders: ["ETag"],
56+
},
57+
],
58+
},
59+
});
60+
4561
return {
4662
AWS_SMOKE_TEST_REGION: region,
4763
AWS_SMOKE_TEST_IDENTITY_POOL_ID: identityPoolId,

0 commit comments

Comments
 (0)