-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
175 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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,14 @@ | ||
import { type RequestContext } from "../../types"; | ||
|
||
export function GET(request: RequestContext) { | ||
return new Response(JSON.stringify({ hello: "world" }), { | ||
headers: { "content-type": "application/json" }, | ||
}); | ||
} | ||
|
||
export async function POST(request: RequestContext) { | ||
const formData = await request.formData(); | ||
const name = formData.get("name"); | ||
const email = formData.get("email"); | ||
return new Response(JSON.stringify({ name, email })); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1 +1,7 @@ | ||
// for testing purposes | ||
export const Head = () => { | ||
return <title id="title">Page not found</title>; | ||
}; | ||
|
||
export default async function _404() { | ||
return <h1>Page not found 404</h1>; | ||
} |
This file contains 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
// for testing purposes | ||
export default async function SomePage() { | ||
return <h1>Some page</h1>; | ||
} |
Empty file.
This file contains 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,24 @@ | ||
import fs from "node:fs"; | ||
import getConstants from "../../constants"; | ||
import { serveOptions } from "./serve-options"; | ||
|
||
const { IS_PRODUCTION, ROOT_DIR, PAGES_DIR } = getConstants(); | ||
|
||
if (IS_PRODUCTION && !fs.existsSync(ROOT_DIR)) { | ||
console.error('Not exist "build" yet. Please run "brisa build" first'); | ||
process.exit(1); | ||
} | ||
|
||
if (!fs.existsSync(PAGES_DIR)) { | ||
const path = IS_PRODUCTION ? "build/pages" : "src/pages"; | ||
const cli = IS_PRODUCTION ? "brisa start" : "brisa dev"; | ||
|
||
console.error(`Not exist ${path}" directory. It\'s required to run "${cli}"`); | ||
process.exit(1); | ||
} | ||
|
||
const server = Bun.serve(serveOptions); | ||
|
||
console.log( | ||
`Listening on http://localhost:${server.port} (${process.env.NODE_ENV})...`, | ||
); |
This file contains 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,108 @@ | ||
import path from "node:path"; | ||
import { | ||
describe, | ||
it, | ||
expect, | ||
beforeEach, | ||
afterEach, | ||
beforeAll, | ||
afterAll, | ||
} from "bun:test"; | ||
import getConstants from "../../constants"; | ||
import { Serve, gc } from "bun"; | ||
import { GlobalRegistrator } from "@happy-dom/global-registrator"; | ||
|
||
const ROOT_DIR = path.join(import.meta.dir, "..", "..", "__fixtures__"); | ||
|
||
const PAGES_DIR = path.join(ROOT_DIR, "pages"); | ||
const ASSETS_DIR = path.join(ROOT_DIR, "assets"); | ||
let serveOptions: Serve; | ||
|
||
function testRequest(request: Request): Response { | ||
return ( | ||
// @ts-ignore | ||
(serveOptions.fetch(request, { requestIP: () => {}, upgrade: () => {} }) || | ||
new Response()) as Response | ||
); | ||
} | ||
|
||
describe("CLI: serve", () => { | ||
beforeAll(() => { | ||
GlobalRegistrator.unregister(); | ||
}); | ||
|
||
afterAll(() => { | ||
GlobalRegistrator.register(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
globalThis.mockConstants = { | ||
...(getConstants() ?? {}), | ||
PAGES_DIR, | ||
ROOT_DIR, | ||
SRC_DIR: ROOT_DIR, | ||
ASSETS_DIR, | ||
I18N_CONFIG: { | ||
locales: ["en", "es"], | ||
defaultLocale: "es", | ||
}, | ||
}; | ||
serveOptions = (await import("./serve-options")).serveOptions; | ||
}); | ||
|
||
afterEach(() => { | ||
globalThis.mockConstants = undefined; | ||
gc(true); | ||
}); | ||
|
||
it("should return 404 page", async () => { | ||
const response = await testRequest( | ||
new Request("http://localhost:1234/es/not-found-page"), | ||
); | ||
const html = await response.text(); | ||
|
||
expect(response.status).toBe(404); | ||
expect(html).toContain('<title id="title">Page not found</title>'); | ||
expect(html).not.toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
|
||
it("should return a page with layout and i18n", async () => { | ||
const response = await testRequest( | ||
new Request(`http://localhost:1234/es/somepage`), | ||
); | ||
const html = await response.text(); | ||
expect(response.status).toBe(200); | ||
expect(html).toContain('<html lang="es">'); | ||
expect(html).toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Some page</h1>"); | ||
}); | ||
|
||
it("should be possible to fetch an api route GET", async () => { | ||
const response = await testRequest( | ||
new Request(`http:///localhost:1234/es/api/example`), | ||
); | ||
const json = await response.json(); | ||
|
||
expect(response.status).toBe(200); | ||
expect(json).toEqual({ hello: "world" }); | ||
}); | ||
|
||
it("should be possible to fetch an api route POST with a FormData", async () => { | ||
const body = new FormData(); | ||
|
||
body.append("name", "Brisa"); | ||
body.append("email", "[email protected]"); | ||
|
||
const response = await testRequest( | ||
new Request(`http:///localhost:1234/es/api/example`, { | ||
method: "POST", | ||
body, | ||
}), | ||
); | ||
const json = await response.json(); | ||
|
||
expect(response.status).toBe(200); | ||
expect(json).toEqual({ name: "Brisa", email: "[email protected]" }); | ||
}); | ||
}); |
This file contains 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