UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

136 lines (135 loc) 3.5 kB
import { afterEach, describe, expect, it, vi } from "vitest"; import { createWorkerHandler } from "./workerHandler.native.js"; function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } } var pageRoute = { file: "app/some-page.tsx", page: "/some-page", namedRegex: "^/some-page(?:/)?$", urlPath: "/some-page", urlCleanPath: "/some-page", routeKeys: {}, type: "ssr", middlewares: [] }; var apiRoute = { file: "app/api/status+api.ts", page: "/api/status", namedRegex: "^/api/status(?:/)?$", urlPath: "/api/status", urlCleanPath: "/api/status", routeKeys: {}, type: "api", middlewares: [] }; var buildInfo = { constants: { CACHE_KEY: "test" }, routeToBuildInfo: { [pageRoute.file]: { type: "ssr", path: pageRoute.page, routeFile: pageRoute.file, middlewares: [], preloadPath: "", cssPreloadPath: "", loaderPath: "", cleanPath: pageRoute.page, htmlPath: "", clientJsPath: "", serverJsPath: "", params: {}, preloads: [], css: [], layoutCSS: [] } }, pathToRoute: {}, routeMap: {}, manifest: { pageRoutes: [pageRoute], apiRoutes: [apiRoute], allRoutes: [pageRoute, apiRoute] }, preloads: {}, cssPreloads: {}, loaders: {} }; var lazyRoutes = { serverEntry: async function () { return { default: { render: function () { return "<html></html>"; } } }; }, pages: { [pageRoute.file]: async function () { return {}; } }, api: { [apiRoute.page]: async function () { return { HEAD: function () { return new Response(null, { status: 204 }); } }; } }, middlewares: {} }; function createHandler() { vi.stubEnv("ONE_BUFFERED_SSR", "1"); return createWorkerHandler({ oneOptions: { web: { defaultRenderMode: "ssr" } }, buildInfo, lazyRoutes }).handleRequest; } afterEach(function () { vi.unstubAllEnvs(); }); describe("createWorkerHandler", function () { it("answers HEAD page requests exactly like GET", async function () { var handleRequest = createHandler(); var get = await handleRequest(new Request("https://example.com/some-page")); var head = await handleRequest(new Request("https://example.com/some-page", { method: "HEAD" })); if (!_instanceof(get, Response) || !_instanceof(head, Response)) { throw new Error("expected both GET and HEAD to return a Response"); } expect(get.status).toBe(200); expect(head.status).toBe(get.status); expect(head.headers.get("content-type")).toBe(get.headers.get("content-type")); }); it("does not route POST requests to page handlers", async function () { var response = await createHandler()(new Request("https://example.com/some-page", { method: "POST" })); expect(response).toBeNull(); }); it("continues routing HEAD requests to API handlers", async function () { var response = await createHandler()(new Request("https://example.com/api/status", { method: "HEAD" })); expect(response).toBeInstanceOf(Response); expect(response === null || response === void 0 ? void 0 : response.status).toBe(204); }); }); //# sourceMappingURL=workerHandler.test.native.js.map