fix: allow user to delete default bruno headers in pre-request (#7331)

* fix: allow user to delete default bruno headers

* fix: resolved comments

---------

Co-authored-by: shubh-bruno <shubh-bruno@shubh-bruno.local>
This commit is contained in:
shubh-bruno
2026-03-03 14:35:54 +05:30
committed by GitHub
parent bba0e97435
commit ca0412b58b
5 changed files with 219 additions and 29 deletions

View File

@@ -20,6 +20,7 @@ import https from 'node:https';
type ModifiedInternalAxiosRequestConfig = InternalAxiosRequestConfig & {
startTime: number;
__headersToDelete?: string[];
};
type ModifiedAxiosResponse = AxiosResponse & {
@@ -51,10 +52,28 @@ const makeAxiosInstance = (customRequestConfig?: AxiosRequestConfig) => {
customRequestConfig = customRequestConfig || {};
const axiosInstance = axios.create({
...baseRequestConfig,
...customRequestConfig
...customRequestConfig,
headers: {}
});
axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
// Apply header deletions requested via req.deleteHeader() in pre-request scripts.
const modConfig = config as ModifiedInternalAxiosRequestConfig;
const headersToDelete = modConfig.__headersToDelete;
if (headersToDelete && Array.isArray(headersToDelete)) {
headersToDelete.forEach((headerName: string) => {
const lower = headerName.toLowerCase();
if (lower === 'host' || lower === 'connection') return;
// Using set(name, null) rather than delete(): the axios http adapter guards its
// own defaults (User-Agent, Accept-Encoding) with set(..., false) which only
// skips writing when the key already exists. delete() removes the key entirely,
// so the guard misses and the adapter re-adds the default. null keeps the key
// present (blocking the guard) while toJSON() omits null values from the wire.
config.headers.set(headerName, null);
});
delete modConfig.__headersToDelete;
}
const modifiedConfig: ModifiedInternalAxiosRequestConfig = {
...config,
startTime: Date.now()