-
-
Notifications
You must be signed in to change notification settings - Fork 14
chore: add additional tests #116
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { defineEventHandler } from "nitro/h3"; | ||
|
|
||
| export default defineEventHandler((event) => { | ||
| return event.context.basicAuth?.username ?? "no-auth"; | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,16 @@ const baseURL = "/base/"; | |||||
|
|
||||||
| const withBase = (p: string) => baseURL + p.replace(/^\//, ""); | ||||||
|
|
||||||
| const tests = ["api", "form-data", "multipart-form-data", "sourcemap"]; | ||||||
| const tests = [ | ||||||
| "api", | ||||||
| "basic-auth", | ||||||
| "form-data", | ||||||
| "headers", | ||||||
| "meta", | ||||||
| "multipart-form-data", | ||||||
| "redirect", | ||||||
| "sourcemap", | ||||||
| ]; | ||||||
|
|
||||||
| const manualTests = ["env", "node-compat", "headers"]; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate test name in
Proposed fix-const manualTests = ["env", "node-compat", "headers"];
+const manualTests = ["env", "node-compat"];📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { defineEventHandler } from "nitro/h3"; | ||
|
|
||
| export default defineEventHandler(() => "REDIRECTED"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { defineTestHandler } from "../../utils/test"; | ||
|
|
||
| // Route rule in nitro.config.ts protects /base/basic-auth-protected with basic auth. | ||
| // The handler echoes `event.context.basicAuth.username`, which is only populated | ||
| // when the basicAuth route rule runs successfully. We avoid triggering a 401 | ||
| // response here because browsers show a native auth popup on 401 + WWW-Authenticate. | ||
| export default defineTestHandler( | ||
| "basic-auth", | ||
| () => "test-page", | ||
| async ({ assert }) => { | ||
| const res = await fetch("/base/basic-auth-protected", { | ||
| headers: { Authorization: "Basic " + btoa("admin:nitrorunseverywhere") }, | ||
| }); | ||
| assert(res.status === 200, `Expected 200 with valid credentials, got: ${res.status}`); | ||
| const text = await res.text(); | ||
| assert(text === "admin", `Expected body "admin" (echoed from basicAuth context), got: ${text}`); | ||
| }, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { defineTestHandler } from "../../utils/test"; | ||
|
|
||
| // Route rule in nitro.config.ts sets multiple response headers on this path | ||
| // to exercise name/value edge cases that should work across all providers. | ||
| export default defineTestHandler( | ||
| "headers", | ||
| () => "OK", | ||
| async ({ assert, log }) => { | ||
| const res = await fetch(""); | ||
|
|
||
| const cases: Array<[string, string]> = [ | ||
| // Baseline ASCII | ||
| ["x-nitro-test", "hello"], | ||
| // Dots, dashes, digits in the name | ||
| ["x-nitro.test-2", "dotted"], | ||
| // Internal spaces in value (legal per RFC 9110) | ||
| ["x-nitro-spaced", "hello world from nitro"], | ||
| // Long value (1 KB) | ||
| ["x-nitro-long", "a".repeat(1024)], | ||
| // Special-but-legal tchars in value | ||
| ["x-nitro-special", "a!#$%&'*+-.^_`|~b"], | ||
| // Numeric-like value | ||
| ["x-nitro-num", "42"], | ||
| ]; | ||
|
|
||
| for (const [name, expected] of cases) { | ||
| const actual = res.headers.get(name); | ||
| log(`${name}: ${actual}`); | ||
| assert(actual === expected, `Expected ${name}=${expected}, got: ${actual}`); | ||
| } | ||
|
|
||
| // Case-insensitive lookup must work regardless of how the runtime preserves case. | ||
| assert( | ||
| res.headers.get("X-NITRO-TEST") === "hello", | ||
| "Expected case-insensitive header lookup to work", | ||
| ); | ||
| }, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { version } from "nitro/meta"; | ||
| import { defineTestHandler } from "../../utils/test"; | ||
|
|
||
| export default defineTestHandler( | ||
| "meta", | ||
| () => ({ version }), | ||
| async ({ assert, log }) => { | ||
| const res = await fetch("").then((r) => r.json()); | ||
| log(`Nitro version: ${res.version}`); | ||
| assert( | ||
| typeof res.version === "string" && res.version.length > 0, | ||
| `Expected non-empty version string, got: ${res.version}`, | ||
| ); | ||
| assert(/^\d+\.\d+\.\d+/.test(res.version), `Expected semver-like version, got: ${res.version}`); | ||
| }, | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { defineTestHandler } from "../../utils/test"; | ||
|
|
||
| // Route rule in nitro.config.ts redirects /base/redirect-source to /base/redirect-target. | ||
| export default defineTestHandler( | ||
| "redirect", | ||
| () => "test-page", | ||
| async ({ assert }) => { | ||
| const res = await fetch("/base/redirect-source"); | ||
| const text = await res.text(); | ||
| assert(res.redirected === true, `Expected redirected=true, got: ${res.redirected}`); | ||
| assert(text === "REDIRECTED", `Expected body "REDIRECTED", got: ${text}`); | ||
| }, | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid committing plaintext credentials, even for demo auth.
password: "nitrorunseverywhere"is committed to the repo. For a public test-deployment project this is likely intentional (the credential must be known to exercise the test), but it's worth noting: once public it can never be reused for anything sensitive, and it trains a pattern of inline secrets. Consider sourcing it from an env var with a documented default, e.g.password: process.env.BASIC_AUTH_PASSWORD ?? "nitrorunseverywhere", so real deployments can override it without code changes.🤖 Prompt for AI Agents