one
Version:
One is a new React Framework that makes Vite serve both native and web.
119 lines (118 loc) • 3.04 kB
JavaScript
import { afterEach, describe, expect, it, vi } from "vitest";
import { createWorkerHandler } from "./workerHandler.mjs";
const pageRoute = {
file: "app/some-page.tsx",
page: "/some-page",
namedRegex: "^/some-page(?:/)?$",
urlPath: "/some-page",
urlCleanPath: "/some-page",
routeKeys: {},
type: "ssr",
middlewares: []
};
const apiRoute = {
file: "app/api/status+api.ts",
page: "/api/status",
namedRegex: "^/api/status(?:/)?$",
urlPath: "/api/status",
urlCleanPath: "/api/status",
routeKeys: {},
type: "api",
middlewares: []
};
const buildInfo = {
constants: {
CACHE_KEY: "test"
},
routeToBuildInfo: {
[]: {
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: {}
};
const lazyRoutes = {
serverEntry: async () => ({
default: {
render: () => "<html></html>"
}
}),
pages: {
[]: async () => ({})
},
api: {
[]: async () => ({
HEAD: () => new Response(null, {
status: 204
})
})
},
middlewares: {}
};
function createHandler() {
vi.stubEnv("ONE_BUFFERED_SSR", "1");
return createWorkerHandler({
oneOptions: {
web: {
defaultRenderMode: "ssr"
}
},
buildInfo,
lazyRoutes
}).handleRequest;
}
afterEach(() => {
vi.unstubAllEnvs();
});
describe("createWorkerHandler", () => {
it("answers HEAD page requests exactly like GET", async () => {
const handleRequest = createHandler();
const get = await handleRequest(new Request("https://example.com/some-page"));
const head = await handleRequest(new Request("https://example.com/some-page", {
method: "HEAD"
}));
if (!(get instanceof Response) || !(head instanceof 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 () => {
const response = await createHandler()(new Request("https://example.com/some-page", {
method: "POST"
}));
expect(response).toBeNull();
});
it("continues routing HEAD requests to API handlers", async () => {
const response = await createHandler()(new Request("https://example.com/api/status", {
method: "HEAD"
}));
expect(response).toBeInstanceOf(Response);
expect(response?.status).toBe(204);
});
});
//# sourceMappingURL=workerHandler.test.mjs.map