-
Notifications
You must be signed in to change notification settings - Fork 0
Task-explorer: support relative base path + bumped version #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export { injectBasePath }; | ||
|
|
||
| function injectBasePath(html: string, basePath: string): string { | ||
| const baseHref = basePath ? `${basePath}/` : "/"; | ||
| return html.replace( | ||
| "<head>", | ||
| `<head><base href="${baseHref}"><script>window.__BASE_PATH__=${JSON.stringify(basePath)};</script>`, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { group, test, expect } from "@ambarltd/core/test"; | ||
| import { injectBasePath } from "@be/lib/html-injection"; | ||
|
|
||
| const BARE_HTML = `<!doctype html><html><head><title>T</title></head><body><div id="app"></div></body></html>`; | ||
|
|
||
| export const tests = group("injectBasePath", [ | ||
| test("injects base href and window.__BASE_PATH__ when BASE_PATH is set", () => { | ||
| const result = injectBasePath(BARE_HTML, "/tasks"); | ||
| expect.contains(`<base href="/tasks/">`, result); | ||
| expect.contains(`window.__BASE_PATH__="/tasks"`, result); | ||
| }), | ||
|
|
||
| test("injects base href of / and empty string when BASE_PATH is empty", () => { | ||
| const result = injectBasePath(BARE_HTML, ""); | ||
| expect.contains(`<base href="/">`, result); | ||
| expect.contains(`window.__BASE_PATH__=""`, result); | ||
| }), | ||
|
|
||
| test("injection is placed inside <head>, not outside", () => { | ||
| const result = injectBasePath(BARE_HTML, "/tasks"); | ||
| const headOpen = result.indexOf("<head>"); | ||
| const baseTag = result.indexOf("<base href"); | ||
| const headClose = result.indexOf("</head>"); | ||
| expect.equals(baseTag > headOpen, true); | ||
| expect.equals(baseTag < headClose, true); | ||
| }), | ||
|
|
||
| test("appends trailing slash to base href for nested base paths", () => { | ||
| const result = injectBasePath(BARE_HTML, "/app/tasks"); | ||
| expect.contains(`<base href="/app/tasks/">`, result); | ||
| expect.contains(`window.__BASE_PATH__="/app/tasks"`, result); | ||
| }), | ||
|
|
||
| test("leaves rest of HTML untouched", () => { | ||
| const result = injectBasePath(BARE_HTML, "/tasks"); | ||
| expect.contains(`<title>T</title>`, result); | ||
| expect.contains(`<div id="app"></div>`, result); | ||
| }), | ||
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { group, test, expect } from "@ambarltd/core/test"; | ||
| import request from "supertest"; | ||
| import express, { type Request, type Response } from "express"; | ||
| import { injectBasePath } from "@be/lib/html-injection"; | ||
|
|
||
| const STUB_HTML = `<!doctype html><html><head><title>T</title></head><body><div id="app"></div></body></html>`; | ||
|
|
||
| function buildApp(basePath: string) { | ||
| const app = express(); | ||
| const injected = injectBasePath(STUB_HTML, basePath); | ||
|
|
||
| function sendIndex(_req: Request, res: Response): void { | ||
| res.setHeader("Cache-Control", "no-cache").type("html").send(injected); | ||
| } | ||
|
|
||
| if (basePath) { | ||
| app.get("/", (_req: Request, res: Response) => res.redirect(301, basePath)); | ||
| } | ||
|
|
||
| const staticMountPath = basePath || "/"; | ||
| const indexPath = staticMountPath.replace(/\/$/, "") || "/"; | ||
| app.get(indexPath, sendIndex); | ||
| app.get(basePath + "/*", sendIndex); | ||
|
|
||
| return app; | ||
| } | ||
|
|
||
| const appWithBasePath = buildApp("/tasks"); | ||
| const appAtAnotherPath = buildApp("/another-path"); | ||
| const appAtRoot = buildApp(""); | ||
|
|
||
| export const tests = group("Routing", [ | ||
| group("with BASE_PATH=/tasks", [ | ||
| test("GET / redirects 301 to /tasks", async () => { | ||
| const res = await request(appWithBasePath).get("/"); | ||
| expect.equals(res.status, 301); | ||
| expect.equals(res.headers["location"], "/tasks"); | ||
| }), | ||
|
|
||
| test("GET /tasks serves HTML with injected base href", async () => { | ||
| const res = await request(appWithBasePath).get("/tasks"); | ||
| expect.equals(res.status, 200); | ||
| expect.equals(res.type, "text/html"); | ||
| expect.contains(`<base href="/tasks/">`, res.text); | ||
| expect.contains(`window.__BASE_PATH__="/tasks"`, res.text); | ||
| }), | ||
|
|
||
| test("GET /tasks/workflow/abc SPA fallback serves same injected HTML", async () => { | ||
| const res = await request(appWithBasePath).get("/tasks/workflow/abc"); | ||
| expect.equals(res.status, 200); | ||
| expect.contains(`<base href="/tasks/">`, res.text); | ||
| expect.contains(`window.__BASE_PATH__="/tasks"`, res.text); | ||
| }), | ||
|
|
||
| test("GET /tasks/ serves injected HTML", async () => { | ||
| const res = await request(appWithBasePath).get("/tasks/"); | ||
| expect.equals(res.status, 200); | ||
| expect.contains(`<base href="/tasks/">`, res.text); | ||
| }), | ||
|
|
||
| test("HTML responses have Cache-Control: no-cache", async () => { | ||
| const res = await request(appWithBasePath).get("/tasks"); | ||
| expect.equals(res.headers["cache-control"], "no-cache"); | ||
| }), | ||
|
|
||
| test("GET /random-path outside base path returns 404", async () => { | ||
| const res = await request(appWithBasePath).get("/random-path"); | ||
| expect.equals(res.status, 404); | ||
| }), | ||
| ]), | ||
|
|
||
| group("with BASE_PATH=/another-path", [ | ||
| test("GET / redirects 301 to /another-path", async () => { | ||
| const res = await request(appAtAnotherPath).get("/"); | ||
| expect.equals(res.status, 301); | ||
| expect.equals(res.headers["location"], "/another-path"); | ||
| }), | ||
|
|
||
| test("GET /another-path serves HTML with injected base href", async () => { | ||
| const res = await request(appAtAnotherPath).get("/another-path"); | ||
| expect.equals(res.status, 200); | ||
| expect.contains(`<base href="/another-path/">`, res.text); | ||
| expect.contains(`window.__BASE_PATH__="/another-path"`, res.text); | ||
| }), | ||
|
|
||
| test("GET /another-path/deep/route SPA fallback serves injected HTML", async () => { | ||
| const res = await request(appAtAnotherPath).get("/another-path/deep/route"); | ||
| expect.equals(res.status, 200); | ||
| expect.contains(`<base href="/another-path/">`, res.text); | ||
| expect.contains(`window.__BASE_PATH__="/another-path"`, res.text); | ||
| }), | ||
|
|
||
| test("GET /tasks returns 404 (different base path)", async () => { | ||
| const res = await request(appAtAnotherPath).get("/tasks"); | ||
| expect.equals(res.status, 404); | ||
| }), | ||
| ]), | ||
|
|
||
| group("with BASE_PATH empty (root deployment)", [ | ||
| test("GET / serves HTML directly without redirect", async () => { | ||
| const res = await request(appAtRoot).get("/"); | ||
| expect.equals(res.status, 200); | ||
| expect.equals(res.type, "text/html"); | ||
| }), | ||
|
|
||
| test("GET / injects base href of / and empty __BASE_PATH__", async () => { | ||
| const res = await request(appAtRoot).get("/"); | ||
| expect.contains(`<base href="/">`, res.text); | ||
| expect.contains(`window.__BASE_PATH__=""`, res.text); | ||
| }), | ||
|
|
||
| test("GET /some/route SPA fallback serves HTML with root base href", async () => { | ||
| const res = await request(appAtRoot).get("/some/route"); | ||
| expect.equals(res.status, 200); | ||
| expect.contains(`<base href="/">`, res.text); | ||
| }), | ||
| ]), | ||
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| interface Window { | ||
| __BASE_PATH__: string | undefined; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.