|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + formatBody, |
| 5 | + formatContentLength, |
| 6 | + formatHeaders, |
| 7 | + formatMethod, |
| 8 | + formatTime, |
| 9 | + formatUri, |
| 10 | +} from "@/logging/formatters"; |
| 11 | + |
| 12 | +describe("Logging formatters", () => { |
| 13 | + it("formats time in appropriate units", () => { |
| 14 | + expect(formatTime(500)).toBe("500ms"); |
| 15 | + expect(formatTime(1000)).toBe("1.00s"); |
| 16 | + expect(formatTime(5500)).toBe("5.50s"); |
| 17 | + expect(formatTime(60000)).toBe("1.00m"); |
| 18 | + expect(formatTime(150000)).toBe("2.50m"); |
| 19 | + expect(formatTime(3600000)).toBe("1.00h"); |
| 20 | + expect(formatTime(7200000)).toBe("2.00h"); |
| 21 | + }); |
| 22 | + |
| 23 | + describe("formatMethod", () => { |
| 24 | + it("normalizes HTTP methods to uppercase", () => { |
| 25 | + expect(formatMethod("get")).toBe("GET"); |
| 26 | + expect(formatMethod("post")).toBe("POST"); |
| 27 | + expect(formatMethod("PUT")).toBe("PUT"); |
| 28 | + expect(formatMethod("delete")).toBe("DELETE"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("defaults to GET for undefined", () => { |
| 32 | + expect(formatMethod(undefined)).toBe("GET"); |
| 33 | + expect(formatMethod("")).toBe("GET"); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("formatContentLength", () => { |
| 38 | + it("uses content-length header when available", () => { |
| 39 | + const result = formatContentLength({ "content-length": "1024" }, null); |
| 40 | + expect(result).toContain("1.02 kB"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("handles invalid content-length header", () => { |
| 44 | + const result = formatContentLength({ "content-length": "invalid" }, null); |
| 45 | + expect(result).toContain("?"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("calculates size for strings", () => { |
| 49 | + const result = formatContentLength({}, "hello"); |
| 50 | + expect(result).toContain("5 B"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("calculates size for buffers", () => { |
| 54 | + const result = formatContentLength({}, Buffer.from("test")); |
| 55 | + expect(result).toContain("4 B"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("estimates size for objects", () => { |
| 59 | + const result = formatContentLength({}, { foo: "bar" }); |
| 60 | + expect(result).toMatch(/~\d+/); |
| 61 | + }); |
| 62 | + |
| 63 | + it("handles circular references safely", () => { |
| 64 | + const circular: Record<string, unknown> = { a: 1 }; |
| 65 | + circular.self = circular; |
| 66 | + const result = formatContentLength({}, circular); |
| 67 | + expect(result).toMatch(/~\d+/); |
| 68 | + }); |
| 69 | + |
| 70 | + it("handles null and undefined", () => { |
| 71 | + expect(formatContentLength({}, null)).toContain("0"); |
| 72 | + expect(formatContentLength({}, undefined)).toContain("0"); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("formatUri", () => { |
| 77 | + it("returns URL when present", () => { |
| 78 | + expect(formatUri({ url: "https://example.com/api" })).toBe( |
| 79 | + "https://example.com/api", |
| 80 | + ); |
| 81 | + expect(formatUri({ url: "/relative/path" })).toBe("/relative/path"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("returns placeholder for missing URL", () => { |
| 85 | + expect(formatUri(undefined)).toContain("no url"); |
| 86 | + expect(formatUri({})).toContain("no url"); |
| 87 | + expect(formatUri({ url: "" })).toContain("no url"); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("formatHeaders", () => { |
| 92 | + it("formats headers as key-value pairs", () => { |
| 93 | + const headers = { |
| 94 | + "content-type": "application/json", |
| 95 | + accept: "text/html", |
| 96 | + }; |
| 97 | + const result = formatHeaders(headers); |
| 98 | + expect(result).toContain("content-type: application/json"); |
| 99 | + expect(result).toContain("accept: text/html"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("redacts sensitive headers", () => { |
| 103 | + const sensitiveHeaders = ["Coder-Session-Token", "Proxy-Authorization"]; |
| 104 | + |
| 105 | + sensitiveHeaders.forEach((header) => { |
| 106 | + const result = formatHeaders({ [header]: "secret-value" }); |
| 107 | + expect(result).toContain(`${header}: <redacted>`); |
| 108 | + expect(result).not.toContain("secret-value"); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("returns placeholder for empty headers", () => { |
| 113 | + expect(formatHeaders({})).toBe("<no headers>"); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe("formatBody", () => { |
| 118 | + it("formats various body types", () => { |
| 119 | + expect(formatBody({ key: "value" })).toContain("key: 'value'"); |
| 120 | + expect(formatBody("plain text")).toContain("plain text"); |
| 121 | + expect(formatBody([1, 2, 3])).toContain("1"); |
| 122 | + expect(formatBody(123)).toContain("123"); |
| 123 | + expect(formatBody(true)).toContain("true"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("handles circular references gracefully", () => { |
| 127 | + const circular: Record<string, unknown> = { a: 1 }; |
| 128 | + circular.self = circular; |
| 129 | + const result = formatBody(circular); |
| 130 | + expect(result).toBeTruthy(); |
| 131 | + expect(result).not.toContain("invalid body"); |
| 132 | + expect(result).toContain("a: 1"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("handles deep nesting", () => { |
| 136 | + const deep = { |
| 137 | + level1: { level2: { level3: { level4: { value: "deep" } } } }, |
| 138 | + }; |
| 139 | + const result = formatBody(deep); |
| 140 | + expect(result).toContain("level4: { value: 'deep' }"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("returns placeholder for empty values", () => { |
| 144 | + const emptyValues = [null, undefined, "", 0, false]; |
| 145 | + emptyValues.forEach((value) => { |
| 146 | + expect(formatBody(value)).toContain("no body"); |
| 147 | + }); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments