Skip to content

Commit 291b207

Browse files
committed
Add tests for requestOptions
1 parent f6c53ea commit 291b207

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export function buildRequestOptions(
4949
): http.RequestOptions {
5050
const clonedParams = { ...parameters };
5151
for (const k in clonedParams) {
52-
if (clonedParams[k] === undefined) {
52+
if (
53+
k === "requestOptions" ||
54+
k === "timeout" ||
55+
clonedParams[k] === undefined
56+
) {
5357
delete clonedParams[k];
5458
}
5559
}
@@ -61,8 +65,8 @@ export function buildRequestOptions(
6165

6266
return {
6367
...base,
64-
...(parameters.requestOptions as http.RequestOptions),
6568
...config.requestOptions,
69+
...(parameters.requestOptions as http.RequestOptions),
6670
};
6771
}
6872

tests/utils_test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import {
1818
getSource,
1919
} from "../src/utils.ts";
2020
import { RequestTimeoutError } from "../src/errors.ts";
21+
import { Config, config } from "../src/config.ts";
22+
import http from "node:http";
23+
import qs from "node:querystring";
2124

2225
loadSync({ export: true });
2326
const BASE_OPTIONS = Deno.env.get("ENV_TYPE") === "local"
@@ -101,6 +104,98 @@ describe("buildRequestOptions", () => {
101104
},
102105
);
103106
});
107+
108+
describe("with requestOptions", () => {
109+
let originalConfig: Config;
110+
111+
beforeAll(() => {
112+
originalConfig = { ...config };
113+
});
114+
115+
afterAll(() => {
116+
Object.assign(config, originalConfig);
117+
});
118+
119+
it("uses default options when no custom options provided", async () => {
120+
const options = await buildRequestOptions("/search", { q: "coffee" });
121+
assertEquals(options.method, "GET");
122+
assertEquals(options.path, "/search?q=coffee");
123+
});
124+
125+
it("uses custom request options from parameters", async () => {
126+
const customOptions: http.RequestOptions = {
127+
headers: {
128+
"User-Agent": "Custom User Agent",
129+
"X-Custom-Header": "param-value",
130+
},
131+
timeout: 5000,
132+
};
133+
134+
const params = {
135+
q: "coffee",
136+
requestOptions: customOptions,
137+
} as unknown as qs.ParsedUrlQueryInput;
138+
139+
const options = await buildRequestOptions("/search", params);
140+
141+
assertEquals(options.headers?.["User-Agent"], "Custom User Agent");
142+
assertEquals(options.headers?.["X-Custom-Header"], "param-value");
143+
assertEquals(options.timeout, 5000);
144+
assertEquals(options.path, "/search?q=coffee");
145+
});
146+
147+
it("uses request options from config when no options in parameters", async () => {
148+
const configOptions: http.RequestOptions = {
149+
headers: {
150+
"User-Agent": "Config User Agent",
151+
"X-Custom-Header": "config-value",
152+
},
153+
timeout: 5000,
154+
};
155+
156+
config.requestOptions = configOptions;
157+
158+
const options = await buildRequestOptions("/search", { q: "coffee" });
159+
160+
assertEquals(options.headers?.["User-Agent"], "Config User Agent");
161+
assertEquals(options.headers?.["X-Custom-Header"], "config-value");
162+
assertEquals(options.timeout, 5000);
163+
assertEquals(options.path, "/search?q=coffee");
164+
});
165+
166+
it("parameters requestOptions merges over config options", async () => {
167+
const configOptions: http.RequestOptions = {
168+
headers: {
169+
"User-Agent": "Config User Agent",
170+
"X-Custom-Header": "config-value",
171+
},
172+
timeout: 5000,
173+
};
174+
175+
const paramOptions: http.RequestOptions = {
176+
headers: {
177+
"User-Agent": "Parameter User Agent",
178+
"X-Custom-Header": "param-value",
179+
},
180+
hostname: "localhost",
181+
};
182+
183+
config.requestOptions = configOptions;
184+
185+
const params = {
186+
q: "coffee",
187+
requestOptions: paramOptions,
188+
} as unknown as qs.ParsedUrlQueryInput;
189+
190+
const options = await buildRequestOptions("/search", params);
191+
192+
assertEquals(options.headers?.["User-Agent"], "Parameter User Agent");
193+
assertEquals(options.headers?.["X-Custom-Header"], "param-value");
194+
assertEquals(options.hostname, "localhost");
195+
assertEquals(options.timeout, 5000);
196+
assertEquals(options.path, "/search?q=coffee");
197+
});
198+
});
104199
});
105200

106201
describe(

0 commit comments

Comments
 (0)