Skip to content

Commit

Permalink
Add back endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 19, 2024
1 parent d525c13 commit e181ad5
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/endpoints/basic-01.test.ts
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)
})
33 changes: 33 additions & 0 deletions tests/endpoints/json-body-01.test.ts
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")
})
78 changes: 78 additions & 0 deletions tests/fixtures/get-test-route.ts
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,
}
}

0 comments on commit e181ad5

Please sign in to comment.