fix authorization headers generation
add unit tests
This commit is contained in:
@@ -197,38 +197,7 @@ function formatVariableValue(value) {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function mergeRequestConfig(base, updates) {
|
||||
if (!updates || typeof updates !== 'object') {
|
||||
return base;
|
||||
}
|
||||
|
||||
const merged = { ...(base || {}) };
|
||||
if (updates.auth) {
|
||||
if (updates.auth === 'inherit' && merged.auth && typeof merged.auth === 'object') {
|
||||
merged.auth = merged.auth;
|
||||
} else if (typeof updates.auth === 'object') {
|
||||
merged.auth = updates.auth;
|
||||
}
|
||||
}
|
||||
if (Array.isArray(updates.variables)) {
|
||||
const variables = [...(Array.isArray(base.variables) ? base.variables : [])];
|
||||
const byName = new Map();
|
||||
for (const variable of variables) {
|
||||
if (variable && variable.name) {
|
||||
byName.set(String(variable.name), variable);
|
||||
}
|
||||
}
|
||||
for (const variable of updates.variables) {
|
||||
if (variable && variable.name) {
|
||||
byName.set(String(variable.name), variable);
|
||||
}
|
||||
}
|
||||
merged.variables = [...byName.values()];
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
function getRequestConfigForFile(yamlFile, sourceDir) {
|
||||
export function getRequestConfigForFile(yamlFile, sourceDir) {
|
||||
const resolved = [];
|
||||
const seenFiles = new Set();
|
||||
|
||||
@@ -243,16 +212,35 @@ function getRequestConfigForFile(yamlFile, sourceDir) {
|
||||
|
||||
try {
|
||||
const parsed = parseYaml(filePath);
|
||||
let requestConfig = null;
|
||||
|
||||
if (parsed && parsed.request && typeof parsed.request === 'object') {
|
||||
resolved.push(parsed.request);
|
||||
requestConfig = parsed.request;
|
||||
} else if (parsed && typeof parsed === 'object') {
|
||||
const config = {};
|
||||
if (Object.prototype.hasOwnProperty.call(parsed, 'auth')) {
|
||||
config.auth = parsed.auth;
|
||||
} else if (parsed.http && typeof parsed.http === 'object' && Object.prototype.hasOwnProperty.call(parsed.http, 'auth')) {
|
||||
config.auth = parsed.http.auth;
|
||||
}
|
||||
if (Array.isArray(parsed.variables)) {
|
||||
config.variables = parsed.variables;
|
||||
}
|
||||
if (Object.keys(config).length > 0) {
|
||||
requestConfig = config;
|
||||
}
|
||||
}
|
||||
|
||||
if (requestConfig !== null) {
|
||||
resolved.push(requestConfig);
|
||||
} else if (path.resolve(filePath) === path.resolve(yamlFile)) {
|
||||
resolved.push({});
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore files that cannot be parsed as YAML for request inheritance.
|
||||
}
|
||||
};
|
||||
|
||||
addFile(yamlFile);
|
||||
|
||||
const dirChain = [];
|
||||
let currentDir = path.dirname(yamlFile);
|
||||
while (true) {
|
||||
@@ -272,9 +260,52 @@ function getRequestConfigForFile(yamlFile, sourceDir) {
|
||||
addFile(path.join(dir, 'folder.yml'));
|
||||
}
|
||||
|
||||
addFile(yamlFile);
|
||||
|
||||
return resolved.reduce((result, config) => mergeRequestConfig(result, config), {});
|
||||
}
|
||||
|
||||
export function mergeRequestConfig(base, updates) {
|
||||
if (!updates || typeof updates !== 'object') {
|
||||
return base;
|
||||
}
|
||||
|
||||
const merged = { ...(base || {}) };
|
||||
if (Object.prototype.hasOwnProperty.call(updates, 'auth')) {
|
||||
if (updates.auth === 'inherit') {
|
||||
if (merged.auth && typeof merged.auth === 'object') {
|
||||
merged.auth = merged.auth;
|
||||
} else {
|
||||
delete merged.auth;
|
||||
}
|
||||
} else if (typeof updates.auth === 'object' && updates.auth !== null) {
|
||||
merged.auth = updates.auth;
|
||||
} else {
|
||||
delete merged.auth;
|
||||
}
|
||||
} else {
|
||||
delete merged.auth;
|
||||
}
|
||||
|
||||
if (Array.isArray(updates.variables)) {
|
||||
const variables = [...(Array.isArray(base.variables) ? base.variables : [])];
|
||||
const byName = new Map();
|
||||
for (const variable of variables) {
|
||||
if (variable && variable.name) {
|
||||
byName.set(String(variable.name), variable);
|
||||
}
|
||||
}
|
||||
for (const variable of updates.variables) {
|
||||
if (variable && variable.name) {
|
||||
byName.set(String(variable.name), variable);
|
||||
}
|
||||
}
|
||||
merged.variables = [...byName.values()];
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
function buildRequestContent(request, requestName, requestConfig = {}, dotenvVariables = new Set()) {
|
||||
const lines = [];
|
||||
const variableDefinitions = [];
|
||||
|
||||
Reference in New Issue
Block a user