deno.land / x / abc@v1.3.3 / middleware / logger_test.ts

logger_test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Context } from "../context.ts";
import { assert, assertEquals,} from "../vendor/https/deno.land/std/testing/asserts.ts";import { DefaultFormatter, logger } from "./logger.ts";const { test, makeTempFileSync, readFileSync, openSync, removeSync } = Deno;
const dt = new Date();const ctx = { request: { method: "GET", url: "", proto: "HTTP/1.1", },} as Context;const decoder = new TextDecoder();
test("middleware logger", function (): void { const fpath = makeTempFileSync(); const f = openSync(fpath, { write: true }); logger({ output: f, })((c) => c)(ctx); const out = decoder.decode(readFileSync(fpath)); assert(out.includes(" GET / HTTP/1.1\n")); assert(new Date(out.split(" ")[0]).getTime() >= dt.getTime()); f.close(); removeSync(fpath);});
test("middleware logger default formatter", function (): void { const logInfo = DefaultFormatter(ctx); assert(logInfo.endsWith(" GET / HTTP/1.1\n")); assert(new Date(logInfo.split(" ")[0]).getTime() >= dt.getTime());});
test("middleware logger custom formatter", function (): void { const fpath = makeTempFileSync(); const f = openSync(fpath, { write: true }); const info = "Hello, 你好!"; logger({ output: f, formatter: (): string => info, })((c) => c)(ctx); const out = decoder.decode(readFileSync(fpath)); assertEquals(out, info); f.close(); removeSync(fpath);});
abc

Version Info

Tagged at
2 years ago