Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node:http: allow setting response.statusCode and statusMessage [v2] #11082

Merged
merged 15 commits into from
May 15, 2024
Merged
7 changes: 7 additions & 0 deletions src/js/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,12 +818,19 @@ Object.defineProperty(IncomingMessage.prototype, "statusCode", {
get() {
return this[reqSymbol].status;
},
set(v) {
if (!(v in STATUS_CODES)) return;
this[reqSymbol].status = v;
},
});

Object.defineProperty(IncomingMessage.prototype, "statusMessage", {
get() {
return STATUS_CODES[this[reqSymbol].status];
},
set(v) {
//noop
},
});

Object.defineProperty(IncomingMessage.prototype, "httpVersion", {
Expand Down
Binary file modified test/bun.lockb
Binary file not shown.
20 changes: 4 additions & 16 deletions test/cli/install/bun-link.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawn, file } from "bun";
import { afterAll, afterEach, beforeAll, beforeEach, expect, it } from "bun:test";
import { bunExe, bunEnv as env, tmpdirSync, toBeValidBin, toHaveBins } from "harness";
import { bunExe, bunEnv as env, runBunInstall, toBeValidBin, toHaveBins, tmpdirSync } from "harness";
import { access, writeFile, mkdir } from "fs/promises";
import { basename, join } from "path";
import {
Expand Down Expand Up @@ -55,36 +55,24 @@ it("should link and unlink workspace package", async () => {
version: "0.0.1",
}),
);
var { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "install"],
cwd: link_dir,
stdout: "pipe",
stdin: "pipe",
stderr: "pipe",
env,
});
expect(stderr).toBeDefined();
var err = await new Response(stderr).text();
let { out, err } = await runBunInstall(env, link_dir);
expect(err.replace(/^(.*?) v[^\n]+/, "$1").split(/\r?\n/)).toEqual(["bun install", " Saved lockfile", ""]);
expect(stdout).toBeDefined();
var out = await new Response(stdout).text();
expect(out.replace(/\s*\[[0-9\.]+ms\]\s*$/, "").split(/\r?\n/)).toEqual([
"",
` + boba@workspace:packages/boba`,
` + moo@workspace:packages/moo`,
"",
" 2 packages installed",
]);
expect(await exited).toBe(0);

({ stdout, stderr, exited } = spawn({
let { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "link"],
cwd: join(link_dir, "packages", "moo"),
stdout: "pipe",
stdin: "pipe",
stderr: "pipe",
env,
}));
});

expect(stderr).toBeDefined();
err = await new Response(stderr).text();
Expand Down
47 changes: 6 additions & 41 deletions test/cli/install/bun-workspaces.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawnSync } from "bun";
import { bunExe, bunEnv as env, tmpdirSync, toMatchNodeModulesAt } from "harness";
import { bunExe, bunEnv as env, runBunInstall, tmpdirSync, toMatchNodeModulesAt } from "harness";
import { join } from "path";
import { writeFileSync, mkdirSync, rmSync } from "fs";
import { beforeEach, test, expect } from "bun:test";
Expand Down Expand Up @@ -27,7 +27,7 @@ cache = false
);
});

test("dependency on workspace without version in package.json", () => {
test("dependency on workspace without version in package.json", async () => {
writeFileSync(
join(packageDir, "package.json"),
JSON.stringify({
Expand Down Expand Up @@ -77,29 +77,17 @@ test("dependency on workspace without version in package.json", () => {
}),
);

const { stdout, exitCode } = spawnSync({
cmd: [bunExe(), "install"],
cwd: packageDir,
stderr: "inherit",
stdout: "pipe",
env,
});

const { out } = await runBunInstall(env, packageDir);
const lockfile = parseLockfile(packageDir);
expect(lockfile).toMatchNodeModulesAt(packageDir);
expect(lockfile).toMatchSnapshot(`version: ${version}`);

const out = stdout.toString();
expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
"",
" + bar@workspace:packages/bar",
" + lodash@workspace:packages/mono",
"",
" 2 packages installed",
]);

expect(exitCode).toBe(0);

rmSync(join(packageDir, "node_modules"), { recursive: true, force: true });
rmSync(join(packageDir, "bun.lockb"), { recursive: true, force: true });
}
Expand All @@ -118,36 +106,24 @@ test("dependency on workspace without version in package.json", () => {
}),
);

const { exitCode, stdout } = spawnSync({
cmd: [bunExe(), "install"],
cwd: packageDir,
stderr: "inherit",
stdout: "pipe",
env,
});

const { out } = await runBunInstall(env, packageDir);
const lockfile = parseLockfile(packageDir);
expect(lockfile).toMatchNodeModulesAt(packageDir);
expect(lockfile).toMatchSnapshot(`version: ${version}`);

const out = stdout.toString();
expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
"",
" + bar@workspace:packages/bar",
" + lodash@workspace:packages/mono",
"",
" 3 packages installed",
]);

expect(exitCode).toBe(0);

rmSync(join(packageDir, "node_modules"), { recursive: true, force: true });
rmSync(join(packageDir, "packages", "bar", "node_modules"), { recursive: true, force: true });
rmSync(join(packageDir, "bun.lockb"), { recursive: true, force: true });
}
}, 20_000);

