Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions test/js/bun/s3/s3.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type { S3Options } from "bun";
import { S3Client, s3 as defaultS3, file, randomUUIDv7 } from "bun";
import { afterAll, describe, expect, it } from "bun:test";
import { afterAll, describe, expect, it, setDefaultTimeout } from "bun:test";
import { createHash, createHmac, randomUUID } from "crypto";
import { bunEnv, bunExe, getSecret, isCI, tempDir, tempDirWithFiles } from "harness";
import { bunEnv, bunExe, getSecret, isCI, isDebug, tempDir, tempDirWithFiles } from "harness";
import path from "path";
import { spawnServer } from "s3-server";
const s3 = (...args) => defaultS3.file(...args);
const S3 = (...args) => new S3Client(...args);

// The local server needs no container, so each test of this file runs under `bun bd test`. A debug build
// runs them many times slower than a release build: some tests that move no data took more than 5 seconds.
// CI does not run debug builds, and it gives each test its time with `--timeout`.
if (isDebug) setDefaultTimeout(60_000);
Comment thread
robobun marked this conversation as resolved.
Resolved

type S3Credentials = S3Options & {
service: string;
};
Expand Down Expand Up @@ -1485,6 +1490,60 @@ describe("s3-server", () => {
}
});

it.concurrent("signs for the region auto when the options and the environment have no region", async () => {
// Each other client of this file has a region. This one has the default of Bun for an endpoint that is not AWS.
// Windows reads `s3_region` as `S3_REGION`.
const absent = ["S3_REGION", "AWS_REGION", "S3_SESSION_TOKEN", "AWS_SESSION_TOKEN"];
const env = Object.fromEntries(Object.entries(bunEnv).filter(([name]) => !absent.includes(name.toUpperCase())));
const program = `
import { s3 } from "bun";
const file = s3.file("default-region.txt");
const outcome = error => error.code + ": " + error.message;
const presigned = await fetch(file.presign());
console.log(
JSON.stringify([
await file.text().catch(outcome),
await file.write("Hello Bun!").catch(outcome),
presigned.status,
presigned.headers.get("content-type"),
await presigned.text(),
]),
);
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", program],
env: {
...env,
"S3_ENDPOINT": localCredentials.endpoint as string,
"S3_BUCKET": localCredentials.bucket as string,
"S3_ACCESS_KEY_ID": localCredentials.accessKeyId as string,
"S3_SECRET_ACCESS_KEY": localCredentials.secretAccessKey as string,
},
// Bun reads the .env files of its directory. This directory has none.
cwd: testDir,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
// The server has the region us-east-1. Its error tells the region of the signature.
const refused =
"AuthorizationHeaderMalformed: The authorization header is malformed; the region 'auto' is wrong; expecting 'us-east-1'";
const message =
"Error parsing the X-Amz-Credential parameter; the region 'auto' is wrong; expecting 'us-east-1'";
const [read, write, status, type, document] = JSON.parse(stdout);
// Each response has its own IDs.
const ids = /<RequestId>[0-9A-F]{16}<\/RequestId><HostId>[A-Za-z0-9+\/]{56}<\/HostId>/;
expect([read, write, status, type, document.replace(ids, "<RequestId/><HostId/>")]).toEqual([
refused,
refused,
400,
"application/xml",
`<?xml version="1.0" encoding="UTF-8"?>\n<Error><Code>AuthorizationQueryParametersError</Code><Message>${message}</Message><Region>us-east-1</Region><RequestId/><HostId/></Error>`,
]);
Comment thread
robobun marked this conversation as resolved.
Resolved
expect(exitCode).toBe(0);
});

describe.concurrent("should accept / or \\ in start and end of bucket name", () => {
let bucketPrefixI = 0;
for (let start of ["/", "\\", ""]) {
Expand Down
3 changes: 3 additions & 0 deletions test/packages/s3-server/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ if (isDebug) setDefaultTimeout(60_000);
* A debug build of Bun adds `content-type: application/octet-stream` to a
* response of `Bun.serve` that has no body. A release build does not. This
* function removes that header, so that a test sees the response of a release build.
*
* https://github.com/oven-sh/bun/pull/30997 has the correction for `Bun.serve`.
* After it, this function and `TestClient` can go.
*/
export function withoutDefaultType(response: Response): Response {
const length = response.headers.get("content-length");
Expand Down
Loading