Skip to main content
Module

x/oak/body_test.ts

A middleware framework for handling HTTP with Deno 🐿️ 🦕
Extremely Popular
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
// Copyright 2018-2022 the oak authors. All rights reserved. MIT license.
import { RequestBody } from "./body.ts";import { readAll } from "./deps.ts";import { assert, assertEquals, assertRejects, assertStrictEquals,} from "./test_deps.ts";import type { ServerRequestBody } from "./types.d.ts";
const { test } = Deno;
const decoder = new TextDecoder();
const multipartContentType = `multipart/form-data; boundary=OAK-SERVER-BOUNDARY`;
const multipartFixture = `--OAK-SERVER-BOUNDARYContent-Disposition: form-data; name="hello"
world--OAK-SERVER-BOUNDARY--`;
function toServerRequestBody(request: Request): [ServerRequestBody, Headers] { return [{ // deno-lint-ignore no-explicit-any body: request.body as any, readBody: async () => { const ab = await request.arrayBuffer(); return new Uint8Array(ab); }, }, request.headers];}
test({ name: "body - form", async fn() { const rBody = `foo=bar&bar=1&baz=qux+%2B+quux`; const requestBody = new RequestBody( ...toServerRequestBody( new Request( "http://localhost/index.html", { body: rBody, method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": String(rBody.length), }, }, ), ), ); assert(requestBody.has()); const body = requestBody.get({}); assert(body.type === "form"); const actual = await body.value; assertEquals( Array.from(actual.entries()), [["foo", "bar"], ["bar", "1"], ["baz", "qux + quux"]], ); },});
test({ name: "body - form-data", async fn() { const requestBody = new RequestBody(...toServerRequestBody( new Request( "http://localhost/index.html", { body: multipartFixture, method: "POST", headers: { "content-type": multipartContentType, }, }, ), )); assert(requestBody.has()); const body = requestBody.get({}); assert(body.type === "form-data"); const actual = await body.value.read(); assertEquals(actual.fields, { hello: "world" }); },});
test({ name: "body - json", async fn() { const rBody = JSON.stringify({ hello: "world" }); const requestBody = new RequestBody( ...toServerRequestBody( new Request( "http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/json", "content-length": String(rBody.length), }, }, ), ), ); assert(requestBody.has()); const body = requestBody.get({}); assert(body.type === "json"); assertEquals(await body.value, { hello: "world" }); },});
test({ name: "body - bytes", async fn() { const rBody = `console.log("hello world!");\n`; const requestBody = new RequestBody( ...toServerRequestBody( new Request( "http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/javascript", "content-length": String(rBody.length), }, }, ), ), ); assert(requestBody.has()); const body = requestBody.get({}); assert(body.type === "bytes"); const actual = await body.value; assertEquals(decoder.decode(actual), `console.log("hello world!");\n`); },});
test({ name: "body - text", async fn() { const rBody = "hello"; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "text/plain", "content-length": String(rBody.length), }, }), ), ); assert(requestBody.has()); const body = requestBody.get({}); assert(body.type === "text"); assertEquals(await body.value, "hello"); },});
test({ name: "body - undefined", fn() { const requestBody = new RequestBody( ...toServerRequestBody(new Request("http://localhost/index.html")), ); assertEquals(requestBody.has(), false); const body = requestBody.get({}); assert(body.type === "undefined"); assertEquals(body.value, undefined); },});
test({ name: "body - type: reader", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: "hello world", method: "POST", headers: { "content-type": "text/plain", }, }), ), ); const body = requestBody.get({ type: "reader" }); assert(body.type === "reader"); const actual = await readAll(body.value); assertEquals(decoder.decode(actual), "hello world"); },});
test({ name: "body - type: stream", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: "hello world", method: "POST", headers: { "content-type": "text/plain", }, }), ), ); const body = requestBody.get({ type: "stream" }); assert(body.type === "stream"); const actual = await new Response(body.value).text(); assertEquals(actual, "hello world"); },});
test({ name: "body - type: form", async fn() { const rBody = `foo=bar&bar=1&baz=qux+%2B+quux`; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "Content-Type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get({ type: "form" }); assert(body.type === "form"); const actual = await body.value; assertEquals( Array.from(actual.entries()), [["foo", "bar"], ["bar", "1"], ["baz", "qux + quux"]], ); },});
test({ name: "body - type: form-data", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: multipartFixture, method: "POST", headers: { "content-type": "application/javascript; boundary=OAK-SERVER-BOUNDARY", }, }), ), ); const body = requestBody.get({ type: "form-data" }); assert(body.type === "form-data"); const actual = await body.value.read(); assertEquals(actual.fields, { hello: "world" }); },});
test({ name: "body - type: bytes", async fn() { const rBody = `console.log("hello world!");\n`; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "text/plain", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get({ type: "bytes" }); assert(body.type === "bytes"); const actual = await body.value; assertEquals(decoder.decode(actual), `console.log("hello world!");\n`); },});
test({ name: "body - type: json", async fn() { const rBody = JSON.stringify({ hello: "world" }); const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get({ type: "json" }); assert(body.type === "json"); assertEquals(await body.value, { hello: "world" }); },});
test({ name: "body - type: text", async fn() { const rBody = "hello"; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get({ type: "text" }); assert(body.type === "text"); assertEquals(await body.value, "hello"); },});
test({ name: "body - type - body undefined", async fn() { const requestBody = new RequestBody( ...toServerRequestBody(new Request("http://localhost/index.html")), ); assertEquals(requestBody.has(), false); const body = requestBody.get({ type: "text" }); assert(body.type === "text"); assertEquals(await body.value, ""); },});
test({ name: "body - contentTypes: form", async fn() { const rBody = `foo=bar&bar=1&baz=qux+%2B+quux`; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "Content-Type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get( { contentTypes: { form: ["application/javascript"] } }, ); assert(body.type === "form"); const actual = await body.value; assertEquals( Array.from(actual.entries()), [["foo", "bar"], ["bar", "1"], ["baz", "qux + quux"]], ); },});
test({ name: "body - contentTypes: form-data", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: multipartFixture, method: "POST", headers: { "content-type": "application/javascript; boundary=OAK-SERVER-BOUNDARY", }, }), ), ); const body = requestBody.get( { contentTypes: { formData: ["application/javascript"] } }, ); assert(body.type === "form-data"); const actual = await body.value.read(); assertEquals(actual.fields, { hello: "world" }); },});
test({ name: "body - contentTypes: bytes", async fn() { const rBody = `console.log("hello world!");\n`; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "text/plain", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get({ contentTypes: { bytes: ["text/plain"] } }); assert(body.type === "bytes"); const actual = await body.value; assertEquals(decoder.decode(actual), `console.log("hello world!");\n`); },});
test({ name: "body - contentTypes: json", async fn() { const rBody = JSON.stringify({ hello: "world" }); const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get( { contentTypes: { json: ["application/javascript"] } }, ); assert(body.type === "json"); assertEquals(await body.value, { hello: "world" }); },});
test({ name: "body - contentTypes: text", async fn() { const rBody = "hello"; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const body = requestBody.get( { contentTypes: { text: ["application/javascript"] } }, ); assert(body.type === "text"); assertEquals(await body.value, "hello"); },});
test({ name: "body - multiple gets memoized", fn() { const rBody = `console.log("hello world!");\n`; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: rBody, method: "POST", headers: { "content-type": "application/javascript", "content-length": String(rBody.length), }, }), ), ); const a = requestBody.get({}); const b = requestBody.get({}); assertStrictEquals(a.type, b.type); assertStrictEquals(a.value, b.value); assert(a !== b); },});
test({ name: "body - can get different types", async fn() { const body = JSON.stringify({ hello: "world" }); const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body, method: "POST", headers: { "content-type": "application/json", "content-length": String(body.length), }, }), ), ); const textBody = requestBody.get({ type: "text" }); assert(textBody.type === "text"); assertEquals(await textBody.value, body); const bodyJson = requestBody.get({}); assert(bodyJson.type === "json"); assertEquals(await bodyJson.value, { hello: "world" }); },});
test({ name: "body - multiple streams", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: "hello world", method: "POST", headers: { "content-type": "text/plain", }, }), ), ); const a = requestBody.get({ type: "stream" }); const b = requestBody.get({ type: "stream" }); assert(a.type === "stream"); assert(b.type === "stream"); const textA = await new Response(a.value).text(); const textB = await new Response(b.value).text(); assertEquals(textA, textB); },});
test({ name: "body - default limit no content type", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: "hello world", method: "POST", headers: { "content-type": "text/plain", }, }), ), ); const actual = requestBody.get(); await assertRejects( async () => { await actual.value; }, RangeError, "Body exceeds a limit of ", ); },});
test({ name: "body - limit set to 0", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: "hello world", method: "POST", headers: { "content-type": "text/plain", }, }), ), ); const actual = requestBody.get({ type: "text", limit: 0 }); assertEquals(await actual.value, "hello world"); },});
test({ name: "body - limit set to Infinity", async fn() { const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body: "hello world", method: "POST", headers: { "content-type": "text/plain", }, }), ), ); const actual = requestBody.get({ type: "text", limit: Infinity }); assertEquals(await actual.value, "hello world"); },});
test({ name: "body - limit set to 1000", async fn() { const body = "hello world"; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body, method: "POST", headers: { "content-type": "text/plain", "content-length": String(body.length), }, }), ), ); const actual = requestBody.get({ type: "text", limit: 1000 }); assertEquals(await actual.value, "hello world"); },});
test({ name: "body - exceeds limit", async fn() { const body = "hello world"; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body, method: "POST", headers: { "content-type": "text/plain", "content-length": String(body.length), }, }), ), ); const actual = requestBody.get({ type: "text", limit: 2 }); await assertRejects( async () => { await actual.value; }, RangeError, "Body exceeds a limit of ", ); },});
test({ name: "body - exceeds limit but can retrieve with different limit", async fn() { const body = "hello world"; const requestBody = new RequestBody( ...toServerRequestBody( new Request("http://localhost/index.html", { body, method: "POST", headers: { "content-type": "text/plain", "content-length": String(body.length), }, }), ), ); let actual = requestBody.get({ type: "text", limit: 2 }); await assertRejects( async () => { await actual.value; }, RangeError, "Body exceeds a limit of ", ); actual = requestBody.get(); assertEquals(await actual.value, "hello world"); },});