test("dependency on same name as workspace and dist-tag", () => {
test("dependency on same name as workspace and dist-tag", async () => {
writeFileSync(
join(packageDir, "package.json"),
JSON.stringify({
Expand Down Expand Up @@ -177,26 +153,15 @@ test("dependency on same name as workspace and dist-tag", () => {
}),
);

const { stdout, exitCode } = spawnSync({
cmd: [bunExe(), "install"],
cwd: packageDir,
stderr: "inherit",
stdout: "pipe",
env,
});

const { out } = await runBunInstall(env, packageDir);
const lockfile = parseLockfile(packageDir);
expect(lockfile).toMatchSnapshot("with version");
expect(lockfile).toMatchNodeModulesAt(packageDir);

const out = stdout.toString();
expect(out.replace(/\s*\[[0-9\.]+m?s\]\s*$/, "").split(/\r?\n/)).toEqual([
"",
" + bar@workspace:packages/bar",
" + lodash@workspace:packages/mono",
"",
" 3 packages installed",
]);

expect(exitCode).toBe(0);
});
21 changes: 21 additions & 0 deletions test/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,24 @@ export function mergeWindowEnvs(envs: Record<string, string | undefined>[]) {
export function tmpdirSync(pattern: string = "bun.test.") {
return fs.mkdtempSync(join(fs.realpathSync(os.tmpdir()), pattern));
}

export async function runBunInstall(env: NodeJS.ProcessEnv, cwd: string) {
const { stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "install"],
cwd,
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env,
});
expect(stdout).toBeDefined();
expect(stderr).toBeDefined();
let err = await new Response(stderr).text();
expect(err).not.toContain("panic:");
expect(err).not.toContain("error:");
expect(err).not.toContain("warn:");
expect(err).toContain("Saved lockfile");
let out = await new Response(stdout).text();
expect(await exited).toBe(0);
return { out, err, exited };
}
2 changes: 1 addition & 1 deletion test/js/bun/util/filesystem_router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function createTree(basedir: string, paths: string[]) {
}
var count = 0;
function make(files: string[]) {
const dir = tmpdirSync();
const dir = tmpdirSync().replaceAll("\\", "/");
rmSync(dir, {
recursive: true,
force: true,
Expand Down
26 changes: 26 additions & 0 deletions test/js/third_party/_fixtures/msw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import axios from "axios";
import { http, passthrough, HttpResponse } from "msw";
import { setupServer } from "msw/node";

const server = setupServer(
...[
http.get("http://localhost/", () => {
// return passthrough()
return HttpResponse.json({ results: [{}, {}] });
}),
],
);
server.listen({
onUnhandledRequest: "warn",
});

axios
.get("http://localhost/?page=2")
.then(function (response) {
// handle success
console.log(response.data.results.length);
})
.catch(function (error) {
// handle error
console.log(error?.message);
});
18 changes: 18 additions & 0 deletions test/js/third_party/_fixtures/st.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createServer } from "node:http";
import st from "st";

function listen(server): Promise<URL> {
return new Promise((resolve, reject) => {
server.listen({ port: 0 }, (err, hostname, port) => {
if (err) {
reject(err);
} else {
resolve(new URL("http://" + hostname + ":" + port));
}
});
});
}
await using server = createServer(st(process.cwd()));
const url = await listen(server);
const res = await fetch(new URL("/st.ts", url));
console.log(await res.text());
19 changes: 19 additions & 0 deletions test/js/third_party/msw.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { bunExe } from "bun:harness";
import { bunEnv, runBunInstall, tmpdirSync } from "harness";
import { expect, it } from "bun:test";
import * as path from "node:path";

it("works", async () => {
let { stdout, stderr, exited } = Bun.spawn({
nektro marked this conversation as resolved.
Show resolved Hide resolved
cmd: [bunExe(), "run", path.join(import.meta.dirname, "_fixtures", "msw.ts")],
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env: bunEnv,
});
let err = await new Response(stderr).text();
expect(err).toBeEmpty();
let out = await new Response(stdout).text();
expect(out).toEqual("2\n");
expect(await exited).toBe(0);
});
50 changes: 6 additions & 44 deletions test/js/third_party/st.test.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,22 @@
import { bunExe } from "bun:harness";
import { bunEnv, tmpdirSync } from "harness";
import { bunEnv, runBunInstall, tmpdirSync } from "harness";
import { expect, it } from "bun:test";
import * as path from "node:path";

it("works", async () => {
const package_dir = tmpdirSync();

const fixture_path = path.join(import.meta.dirname, "_fixtures", "st.ts");
const fixture_data = await Bun.file(fixture_path).text();
let { stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "add", "st@3.0.0"],
cwd: package_dir,
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env: bunEnv,
});
let err = await new Response(stderr).text();
expect(err).not.toContain("panic:");
expect(err).not.toContain("error:");
expect(err).not.toContain("warn:");
let out = await new Response(stdout).text();
expect(await exited).toBe(0);

const fixture_path = path.join(package_dir, "index.ts");
const fixture_data = `
import { createServer } from "node:http";
import st from "st";

function listen(server): Promise<URL> {
return new Promise((resolve, reject) => {
server.listen({ port: 0 }, (err, hostname, port) => {
if (err) {
reject(err);
} else {
resolve(new URL("http://"+hostname+":"+port));
}
});
});
}
await using server = createServer(st(process.cwd()));
const url = await listen(server);
const res = await fetch(new URL("/index.ts", url));
console.log(await res.text());
`;
await Bun.write(fixture_path, fixture_data);

({ stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "run", fixture_path],
cwd: package_dir,
cwd: path.dirname(fixture_path),
stdout: "pipe",
stdin: "ignore",
stderr: "pipe",
env: bunEnv,
}));
});
// err = await new Response(stderr).text();
// expect(err).toBeEmpty();
out = await new Response(stdout).text();
let out = await new Response(stdout).text();
expect(out).toEqual(fixture_data + "\n");
expect(await exited).toBe(0);
});
Loading
Loading