Module
A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕
Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374import { Body } from "./body.ts";import { assert, concat, isHttpError, Status, timingSafeEqual,} from "./deps.ts";import { assertEquals, assertRejects } from "./test_deps.ts";import { type ServerRequest } from "./types.ts";
const MULTIPART_BODY_FIXTURE = `--OAK-SERVER-BOUNDARY\r\nContent-Disposition: form-data; name="hello"\r\n\r\nworld\r\n--OAK-SERVER-BOUNDARY--\r\n`;
const MULTIPART_CONTENTTYPE_FIXTURE = `multipart/form-data; boundary=OAK-SERVER-BOUNDARY`;
const encoder = new TextEncoder();
function nativeToServer( request: Request,): Pick<ServerRequest, "request" | "headers" | "getBody"> { return { request, headers: request.headers, getBody() { return request.body; }, };}
function nodeToServer( headers: HeadersInit, body: BodyInit,): Pick<ServerRequest, "request" | "headers" | "getBody"> { const req = new Request("https://localhost", { method: "POST", body, headers, }); return { headers: req.headers, getBody() { return req.body; }, };}
Deno.test({ name: "body - form - native request", async fn() { const rBody = `foo=bar&bar=1&baz=qux+%2B+quux`; const body = new Body(nativeToServer( new Request( "http://localhost/index.html", { body: rBody, method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": String(rBody.length), }, }, ), )); assert(body.has); assertEquals(body.type(), "form"); const actual = await body.form(); assertEquals( Array.from(actual.entries()), [["foo", "bar"], ["bar", "1"], ["baz", "qux + quux"]], ); },});
Deno.test({ name: "body - form - node request", async fn() { const rBody = `foo=bar&bar=1&baz=qux+%2B+quux`; const body = new Body(nodeToServer( { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": String(rBody.length), }, rBody, )); assert(body.has); assertEquals(body.type(), "form"); const actual = await body.form(); assertEquals( Array.from(actual.entries()), [["foo", "bar"], ["bar", "1"], ["baz", "qux + quux"]], ); },});
Deno.test({ name: "body - formData - native request", async fn() { const body = new Body(nativeToServer( new Request( "http://localhost/index.html", { body: MULTIPART_BODY_FIXTURE, method: "POST", headers: { "content-type": MULTIPART_CONTENTTYPE_FIXTURE, }, }, ), )); assert(body.has); assertEquals(body.type(), "form-data"); const actual = await body.formData(); assertEquals([...actual], [ ["hello", "world"], ]); },});
Deno.test({ name: "body - formData - node request", async fn() { const body = new Body( nodeToServer( { "content-type": MULTIPART_CONTENTTYPE_FIXTURE }, MULTIPART_BODY_FIXTURE, ), ); assert(body.has); assertEquals(body.type(), "form-data"); const actual = await body.formData(); assertEquals([...actual], [ ["hello", "world"], ]); },});
Deno.test({ name: "body - json - native request", async fn() { const rBody = JSON.stringify({ hello: "world" }); const body = new Body( nativeToServer( new Request( "http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/json", "content-length": String(rBody.length), }, }, ), ), ); assert(body.has); assertEquals(body.type(), "json"); assertEquals(await body.json(), { hello: "world" }); },});
Deno.test({ name: "body - json - node request", async fn() { const rBody = JSON.stringify({ hello: "world" }); const body = new Body( nodeToServer( { "content-type": "application/json", "content-length": String(rBody.length), }, rBody, ), ); assert(body.has); assertEquals(body.type(), "json"); assertEquals(await body.json(), { hello: "world" }); },});
Deno.test({ name: "body - json does not parse - native request", async fn() { const rBody = `{ hello: "world" }`; const body = new Body( nativeToServer( new Request( "http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/json", "content-length": String(rBody.length), }, }, ), ), ); assert(body.has); assertEquals(body.type(), "json"); const err = await assertRejects(async () => { await body.json(); }, Error); assert(isHttpError(err)); assertEquals(err.status, Status.BadRequest); },});
Deno.test({ name: "body - json does not parse - node request", async fn() { const rBody = `{ hello: "world" }`; const body = new Body( nodeToServer( { "content-type": "application/json", "content-length": String(rBody.length), }, rBody, ), ); assert(body.has); assertEquals(body.type(), "json"); const err = await assertRejects(async () => { await body.json(); }, Error); assert(isHttpError(err)); assertEquals(err.status, Status.BadRequest); },});
Deno.test({ name: "body - text - native request", async fn() { const rBody = "hello world"; const body = new Body( nativeToServer( new Request("http://localhost:8080", { body: rBody, method: "POST", headers: { "content-type": "text/plain" }, }), ), ); assert(body.has); assertEquals(body.type(), "text"); assertEquals(await body.text(), rBody); },});
Deno.test({ name: "body - text - node request", async fn() { const rBody = "hello world"; const body = new Body( nodeToServer({ "content-type": "text/plain" }, rBody), ); assert(body.has); assertEquals(body.type(), "text"); assertEquals(await body.text(), rBody); },});
Deno.test({ name: "body - arrayBuffer - native request", async fn() { const rBody = "hello world"; const expected = encoder.encode(rBody); const body = new Body(nativeToServer( new Request("http://localhost:8080", { body: rBody, method: "POST", headers: { "content-type": "application/octet-stream" }, }), )); assert(body.has); assertEquals(body.type(), "binary"); assert(timingSafeEqual(await body.arrayBuffer(), expected)); },});
Deno.test({ name: "body - arrayBuffer - node request", async fn() { const rBody = "hello world"; const expected = encoder.encode(rBody); const body = new Body( nodeToServer({ "content-type": "application/octet-stream" }, rBody), ); assert(body.has); assertEquals(body.type(), "binary"); assert(timingSafeEqual(await body.arrayBuffer(), expected)); },});
Deno.test({ name: "body - blob - native request", async fn() { const rBody = "hello world"; const expected = encoder.encode(rBody); const body = new Body(nativeToServer( new Request("http://localhost:8080", { body: rBody, method: "POST", headers: { "content-type": "application/octet-stream" }, }), )); assert(body.has); assertEquals(body.type(), "binary"); const actual = await body.blob(); assert(timingSafeEqual(await actual.arrayBuffer(), expected)); },});
Deno.test({ name: "body - blob - node request", async fn() { const rBody = "hello world"; const expected = encoder.encode(rBody); const body = new Body( nodeToServer({ "content-type": "application/octet-stream" }, rBody), ); assert(body.has); assertEquals(body.type(), "binary"); const actual = await body.blob(); assert(timingSafeEqual(await actual.arrayBuffer(), expected)); },});
Deno.test({ name: "body - stream - native request", async fn() { const rBody = "hello world"; const expected = encoder.encode(rBody); const body = new Body(nativeToServer( new Request("http://localhost:8080", { body: rBody, method: "POST", headers: { "content-type": "application/octet-stream" }, }), )); assert(body.has); assertEquals(body.type(), "binary"); assert(body.stream); let actual = new Uint8Array(); for await (const chunk of body.stream) { actual = concat([actual, chunk]); } assert(timingSafeEqual(actual, expected)); },});
Deno.test({ name: "body - stream - node request", async fn() { const rBody = "hello world"; const expected = encoder.encode(rBody); const body = new Body( nodeToServer({ "content-type": "application/octet-stream" }, rBody), ); assert(body.has); assertEquals(body.type(), "binary"); assert(body.stream); let actual = new Uint8Array(); for await (const chunk of body.stream) { actual = concat([actual, chunk]); } assert(timingSafeEqual(actual, expected)); },});