Skip to content

Commit

Permalink
fix: basic tests now work
Browse files Browse the repository at this point in the history
  • Loading branch information
aarontravass committed Jun 26, 2023
1 parent 8957526 commit 11725c2
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
Empty file added README.md
Empty file.
247 changes: 247 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions dev_depts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


export { makeFetch } from "https://deno.land/x/[email protected]/mod.ts";
export {
describe,
expect,
it,
run,
} from "https://deno.land/x/[email protected]/mod.ts";
export { Server } from "https://deno.land/[email protected]/http/server.ts";
export type { ConnInfo } from "https://deno.land/[email protected]/http/server.ts";
23 changes: 13 additions & 10 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ConnInfo } from "./dev_depts.ts";

export enum LogLevel {
error = "error",
warn = "warn",
Expand All @@ -15,15 +17,15 @@ export interface LoggerOptions {
}

const generateLog =
(req: Request, res: Response) =>
(logArray: string[], options?: LoggerOptions) => {
const url = req["originalUrl"] || req.url;

if (options?.output?.level)
logArray.push("[" + options.output?.level.toUpperCase() + "]");

if (options?.ip) logArray.push(req.headers.get("host") ?? "");
(req: Request, res: Response, connInfo?: ConnInfo) =>
(logArray: string[], options: LoggerOptions) => {
const url = req.url;

logArray.push(
"[" + (options.output?.level || LogLevel.log).toUpperCase() + "]"
);
if (options?.ip)
logArray.push((connInfo?.remoteAddr as Deno.NetAddr)?.hostname ?? "");
logArray.push(res.status.toString());
logArray.push(req.method.toUpperCase());
logArray.push(url);
Expand All @@ -34,9 +36,10 @@ export const logger = (options: LoggerOptions) => {
callback: console.log,
level: LogLevel.log,
};
return (req: Request, res: Response, next?: () => void) => {

return (req: Request, res: Response, connInfo?: ConnInfo) => {
const args: string[] = [];
generateLog(req, res)(args, options);
generateLog(req, res, connInfo)(args, options);
const logString = args.join(" ");
output.callback(logString);
};
Expand Down
20 changes: 20 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, makeFetch, describe, it, run, ConnInfo } from "./dev_depts.ts";
import { logger } from "./mod.ts";

describe("Basic tests", () => {
it("should write logs", async () => {
const callback = (line: string) => {
expect(line).toBeDefined();
console.log(line);
};
const handler = (req: Request, connInfo: ConnInfo) => {
const res = new Response("hello world", { status: 200 });
logger({ output: { callback }, ip: true })(req, res, connInfo);
return res;
};

(await makeFetch(handler)("/")).expectStatus(200);
});
});

run();

0 comments on commit 11725c2

Please sign in to comment.