Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import { assert, assertEquals, assertStrictEq, test } from "./test_deps.ts";
import { Application } from "./application.ts";import { Context } from "./context.ts";import { Status } from "./deps.ts";import { httpErrors } from "./httpError.ts";import { send } from "./send.ts";
let encodingsAccepted = "identity";
function createMockApp< S extends Record<string | number | symbol, any> = Record<string, any>,>( state = {} as S,): Application<S> { return { state, } as any;}
function createMockContext< S extends Record<string | number | symbol, any> = Record<string, any>,>( app: Application<S>, path = "/", method = "GET",) { return ({ app, request: { acceptsEncodings() { return encodingsAccepted; }, headers: new Headers(), method, path, search: undefined, searchParams: new URLSearchParams(), url: new URL(`http://localhost${path}`), }, response: { status: Status.OK, body: undefined, headers: new Headers(), }, state: app.state, } as unknown) as Context<S>;}
function setup< S extends Record<string | number | symbol, any> = Record<string, any>,>( path = "/", method = "GET",): { app: Application<S>; context: Context<S>;} { encodingsAccepted = "identity"; const app = createMockApp<S>(); const context = createMockContext<S>(app, path, method); return { app, context };}
test({ name: "send HTML", async fn() { const { context } = setup("/test.html"); const fixture = await Deno.readFile("./fixtures/test.html"); await send(context, context.request.url.pathname, { root: "./fixtures", }); assertEquals(context.response.body, fixture); assertEquals(context.response.type, ".html"); assertEquals( context.response.headers.get("content-length"), String(fixture.length), ); assert(context.response.headers.get("last-modified") != null); assertEquals(context.response.headers.get("cache-control"), "max-age=0"); },});
test({ name: "send gzip", async fn() { const { context } = setup("/test.json"); const fixture = await Deno.readFile("./fixtures/test.json.gz"); encodingsAccepted = "gzip"; await send(context, context.request.url.pathname, { root: "./fixtures", }); assertEquals(context.response.body, fixture); assertEquals(context.response.type, ".json"); assertEquals(context.response.headers.get("content-encoding"), "gzip"); assertEquals( context.response.headers.get("content-length"), String(fixture.length), ); },});
test({ name: "send brotli", async fn() { const { context } = setup("/test.json"); const fixture = await Deno.readFile("./fixtures/test.json.br"); encodingsAccepted = "br"; await send(context, context.request.url.pathname, { root: "./fixtures", }); assertEquals(context.response.body, fixture); assertEquals(context.response.type, ".json"); assertEquals(context.response.headers.get("content-encoding"), "br"); assertEquals( context.response.headers.get("content-length"), String(fixture.length), ); },});
test({ name: "send identity", async fn() { const { context } = setup("/test.json"); const fixture = await Deno.readFile("./fixtures/test.json"); await send(context, context.request.url.pathname, { root: "./fixtures", }); assertEquals(context.response.body, fixture); assertEquals(context.response.type, ".json"); assertStrictEq(context.response.headers.get("content-encoding"), null); assertEquals( context.response.headers.get("content-length"), String(fixture.length), ); },});
test({ name: "send 404", async fn() { const { context } = setup("/foo.txt"); encodingsAccepted = "identity"; let didThrow = false; try { await send(context, context.request.url.pathname, { root: "./fixtures", }); } catch (e) { assert(e instanceof httpErrors.NotFound); didThrow = true; } assert(didThrow); },});