Skip to main content
Module

x/acorn/handlers_test.ts

A focused RESTful server framework for Deno πŸŒ°πŸ¦•
Go to Latest
File
// Copyright 2022 the oak authors. All rights reserved.
import { assert, assertEquals, assertRejects,} from "https://deno.land/std@0.138.0/testing/asserts.ts";import { Cookies, errors, Status } from "./deps.ts";import { Context } from "./context.ts";
import { auth, immutable } from "./handlers.ts";
Deno.test({ name: "handler - auth() - authorized", async fn() { const handlerWithOptions = auth( () => ({ hello: "world" }), { authorize: () => true }, ); const context = new Context({ request: new Request("https://example.com/"), params: {}, cookies: new Cookies(new Headers(), new Headers()), }); const response = await handlerWithOptions.handler(context); assertEquals(response, { hello: "world" }); },});
Deno.test({ name: "handler - auth() - unauthorized", async fn() { const handlerWithOptions = auth( () => ({ hello: "world" }), { authorize: () => false }, ); const context = new Context({ request: new Request("https://example.com/"), params: {}, cookies: new Cookies(new Headers(), new Headers()), }); await assertRejects( async () => { await handlerWithOptions.handler(context); }, errors.Unauthorized, "Unauthorized", ); },});
Deno.test({ name: "handler - auth() - return body init", async fn() { const handlerWithOptions = auth( () => ({ hello: "world" }), { authorize: () => "not authorized" }, ); const context = new Context({ request: new Request("https://example.com/"), params: {}, cookies: new Cookies(new Headers(), new Headers()), }); const response = await handlerWithOptions.handler(context); assert(response instanceof Response); assertEquals(response.status, Status.Unauthorized); assertEquals(await response.text(), "not authorized"); },});
Deno.test({ name: "handler - immutable() - no options", async fn() { const handlerWithOptions = immutable({ hello: "world" }); assertEquals(Object.keys(handlerWithOptions), ["handler"]); const context = new Context({ request: new Request("https://example.com/"), params: {}, cookies: new Cookies(new Headers(), new Headers()), }); const response = await handlerWithOptions.handler(context); assert(response instanceof Response); assertEquals([...response.headers], [[ "cache-control", "public, max-age=604800, immutable", ], [ "content-type", "application/json; charset=UTF-8", ]]); assertEquals(await response.text(), `{"hello":"world"}`); },});