Skip to content

Commit 6b45021

Browse files
committed
Disallow modifying basic request options
1 parent faca477 commit 6b45021

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ export function buildRequestOptions(
5757
delete clonedParams[k];
5858
}
5959
}
60-
const base = {
60+
const basicOptions = {
6161
..._internals.getHostnameAndPort(),
6262
path: `${path}?${qs.stringify(clonedParams)}`,
6363
method: "GET",
6464
};
6565

6666
return {
67-
...base,
6867
...config.requestOptions,
6968
...(parameters.requestOptions as http.RequestOptions),
69+
...basicOptions,
7070
};
7171
}
7272

tests/utils_test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe("buildRequestOptions", () => {
177177
"User-Agent": "Parameter User Agent",
178178
"X-Custom-Header": "param-value",
179179
},
180-
hostname: "localhost",
180+
agent: true,
181181
};
182182

183183
config.requestOptions = configOptions;
@@ -191,10 +191,25 @@ describe("buildRequestOptions", () => {
191191

192192
assertEquals(options.headers?.["User-Agent"], "Parameter User Agent");
193193
assertEquals(options.headers?.["X-Custom-Header"], "param-value");
194-
assertEquals(options.hostname, "localhost");
194+
assertEquals(options.agent, true);
195195
assertEquals(options.timeout, 5000);
196196
assertEquals(options.path, "/search?q=coffee");
197197
});
198+
199+
it("basic options are not allowed to be changed", async () => {
200+
config.requestOptions = {
201+
hostname: "localhost",
202+
port: 3000,
203+
path: "/test",
204+
method: "POST",
205+
};
206+
207+
const options = await buildRequestOptions("/search", { q: "coffee" });
208+
assertEquals(options.hostname, "serpapi.com");
209+
assertEquals(options.port, 443);
210+
assertEquals(options.path, "/search?q=coffee");
211+
assertEquals(options.method, "GET");
212+
});
198213
});
199214
});
200215

0 commit comments

Comments
 (0)