Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 36 additions & 1 deletion examples/e2e/pages-router/e2e/api.test.ts
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,40 @@ test("should not fail on an api route", async ({ page }) => {
const result = await page.goto("/api/hello");
expect(result?.status()).toBe(200);
const body = await result?.json();
expect(body).toEqual({ hello: "world" });
expect(body).toEqual({ hello: "OpenNext rocks!" });
});

test("should work with dynamic api route", async ({ page }) => {
const result = await page.goto("/api/dynamic/opennext");
expect(result?.status()).toBe(200);
const body = await result?.json();
expect(body).toEqual({ slug: "opennext" });
});

test("should work with catch all api route", async ({ page }) => {
const result = await page.goto("/api/dynamic/catch-all/first/second/third");
expect(result?.status()).toBe(200);
const body = await result?.json();
expect(body).toEqual({ slug: ["first", "second", "third"] });
});

test("dynamic route should take precedence over catch all", async ({ page }) => {
const result = await page.goto("/api/dynamic/catch-all");
expect(result?.status()).toBe(200);
const body = await result?.json();
expect(body).toEqual({ slug: "catch-all" });
});

test("should work with optional catch all api route", async ({ page }) => {
const result = await page.goto("/api/dynamic/catch-all-optional");
expect(result?.status()).toBe(200);
const body = await result?.json();
expect(body).toEqual({ optional: "true" });
});

test("predefined api route should take presedence", async ({ page }) => {
const result = await page.goto("/api/dynamic/precedence");
expect(result?.status()).toBe(200);
const body = await result?.json();
expect(body).toEqual({ precedence: "true" });
});
6 changes: 6 additions & 0 deletions examples/e2e/pages-router/src/pages/api/dynamic/[slug].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { slug } = req.query;
res.status(200).json({ slug });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ optional: "true" });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { slug } = req.query;
if (!Array.isArray(slug)) {
return res.status(500).json({ error: "Invalid" });
}
res.status(200).json({ slug });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ precedence: "true" });
}
4 changes: 2 additions & 2 deletions examples/e2e/pages-router/src/pages/api/hello.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
// Next.js API route support: https://nextjs.org/docs/pages/building-your-application/routing/api-routes
import type { NextApiRequest, NextApiResponse } from "next";

type Data = {
hello: string;
};

export default function handler(req: NextApiRequest, res: NextApiResponse<Data>) {
res.status(200).json({ hello: "world" });
res.status(200).json({ hello: "OpenNext rocks!" });
}