| 1 | import { describe, expect, it } from "@jest/globals"; |
| 2 | import type { RequestOptions } from "../schemas/models.js"; |
| 3 | import { mergeConfigYamlRequestOptions } from "./merge.js"; |
| 4 | |
| 5 | describe("mergeConfigYamlRequestOptions", () => { |
| 6 | it("should return undefined when both base and global are undefined", () => { |
| 7 | const result = mergeConfigYamlRequestOptions(undefined, undefined); |
| 8 | expect(result).toBeUndefined(); |
| 9 | }); |
| 10 | |
| 11 | it("should return global when base is undefined", () => { |
| 12 | const global: RequestOptions = { |
| 13 | timeout: 5000, |
| 14 | verifySsl: true, |
| 15 | headers: { Authorization: "Bearer token" }, |
| 16 | }; |
| 17 | const result = mergeConfigYamlRequestOptions(undefined, global); |
| 18 | expect(result).toEqual(global); |
| 19 | }); |
| 20 | |
| 21 | it("should return base when global is undefined", () => { |
| 22 | const base: RequestOptions = { |
| 23 | timeout: 3000, |
| 24 | verifySsl: false, |
| 25 | headers: { "X-Custom": "value" }, |
| 26 | }; |
| 27 | const result = mergeConfigYamlRequestOptions(base, undefined); |
| 28 | expect(result).toEqual(base); |
| 29 | }); |
| 30 | |
| 31 | it("should merge headers from both objects", () => { |
| 32 | const base: RequestOptions = { |
| 33 | headers: { "X-Base": "base-value", "X-Common": "base-common" }, |
| 34 | }; |
| 35 | const global: RequestOptions = { |
| 36 | headers: { "X-Global": "global-value", "X-Common": "global-common" }, |
| 37 | }; |
| 38 | const result = mergeConfigYamlRequestOptions(base, global); |
| 39 | expect(result?.headers).toEqual({ |
| 40 | "X-Global": "global-value", |
| 41 | "X-Common": "base-common", // base overrides global |
| 42 | "X-Base": "base-value", |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | it("should override simple values with base values", () => { |
| 47 | const base: RequestOptions = { |
| 48 | timeout: 3000, |
| 49 | verifySsl: false, |
| 50 | proxy: "http://base-proxy.com", |
| 51 | }; |
| 52 | const global: RequestOptions = { |
| 53 | timeout: 5000, |
| 54 | verifySsl: true, |
| 55 | proxy: "http://global-proxy.com", |
| 56 | caBundlePath: "/path/to/ca", |
| 57 | }; |
| 58 | const result = mergeConfigYamlRequestOptions(base, global); |
| 59 | expect(result).toEqual({ |
| 60 | timeout: 3000, // base overrides |
| 61 | verifySsl: false, // base overrides |
| 62 | proxy: "http://base-proxy.com", // base overrides |
| 63 | caBundlePath: "/path/to/ca", // only in global |
| 64 | headers: undefined, |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | it("should handle noProxy array properly (base overrides)", () => { |
| 69 | const base: RequestOptions = { |
| 70 | noProxy: ["localhost", "127.0.0.1"], |
| 71 | }; |
| 72 | const global: RequestOptions = { |
| 73 | noProxy: ["192.168.1.1", "example.com"], |
| 74 | }; |
| 75 | const result = mergeConfigYamlRequestOptions(base, global); |
| 76 | expect(result?.noProxy).toEqual(["localhost", "127.0.0.1"]); // base overrides completely |
| 77 | }); |
| 78 | |
| 79 | it("should handle extraBodyProperties (base overrides)", () => { |
| 80 | const base: RequestOptions = { |
| 81 | extraBodyProperties: { baseProp: "baseValue", common: "base" }, |
| 82 | }; |
| 83 | const global: RequestOptions = { |
| 84 | extraBodyProperties: { globalProp: "globalValue", common: "global" }, |
| 85 | }; |
| 86 | const result = mergeConfigYamlRequestOptions(base, global); |
| 87 | expect(result?.extraBodyProperties).toEqual({ |
| 88 | baseProp: "baseValue", |
| 89 | common: "base", |
| 90 | }); // base overrides completely |
| 91 | }); |
| 92 | |
| 93 | it("should handle clientCertificate (base overrides)", () => { |
| 94 | const base: RequestOptions = { |
| 95 | clientCertificate: { |
| 96 | cert: "base-cert", |
| 97 | key: "base-key", |
| 98 | passphrase: "base-pass", |
| 99 | }, |
| 100 | }; |
| 101 | const global: RequestOptions = { |
| 102 | clientCertificate: { |
| 103 | cert: "global-cert", |
| 104 | key: "global-key", |
| 105 | }, |
| 106 | }; |
| 107 | const result = mergeConfigYamlRequestOptions(base, global); |
| 108 | expect(result?.clientCertificate).toEqual({ |
| 109 | cert: "base-cert", |
| 110 | key: "base-key", |
| 111 | passphrase: "base-pass", |
| 112 | }); // base overrides completely |
| 113 | }); |
| 114 | |
| 115 | it("should return undefined headers when both have empty headers", () => { |
| 116 | const base: RequestOptions = { |
| 117 | headers: {}, |
| 118 | timeout: 1000, |
| 119 | }; |
| 120 | const global: RequestOptions = { |
| 121 | headers: {}, |
| 122 | verifySsl: true, |
| 123 | }; |
| 124 | const result = mergeConfigYamlRequestOptions(base, global); |
| 125 | expect(result?.headers).toBeUndefined(); |
| 126 | expect(result?.timeout).toBe(1000); |
| 127 | expect(result?.verifySsl).toBe(true); |
| 128 | }); |
| 129 | |
| 130 | it("should handle caBundlePath as string", () => { |
| 131 | const base: RequestOptions = { |
| 132 | caBundlePath: "/base/ca.pem", |
| 133 | }; |
| 134 | const global: RequestOptions = { |
| 135 | caBundlePath: "/global/ca.pem", |
| 136 | }; |
| 137 | const result = mergeConfigYamlRequestOptions(base, global); |
| 138 | expect(result?.caBundlePath).toBe("/base/ca.pem"); // base overrides |
| 139 | }); |
| 140 | |
| 141 | it("should handle caBundlePath as array", () => { |
| 142 | const base: RequestOptions = { |
| 143 | caBundlePath: ["/base/ca1.pem", "/base/ca2.pem"], |
| 144 | }; |
| 145 | const global: RequestOptions = { |
| 146 | caBundlePath: ["/global/ca.pem"], |
| 147 | }; |
| 148 | const result = mergeConfigYamlRequestOptions(base, global); |
| 149 | expect(result?.caBundlePath).toEqual(["/base/ca1.pem", "/base/ca2.pem"]); // base overrides |
| 150 | }); |
| 151 | |
| 152 | it("should preserve all fields from base when global has different fields", () => { |
| 153 | const base: RequestOptions = { |
| 154 | timeout: 3000, |
| 155 | headers: { "X-Base": "value" }, |
| 156 | noProxy: ["localhost"], |
| 157 | }; |
| 158 | const global: RequestOptions = { |
| 159 | verifySsl: true, |
| 160 | proxy: "http://proxy.com", |
| 161 | caBundlePath: "/ca.pem", |
| 162 | }; |
| 163 | const result = mergeConfigYamlRequestOptions(base, global); |
| 164 | expect(result).toEqual({ |
| 165 | timeout: 3000, |
| 166 | verifySsl: true, |
| 167 | proxy: "http://proxy.com", |
| 168 | caBundlePath: "/ca.pem", |
| 169 | headers: { "X-Base": "value" }, |
| 170 | noProxy: ["localhost"], |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | it("should handle complex header merging scenario", () => { |
| 175 | const base: RequestOptions = { |
| 176 | headers: { |
| 177 | Authorization: "Bearer base-token", |
| 178 | "X-Custom-Header": "base-custom", |
| 179 | "Content-Type": "application/json", |
| 180 | }, |
| 181 | }; |
| 182 | const global: RequestOptions = { |
| 183 | headers: { |
| 184 | Authorization: "Bearer global-token", |
| 185 | "X-Global-Header": "global-value", |
| 186 | "Accept-Language": "en-US", |
| 187 | }, |
| 188 | }; |
| 189 | const result = mergeConfigYamlRequestOptions(base, global); |
| 190 | expect(result?.headers).toEqual({ |
| 191 | Authorization: "Bearer base-token", // base overrides |
| 192 | "X-Global-Header": "global-value", // from global |
| 193 | "Accept-Language": "en-US", // from global |
| 194 | "X-Custom-Header": "base-custom", // from base |
| 195 | "Content-Type": "application/json", // from base |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | it("should handle all possible fields", () => { |
| 200 | const base: RequestOptions = { |
| 201 | timeout: 1000, |
| 202 | verifySsl: false, |
| 203 | caBundlePath: ["/base/ca.pem"], |
| 204 | proxy: "http://base-proxy.com", |
| 205 | headers: { "X-Base": "base" }, |
| 206 | extraBodyProperties: { baseProp: true }, |
| 207 | noProxy: ["base.local"], |
| 208 | clientCertificate: { cert: "base-cert", key: "base-key" }, |
| 209 | }; |
| 210 | const global: RequestOptions = { |
| 211 | timeout: 2000, |
| 212 | verifySsl: true, |
| 213 | caBundlePath: ["/global/ca.pem"], |
| 214 | proxy: "http://global-proxy.com", |
| 215 | headers: { "X-Global": "global" }, |
| 216 | extraBodyProperties: { globalProp: false }, |
| 217 | noProxy: ["global.local"], |
| 218 | clientCertificate: { cert: "global-cert", key: "global-key" }, |
| 219 | }; |
| 220 | const result = mergeConfigYamlRequestOptions(base, global); |
| 221 | expect(result).toEqual({ |
| 222 | timeout: 1000, // base overrides |
| 223 | verifySsl: false, // base overrides |
| 224 | caBundlePath: ["/base/ca.pem"], // base overrides |
| 225 | proxy: "http://base-proxy.com", // base overrides |
| 226 | headers: { |
| 227 | "X-Global": "global", |
| 228 | "X-Base": "base", |
| 229 | }, // merged |
| 230 | extraBodyProperties: { baseProp: true }, // base overrides |
| 231 | noProxy: ["base.local"], // base overrides |
| 232 | clientCertificate: { cert: "base-cert", key: "base-key" }, // base overrides |
| 233 | }); |
| 234 | }); |
| 235 | }); |