-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
d525c13
commit e181ad5
Showing
3 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import test from "ava" | ||
import { getTestRoute } from "../fixtures/get-test-route.js" | ||
|
||
test("basic-01", async (t) => { | ||
const { axios } = await getTestRoute(t, { | ||
globalSpec: {}, | ||
routeSpec: { | ||
auth: "none", | ||
methods: ["GET", "POST"], | ||
}, | ||
routePath: "/health", | ||
routeFn: async (req: any) => { | ||
return new Response( | ||
JSON.stringify({ | ||
ok: true, | ||
}) | ||
) | ||
}, | ||
}) | ||
|
||
const { data: res } = await axios.get("/health") | ||
|
||
t.is(res.ok, true) | ||
}) |
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,33 @@ | ||
import test from "ava" | ||
import { getTestRoute } from "../fixtures/get-test-route.js" | ||
import { z } from "zod" | ||
|
||
test("json-body-01", async (t) => { | ||
const { axios, serverUrl } = await getTestRoute(t, { | ||
globalSpec: {}, | ||
routeSpec: { | ||
auth: "none", | ||
methods: ["GET", "POST"], | ||
jsonBody: z.object({ | ||
name: z.string(), | ||
}), | ||
}, | ||
routePath: "/post-body", | ||
routeFn: async (req: any) => { | ||
const jsonBody = await req.json() | ||
return new Response( | ||
JSON.stringify({ | ||
ok: true, | ||
jsonBody, | ||
}) | ||
) | ||
}, | ||
}) | ||
|
||
const { data: res } = await axios.post("/post-body", { | ||
name: "hello", | ||
}) | ||
|
||
t.is(res.ok, true) | ||
t.is(res.jsonBody.name, "hello") | ||
}) |
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,78 @@ | ||
import { createWithEdgeSpec } from "src/create-with-edge-spec.js" | ||
import type { AxiosInstance } from "axios" | ||
import getPort from "@ava/get-port" | ||
import defaultAxios from "redaxios" | ||
import { createServerFromRouteMap } from "src/serve/create-server-from-route-map.js" | ||
import { EdgeSpecRouteFn } from "src/types/web-handler" | ||
import { ExecutionContext } from "ava" | ||
import { once } from "events" | ||
|
||
export const getTestRoute = async ( | ||
t: ExecutionContext, | ||
opts: { | ||
globalSpec: any | ||
routeSpec: any | ||
routePath: string | ||
routeFn: EdgeSpecRouteFn | ||
} | ||
): Promise<{ serverUrl: string; axios: AxiosInstance }> => { | ||
const withRouteSpec = createWithEdgeSpec(opts.globalSpec) | ||
|
||
const wrappedRouteFn = withRouteSpec(opts.routeSpec)(opts.routeFn) | ||
|
||
const port = await getPort() | ||
|
||
const app = await createServerFromRouteMap( | ||
{ | ||
[opts.routePath]: wrappedRouteFn, | ||
}, | ||
{ | ||
defaultOrigin: `http://localhost:${port}`, | ||
} | ||
) | ||
|
||
// const app = http.createServer(async (nReq, nRes) => { | ||
// try { | ||
// const webReq = new Request(`http://localhost${nReq.url}`, { | ||
// headers: nReq.headers as any, | ||
// method: nReq.method, | ||
// body: ["GET", "HEAD"].includes(nReq.method!) | ||
// ? undefined | ||
// : (nReq as any), | ||
// }) | ||
|
||
// const res: Response = await wrappedRouteFn(webReq) | ||
// nRes.statusCode = res.status ?? 200 | ||
// const json = res.json && (await res.json().catch((e) => null)) | ||
// const text = res.text && (await res.text().catch((e) => null)) | ||
// if (json) { | ||
// nRes.setHeader("Content-Type", "application/json") | ||
// nRes.end(JSON.stringify(json)) | ||
// } else if (text) { | ||
// nRes.end(text) | ||
// } else { | ||
// throw new Error("Couldn't read response body") | ||
// } | ||
// } catch (e: any) { | ||
// nRes.statusCode = 500 | ||
// nRes.end(e.toString()) | ||
// } | ||
// }) | ||
|
||
app.listen(port) | ||
t.teardown(async () => { | ||
const closePromise = once(app, "close") | ||
app.close() | ||
return closePromise | ||
}) | ||
const serverUrl = `http://localhost:${port}` | ||
|
||
const axios: any = defaultAxios.create({ | ||
baseURL: serverUrl, | ||
}) | ||
|
||
return { | ||
serverUrl, | ||
axios, | ||
} | ||
} |