one
Version:
One is a new React Framework that makes Vite serve both native and web.
161 lines (160 loc) • 6 kB
JavaScript
import { describe, expect, it, vi, beforeEach } from "vitest";
import { createHandleRequest } from "./createHandleRequest.native.js";
vi.mock("./vite/getManifest", function () {
return {
getManifest: function () {
return {
pageRoutes: [{
file: "app/[slug].tsx",
page: "/[slug]",
namedRegex: "^/(?<nxtPslug>[^/]+?)(?:/)?$",
routeKeys: {
nxtPslug: "slug"
},
type: "ssr",
middlewares: []
}, {
file: "app/index.tsx",
page: "/",
namedRegex: "^/(?:/)?$",
routeKeys: {},
type: "ssr",
middlewares: []
}],
apiRoutes: []
};
}
};
});
function createRequest(path) {
return new Request(`http://localhost:3000${path}`, {
headers: {
host: "localhost:3000"
}
});
}
describe("createHandleRequest", function () {
var mockHandlers = {
handlePage: vi.fn().mockResolvedValue("<html></html>")
};
beforeEach(function () {
vi.clearAllMocks();
}), describe("static file extension filtering", function () {
it("should skip paths with file extensions (favicon.ico)", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/favicon.ico"));
expect(result).toBeNull(), expect(mockHandlers.handlePage).not.toHaveBeenCalled();
}), it("should skip paths with file extensions (logo.png)", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/logo.png"));
expect(result).toBeNull(), expect(mockHandlers.handlePage).not.toHaveBeenCalled();
}), it("should skip paths with file extensions (styles.css)", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/styles.css"));
expect(result).toBeNull(), expect(mockHandlers.handlePage).not.toHaveBeenCalled();
}), it("should skip paths with file extensions (robots.txt)", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/robots.txt"));
expect(result).toBeNull(), expect(mockHandlers.handlePage).not.toHaveBeenCalled();
}), it("should skip nested paths with file extensions", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/assets/images/logo.png"));
expect(result).toBeNull(), expect(mockHandlers.handlePage).not.toHaveBeenCalled();
}), it("should skip extensions 2-4 chars like .xyz", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/somefile.xyz"));
expect(result).toBeNull(), expect(mockHandlers.handlePage).not.toHaveBeenCalled();
}), it("should NOT skip extensions longer than 4 chars (routes with dots in names)", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
});
await handler(createRequest("/route.normal")), expect(mockHandlers.handlePage).toHaveBeenCalled();
}), it("should NOT skip 5+ char extensions like .woff2", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
});
await handler(createRequest("/font.woff2")), expect(mockHandlers.handlePage).toHaveBeenCalled();
}), it("should NOT skip loader paths ending with _vxrn_loader.js", async function () {
var mockHandlersWithLoader = {
handlePage: vi.fn().mockResolvedValue("<html></html>"),
handleLoader: vi.fn().mockResolvedValue("loader data")
},
{
handler
} = createHandleRequest(mockHandlersWithLoader, {
routerRoot: "/app"
});
await handler(createRequest("/assets/my-page_123_vxrn_loader.js")), expect(mockHandlersWithLoader.handleLoader).toHaveBeenCalled();
});
}), describe("dynamic route matching", function () {
it("should match dynamic routes for regular paths without extensions", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
});
await handler(createRequest("/my-page")), expect(mockHandlers.handlePage).toHaveBeenCalled();
}), it("should match index route", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
});
await handler(createRequest("/")), expect(mockHandlers.handlePage).toHaveBeenCalled();
}), it("should match paths with hyphens", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
});
await handler(createRequest("/my-awesome-page")), expect(mockHandlers.handlePage).toHaveBeenCalled();
});
}), describe("special paths", function () {
it("should skip __vxrnhmr path", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/__vxrnhmr"));
expect(result).toBeNull();
}), it("should skip paths starting with /@", async function () {
var {
handler
} = createHandleRequest(mockHandlers, {
routerRoot: "/app"
}),
result = await handler(createRequest("/@fs/some/path"));
expect(result).toBeNull();
});
});
});
//# sourceMappingURL=createHandleRequest.test.native.js.map