-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor tests for handleNextRequest
- Loading branch information
Showing
11 changed files
with
274 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,111 @@ | ||
import express from "express"; | ||
import { test } from "tap"; | ||
import { handleNextRequest } from "../src/app-route.js"; | ||
|
||
test("simple", async (t) => { | ||
const request = new Request("http://example/foo/bar"); | ||
const app = express(); | ||
app.get("/foo/:hello", (req, res) => { | ||
console.log("handle inner"); | ||
res.json({ message: req.params.hello }); | ||
import { spawn } from "node:child_process"; | ||
|
||
test("real server", async (t) => { | ||
t.before(async () => { | ||
await new Promise((resolve) => { | ||
const child = spawn("rm", ["-rf", "test/next/.next"], {}); | ||
child.on("exit", resolve); | ||
}); | ||
}); | ||
|
||
const nextApp = await next({ dir: "test/next" }); | ||
t.teardown(nextApp.close); | ||
console.log("next url", nextApp.address); | ||
|
||
const query = await fetch(nextApp.address + "/query?message=bar").then((r) => | ||
r.json() | ||
); | ||
t.same(query, { message: "bar" }); | ||
|
||
const headers = await fetch(nextApp.address + "/headers", { | ||
headers: { | ||
example: "bar", | ||
Uppercase: "BAR", | ||
"X-Custom": "custom", | ||
}, | ||
}).then((r) => r.json()); | ||
t.has(headers, { | ||
example: "bar", | ||
uppercase: "BAR", | ||
"x-custom": "custom", | ||
}); | ||
|
||
try { | ||
console.log("handle outer"); | ||
const response = await handleNextRequest(request, app); | ||
t.same(response.body, { message: "bar" }, "returns the body"); | ||
} catch (error) { | ||
console.log("error", error.message); | ||
} | ||
const body = await fetch(nextApp.address + "/body", { | ||
headers: { "content-type": "application/json" }, | ||
method: "POST", | ||
body: JSON.stringify({ message: "bar" }), | ||
}).then((r) => r.json()); | ||
t.same(body, { message: "bar" }); | ||
|
||
const bodyUrlEncoded = await fetch(nextApp.address + "/body", { | ||
method: "POST", | ||
// url encoded | ||
headers: { "content-type": "application/x-www-form-urlencoded" }, | ||
body: "message=bar", | ||
}).then((r) => r.json()); | ||
t.same(bodyUrlEncoded, { message: "bar" }); | ||
|
||
// TODO | ||
const sendFile = await fetch(nextApp.address + "/end", {}).then((r) => | ||
r.text() | ||
); | ||
t.same(sendFile, "barfile"); | ||
}); | ||
|
||
// Cannot run next() from 'next' because of ts transpilation | ||
async function next({ dir }: { dir: string }) { | ||
return new Promise<{ close: () => void; address: string }>( | ||
(resolve, reject) => { | ||
// Start server in child process | ||
let started = false; | ||
|
||
// TODO: random port? | ||
console.log("starting next", dir); | ||
const child = spawn("npx", ["next"], { | ||
cwd: process.cwd() + "/" + dir, | ||
env: { | ||
PATH: process.env.PATH, | ||
PORT: Math.floor(Math.random() * 50000 + 10000).toString(), | ||
}, | ||
detached: false, | ||
}); | ||
|
||
child.stdout.setEncoding("utf8"); | ||
child.stdout.on("data", async function (data) { | ||
// console.log("|", data); | ||
if (!started && data.includes("started server on")) { | ||
started = true; | ||
resolve({ | ||
address: data.split("url:")[1].trim(), | ||
close: kill, | ||
}); | ||
} | ||
}); | ||
child.stderr.setEncoding("utf8"); | ||
child.stderr.on("data", function (data) { | ||
console.log("stderr: ", data); | ||
}); | ||
|
||
function kill() { | ||
return new Promise<void>((resolve) => { | ||
if (child.exitCode === null) { | ||
child.on("exit", resolve); | ||
child.on("close", (d) => console.log("close", d)); | ||
child.on("error", (d) => console.log("error", d)); | ||
} else { | ||
return resolve(); | ||
} | ||
|
||
child.kill(); | ||
setTimeout(() => { | ||
if (child.exitCode === null) { | ||
console.log("SIGKILL"); | ||
child.kill("SIGKILL"); | ||
} | ||
}, 2000); | ||
}); | ||
} | ||
} | ||
); | ||
} |
Oops, something went wrong.