|
| 1 | +import { Fields } from "./Fields"; |
| 2 | +import { getHeadersProxy, headersToFields, headerValueToFieldValues } from "./headersProxy"; |
| 3 | + |
| 4 | +describe("getHeadersProxy", () => { |
| 5 | + const mockHeaders = { |
| 6 | + foo: "bar", |
| 7 | + baz: "qux,quux", |
| 8 | + }; |
| 9 | + const mockFields = Fields.from([ |
| 10 | + { name: "foo", values: ["bar"] }, |
| 11 | + { name: "baz", values: ["qux", "quux"] }, |
| 12 | + ]); |
| 13 | + |
| 14 | + describe("proxy works like a normal Record", () => { |
| 15 | + describe("access and mutation", () => { |
| 16 | + it("can get and set individual keys", () => { |
| 17 | + const headers = getHeadersProxy(new Fields({})); |
| 18 | + headers["foo"] = "bar"; |
| 19 | + headers.baz = "qux,quux"; |
| 20 | + expect(headers["foo"]).toEqual("bar"); |
| 21 | + expect(headers.baz).toEqual("qux,quux"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("can be updated using object spread syntax", () => { |
| 25 | + let headers = getHeadersProxy(mockFields); |
| 26 | + headers = { ...headers, another: "value" }; |
| 27 | + expect(headers).toEqual({ ...mockHeaders, another: "value" }); |
| 28 | + }); |
| 29 | + |
| 30 | + it("can delete keys", () => { |
| 31 | + const headers = getHeadersProxy(new Fields({ fields: mockFields.getAll() })); |
| 32 | + delete headers["foo"]; |
| 33 | + delete headers.baz; |
| 34 | + expect(headers).toEqual({}); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("iteration", () => { |
| 39 | + it("can be iterated over using Object.keys", () => { |
| 40 | + const headers = getHeadersProxy(mockFields); |
| 41 | + const keys = Object.keys(headers); |
| 42 | + expect(keys).toEqual(["foo", "baz"]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("can be iterated over using Object.values", () => { |
| 46 | + const headers = getHeadersProxy(mockFields); |
| 47 | + const values = Object.values(headers); |
| 48 | + expect(values).toEqual(["bar", "qux,quux"]); |
| 49 | + }); |
| 50 | + |
| 51 | + it("can be iterated over using Object.entries", () => { |
| 52 | + const headers = getHeadersProxy(mockFields); |
| 53 | + const entries = Object.entries(headers); |
| 54 | + expect(entries).toEqual([ |
| 55 | + ["foo", "bar"], |
| 56 | + ["baz", "qux,quux"], |
| 57 | + ]); |
| 58 | + }); |
| 59 | + |
| 60 | + it("can be iterated over using `for..in`", () => { |
| 61 | + const keys: string[] = []; |
| 62 | + const headers = getHeadersProxy(mockFields); |
| 63 | + for (const key in headers) { |
| 64 | + keys.push(key); |
| 65 | + } |
| 66 | + expect(keys).toEqual(["foo", "baz"]); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("proxies the fields", () => { |
| 72 | + it("updates fields when individual keys are set on headers", () => { |
| 73 | + const fields = new Fields({}); |
| 74 | + const headers = getHeadersProxy(fields); |
| 75 | + headers["foo"] = "bar"; |
| 76 | + headers.baz = "qux,quux"; |
| 77 | + expect(fields).toEqual(mockFields); |
| 78 | + }); |
| 79 | + |
| 80 | + it("updates fields when keys are deleted from headers", () => { |
| 81 | + const fields = new Fields({ fields: mockFields.getAll() }); |
| 82 | + const headers = getHeadersProxy(fields); |
| 83 | + delete headers["foo"]; |
| 84 | + delete headers.baz; |
| 85 | + expect(fields).toEqual(new Fields({})); |
| 86 | + }); |
| 87 | + |
| 88 | + it("can get values from fields or headers", () => { |
| 89 | + const headers = getHeadersProxy(mockFields); |
| 90 | + expect(headers["foo"]).toEqual(mockFields.getField("foo")?.values.join(",")); |
| 91 | + expect(headers.baz).toEqual(mockFields.getField("baz")?.values.join(",")); |
| 92 | + }); |
| 93 | + |
| 94 | + it("does not proxy class properties of fields", () => { |
| 95 | + const fields = new Fields({}); |
| 96 | + Object.defineProperty(fields, "foo", { |
| 97 | + value: "bar", |
| 98 | + enumerable: true, |
| 99 | + writable: true, |
| 100 | + configurable: true, |
| 101 | + }); |
| 102 | + const headers = getHeadersProxy(fields); |
| 103 | + Object.keys(fields).forEach((key) => { |
| 104 | + expect(headers[key]).toBe(undefined); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it("can use Object prototype methods", () => { |
| 109 | + const fields = new Fields({ fields: mockFields.getAll() }); |
| 110 | + const headers = getHeadersProxy(fields); |
| 111 | + delete headers["foo"]; |
| 112 | + Object.defineProperty(headers, "fizz", { |
| 113 | + value: "buzz", |
| 114 | + enumerable: true, |
| 115 | + writable: true, |
| 116 | + configurable: true, |
| 117 | + }); |
| 118 | + expect(headers.hasOwnProperty("foo")).toBe(false); |
| 119 | + expect(headers.hasOwnProperty("baz")).toBe(true); |
| 120 | + expect(headers.hasOwnProperty("fizz")).toBe(true); |
| 121 | + expect(headers["fizz"]).toEqual("buzz"); |
| 122 | + expect("fizz" in headers).toBe(true); |
| 123 | + expect(headers.hasOwnProperty("encoding")).toBe(false); |
| 124 | + expect({ ...headers }).toEqual({ fizz: "buzz", baz: "qux,quux" }); |
| 125 | + expect(fields.getField("foo")).not.toBeDefined(); |
| 126 | + expect(fields.getField("fizz")?.toString()).toEqual("buzz"); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe("headersToFields", () => { |
| 132 | + it("ignores null and undefined values", () => { |
| 133 | + const headers = { foo: null as any, bar: undefined as any }; |
| 134 | + const fields = headersToFields(headers); |
| 135 | + expect(fields.getField("foo")).not.toBeDefined(); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe("headerValueToFieldValues", () => { |
| 140 | + it("ignores null and undefined values", () => { |
| 141 | + expect(headerValueToFieldValues(undefined as any)).not.toBeDefined(); |
| 142 | + expect(headerValueToFieldValues(null as any)).not.toBeDefined(); |
| 143 | + }); |
| 144 | + it("parses single string value", () => { |
| 145 | + const headerValue = "foo"; |
| 146 | + expect(headerValueToFieldValues(headerValue)).toEqual(["foo"]); |
| 147 | + }); |
| 148 | + it("parses comma-separated string value", () => { |
| 149 | + const headerValue = "foo,bar"; |
| 150 | + expect(headerValueToFieldValues(headerValue)).toEqual(["foo", "bar"]); |
| 151 | + }); |
| 152 | + it("preserves whitespace", () => { |
| 153 | + const headerValue = "foo, bar "; |
| 154 | + expect(headerValueToFieldValues(headerValue)).toEqual(["foo", " bar "]); |
| 155 | + }); |
| 156 | +}); |
0 commit comments