|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | + |
| 3 | +test.describe("all supported methods should work in route handlers", () => { |
| 4 | + test("GET", async ({ request }) => { |
| 5 | + const getRes = await request.get("/methods"); |
| 6 | + const getData = await getRes.json(); |
| 7 | + expect(getRes.status()).toEqual(200); |
| 8 | + expect(getData.message).toEqual("OpenNext is awesome! :) :] :> :D"); |
| 9 | + }); |
| 10 | + |
| 11 | + test("POST", async ({ request }) => { |
| 12 | + const postRes = await request.post("/methods", { |
| 13 | + headers: { |
| 14 | + "Content-Type": "text/plain", |
| 15 | + }, |
| 16 | + data: "OpenNext is awesome! :] :) :> :D", |
| 17 | + }); |
| 18 | + expect(postRes.status()).toBe(202); |
| 19 | + const postData = await postRes.json(); |
| 20 | + expect(postData.message).toBe("ok"); |
| 21 | + const errorPostRes = await request.post("/methods", { |
| 22 | + headers: { |
| 23 | + "Content-Type": "text/plain", |
| 24 | + }, |
| 25 | + data: "OpenNext is not awesome! :C", |
| 26 | + }); |
| 27 | + expect(errorPostRes.status()).toBe(403); |
| 28 | + const errorData = await errorPostRes.json(); |
| 29 | + expect(errorData.message).toBe("forbidden"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("PUT", async ({ request }) => { |
| 33 | + const putRes = await request.put("/methods", { |
| 34 | + data: { |
| 35 | + message: "OpenNext PUT", |
| 36 | + }, |
| 37 | + }); |
| 38 | + expect(putRes.status()).toEqual(201); |
| 39 | + const putData = await putRes.json(); |
| 40 | + expect(putData.message).toEqual("ok"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("PATCH", async ({ request }) => { |
| 44 | + const timestampBefore = new Date(); |
| 45 | + const patchRes = await request.patch("/methods", { |
| 46 | + data: { message: "OpenNext PATCH" }, |
| 47 | + }); |
| 48 | + expect(patchRes.status()).toEqual(202); |
| 49 | + const patchData = await patchRes.json(); |
| 50 | + expect(patchData.message).toEqual("ok"); |
| 51 | + expect(patchData.modified).toEqual(true); |
| 52 | + expect(Date.parse(patchData.timestamp)).toBeGreaterThan(timestampBefore.getTime()); |
| 53 | + }); |
| 54 | + |
| 55 | + test("DELETE", async ({ request }) => { |
| 56 | + const deleteRes = await request.delete("/methods", { |
| 57 | + params: { |
| 58 | + command: "rm -rf / --no-preserve-root", |
| 59 | + }, |
| 60 | + }); |
| 61 | + expect(deleteRes.status()).toEqual(204); |
| 62 | + }); |
| 63 | + |
| 64 | + test("HEAD", async ({ request }) => { |
| 65 | + const headRes = await request.head("/methods"); |
| 66 | + expect(headRes.status()).toEqual(200); |
| 67 | + const headers = headRes.headers(); |
| 68 | + expect(headers["content-type"]).toEqual("text/html; charset=utf-8"); |
| 69 | + // expect(headers["content-length"]).toEqual("1234567"); |
| 70 | + expect(headers["special-header"]).toEqual("OpenNext is the best :) :] :> :D"); |
| 71 | + }); |
| 72 | + |
| 73 | + test("OPTIONS", async ({ request }) => { |
| 74 | + const optionsRes = await request.fetch("/methods", { |
| 75 | + method: "OPTIONS", |
| 76 | + }); |
| 77 | + expect(optionsRes.status()).toEqual(204); |
| 78 | + const headers = optionsRes.headers(); |
| 79 | + expect(headers.allow).toBe("GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, LOVE"); |
| 80 | + expect(headers.special).toBe("OpenNext is the best :) :] :> :D"); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +test("formData should work in POST route handler", async ({ request }) => { |
| 85 | + const formData = new FormData(); |
| 86 | + formData.append("name", "OpenNext [] () %&#!%$#"); |
| 87 | + formData.append("email", "[email protected]"); |
| 88 | + const postRes = await request.post("/methods/post/formdata", { |
| 89 | + form: formData, |
| 90 | + }); |
| 91 | + expect(postRes.status()).toBe(202); |
| 92 | + const postData = await postRes.json(); |
| 93 | + expect(postData.message).toBe("ok"); |
| 94 | +}); |
| 95 | + |
| 96 | +test("revalidate should work in GET route handler", async ({ request, page }) => { |
| 97 | + let time = Date.parse((await request.get("/methods/get/revalidate").then((res) => res.json())).time); |
| 98 | + let newTime: number; |
| 99 | + let tempTime = time; |
| 100 | + do { |
| 101 | + await page.waitForTimeout(1000); |
| 102 | + time = tempTime; |
| 103 | + const newTimeRes = await request.get("/methods/get/revalidate"); |
| 104 | + newTime = Date.parse((await newTimeRes.json()).time); |
| 105 | + tempTime = newTime; |
| 106 | + } while (time !== newTime); |
| 107 | + const midTime = Date.parse((await request.get("/methods/get/revalidate").then((res) => res.json())).time); |
| 108 | + |
| 109 | + await page.waitForTimeout(1000); |
| 110 | + // Expect that the time is still stale |
| 111 | + expect(midTime).toEqual(newTime); |
| 112 | + |
| 113 | + // Wait 5 + 1 seconds for ISR to regenerate time |
| 114 | + await page.waitForTimeout(6000); |
| 115 | + let finalTime = newTime; |
| 116 | + do { |
| 117 | + await page.waitForTimeout(2000); |
| 118 | + finalTime = Date.parse((await request.get("/methods/get/revalidate").then((res) => res.json())).time); |
| 119 | + } while (newTime === finalTime); |
| 120 | + |
| 121 | + expect(newTime).not.toEqual(finalTime); |
| 122 | +}); |
| 123 | + |
| 124 | +test("should cache a static GET route", async ({ request }) => { |
| 125 | + const res = await request.get("/methods/get/static"); |
| 126 | + expect(res.headers()["cache-control"]).toContain("s-maxage=31536000"); |
| 127 | +}); |
| 128 | + |
| 129 | +test("should be able to set cookies in route handler", async ({ request }) => { |
| 130 | + const postRes = await request.post("/methods/post/cookies", { |
| 131 | + form: { |
| 132 | + username: "hakuna", |
| 133 | + password: "matata", |
| 134 | + }, |
| 135 | + }); |
| 136 | + expect(postRes.status()).toBe(202); |
| 137 | + const postData = await postRes.json(); |
| 138 | + expect(postData.message).toBe("ok"); |
| 139 | + const cookies = postRes.headers()["set-cookie"]; |
| 140 | + expect(cookies).toContain("auth_session=SUPER_SECRET_SESSION_ID_1234"); |
| 141 | +}); |
| 142 | + |
| 143 | +test("should be able to redirect in route handler", async ({ request }) => { |
| 144 | + const redirectRes = await request.get("/methods/get/redirect", { |
| 145 | + // Disable auto-redirect to check initial response |
| 146 | + maxRedirects: 0, |
| 147 | + }); |
| 148 | + expect(redirectRes.status()).toBe(307); |
| 149 | + expect(redirectRes.headers().location).toBe("https://nextjs.org/"); |
| 150 | + |
| 151 | + // Check if the redirect works |
| 152 | + const followedRes = await request.get("/methods/get/redirect"); |
| 153 | + expect(followedRes.url()).toBe("https://nextjs.org/"); |
| 154 | +}); |
| 155 | + |
| 156 | +test("dynamic segments should work in route handlers", async ({ request }) => { |
| 157 | + const res = await request.get("/methods/get/dynamic-segments/this-is-a-slug"); |
| 158 | + const data = await res.json(); |
| 159 | + expect(data.slug).toBe("this-is-a-slug"); |
| 160 | +}); |
| 161 | + |
| 162 | +test("query parameters should work in route handlers", async ({ request }) => { |
| 163 | + const res = await request.get("/methods/get/query", { |
| 164 | + params: { |
| 165 | + query: "OpenNext is awesome!", |
| 166 | + }, |
| 167 | + }); |
| 168 | + const data = await res.json(); |
| 169 | + expect(data.query).toBe("OpenNext is awesome!"); |
| 170 | +}); |
0 commit comments