Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import { test, assert, assertEquals, assertStrictEq } from "./test_deps.ts";import { Application } from "./application.ts";import { Context } from "./context.ts";import { serve, Server, ServerRequest } from "./deps.ts";
let serverRequestStack: ServerRequest[] = [];let addrStack: string[] = [];
function teardown() { serverRequestStack = []; addrStack = [];}
class MockServer { close(): void {}
async *[Symbol.asyncIterator]() { for await (const request of serverRequestStack) { yield request; } }}
const mockServe: typeof serve = function(addr: string): Server { addrStack.push(addr); return new MockServer() as Server;};
function createMockRequest(url = "https://example.com/"): ServerRequest { return { url, async respond() {} } as any;}
test(function constructApp() { const app = new Application(); assert(app instanceof Application);});
test(async function registerMiddleware() { serverRequestStack.push(createMockRequest()); const app = new Application(mockServe); let called = 0; app.use((context, next) => { assert(context instanceof Context); assertEquals(typeof next, "function"); called++; });
await app.listen(""); assertEquals(called, 1); teardown();});
test(async function middlewareExecutionOrder1() { serverRequestStack.push(createMockRequest()); const app = new Application(mockServe); const callStack: number[] = []; app.use(() => { callStack.push(1); });
app.use(() => { callStack.push(2); });
await app.listen(""); assertEquals(callStack, [1]); teardown();});
test(async function middlewareExecutionOrder2() { serverRequestStack.push(createMockRequest()); const app = new Application(mockServe); const callStack: number[] = []; app.use((_context, next) => { callStack.push(1); next(); });
app.use(() => { callStack.push(2); });
await app.listen(""); assertEquals(callStack, [1, 2]); teardown();});
test(async function middlewareExecutionOrder3() { serverRequestStack.push(createMockRequest()); const app = new Application(mockServe); const callStack: number[] = []; app.use((_context, next) => { callStack.push(1); next(); callStack.push(2); });
app.use(async () => { callStack.push(3); await Promise.resolve(); callStack.push(4); });
await app.listen(""); assertEquals(callStack, [1, 3, 2, 4]); teardown();});
test(async function middlewareExecutionOrder4() { serverRequestStack.push(createMockRequest()); const app = new Application(mockServe); const callStack: number[] = []; app.use(async (_context, next) => { callStack.push(1); await next(); callStack.push(2); });
app.use(async () => { callStack.push(3); await Promise.resolve(); callStack.push(4); });
await app.listen(""); assertEquals(callStack, [1, 3, 4, 2]); teardown();});
test(async function appListen() { const app = new Application(mockServe); await app.listen("127.0.0.1:8080"); assertEquals(addrStack, ["127.0.0.1:8080"]); teardown();});
test(async function appState() { serverRequestStack.push(createMockRequest()); const app = new Application<{ foo?: string }>(mockServe); app.state.foo = "bar"; let called = false; app.use(context => { assertEquals(context.state, { foo: "bar" }); assertStrictEq(app.state, context.state); called = true; }); await app.listen(""); assert(called);});