Skip to content

Commit 14964f7

Browse files
authored
Merge pull request #305 from parthlambdatest/req_headers
Add request headers config
2 parents ad4f529 + 82d6377 commit 14964f7

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/lib/ctx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export default (options: Record<string, string>): Context => {
113113
ignoreHTTPSErrors: config.ignoreHTTPSErrors ?? false,
114114
skipBuildCreation: config.skipBuildCreation ?? false,
115115
tunnel: tunnelObj,
116-
userAgent: config.userAgent || ''
116+
userAgent: config.userAgent || '',
117+
requestHeaders: config.requestHeaders || {}
117118
},
118119
uploadFilePath: '',
119120
webStaticConfig: [],

src/lib/processSnapshot.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export async function prepareSnapshot(snapshot: Snapshot, ctx: Context): Promise
1919
if (ctx.config.basicAuthorization) {
2020
processedOptions.basicAuthorization = ctx.config.basicAuthorization;
2121
}
22+
if (ctx.config.requestHeaders && Array.isArray(ctx.config.requestHeaders)) {
23+
processedOptions.requestHeaders = ctx.config.requestHeaders
24+
}
2225
ctx.config.allowedHostnames.push(new URL(snapshot.url).hostname);
2326
processedOptions.allowedHostnames = ctx.config.allowedHostnames;
2427
processedOptions.skipCapturedCookies = ctx.env.SMARTUI_DO_NOT_USE_CAPTURED_COOKIES;
@@ -181,6 +184,9 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
181184
};
182185

183186
let processedOptions: Record<string, any> = {};
187+
if (ctx.config.requestHeaders && Array.isArray(ctx.config.requestHeaders)) {
188+
processedOptions.requestHeaders = ctx.config.requestHeaders
189+
}
184190

185191
let globalViewport = ""
186192
let globalBrowser = constants.CHROME
@@ -266,6 +272,13 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
266272
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
267273
requestOptions.headers.Authorization = `Basic ${token}`;
268274
}
275+
if (ctx.config.requestHeaders && Array.isArray(ctx.config.requestHeaders)) {
276+
ctx.config.requestHeaders.forEach((headerObj) => {
277+
Object.entries(headerObj).forEach(([key, value]) => {
278+
requestOptions.headers[key] = value;
279+
});
280+
});
281+
}
269282

270283
// get response
271284
let response, body;

src/lib/schemaValidation.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ const ConfigSchema = {
161161
errorMessage: {
162162
uniqueItems: "Invalid config; duplicates in allowedAssets"
163163
}
164-
165164
},
166165
basicAuthorization: {
167166
type: "object",
@@ -248,6 +247,18 @@ const ConfigSchema = {
248247
type: "string",
249248
errorMessage: "User Agent value must be a valid string"
250249
},
250+
requestHeaders: {
251+
type: "array",
252+
items: {
253+
type: "object",
254+
minProperties: 1,
255+
additionalProperties: { type: "string" }
256+
},
257+
uniqueItems: true,
258+
errorMessage: {
259+
uniqueItems: "Invalid config; duplicates in requestHeaders"
260+
}
261+
}
251262
},
252263
anyOf: [
253264
{ required: ["web"] },

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface Context {
3535
skipBuildCreation?: boolean;
3636
tunnel: tunnelConfig | undefined;
3737
userAgent?: string;
38+
requestHeaders?: Array<Record<string, string>>;
3839
};
3940
uploadFilePath: string;
4041
webStaticConfig: WebStaticConfig;

0 commit comments

Comments
 (0)