front/test-debug-interface.js
2026-03-23 10:00:44 +08:00

140 lines
4.4 KiB
JavaScript

// 测试selectedInterface对象的结构和处理逻辑
const VERBOSE_SCRIPT_LOGS = process.env.DEBUG_SCRIPT_LOGS === 'true';
if (!VERBOSE_SCRIPT_LOGS) {
console.log = () => {};
console.warn = () => {};
}
console.log('Testing selectedInterface processing...\n');
// 模拟从上游接口传递过来的selectedInterface对象
// 这个对象可能包含不同的字段结构
const mockSelectedInterface1 = {
id: 123,
name: '测试接口',
code: 'TEST001',
requestMethod: 'POST',
requestAddress: 'https://api.example.com/test',
contentType: 'application/json',
inParams: `{
"clientNo": "string",
"trackNo": "string",
"weight": 3
}`
};
const mockSelectedInterface2 = {
id: 456,
name: '测试接口2',
code: 'TEST002',
requestMethod: 1, // 数字形式
requestAddress: 'https://api.example.com/test2',
contentType: 'application/x-www-form-urlencoded',
inParams: `{
"userId": "123",
"action": "submit"
}`
};
const mockSelectedInterface3 = {
id: 789,
name: '测试接口3',
code: 'TEST003',
requestMethod: 'GET',
requestAddress: 'https://api.example.com/test3',
// 没有contentType
inParams: `{
"query": "test"
}`
};
// 测试处理函数
function testInterfaceProcessing(selectedInterface, testName) {
console.log(`=== Testing ${testName} ===`);
console.log('selectedInterface:', JSON.stringify(selectedInterface, null, 2));
if (!selectedInterface) {
console.log('No selectedInterface provided');
return;
}
// 1. 检查contentType
let bodyType = 'none';
if (selectedInterface.contentType) {
const contentTypeStr = String(selectedInterface.contentType).toLowerCase();
console.log('contentType string:', contentTypeStr);
if (contentTypeStr.includes('json')) {
bodyType = 'json';
} else if (contentTypeStr.includes('xml')) {
bodyType = 'xml';
} else if (contentTypeStr.includes('x-www-form-urlencoded')) {
bodyType = 'x-www-form-urlencoded';
} else if (contentTypeStr.includes('form-data') || contentTypeStr.includes('multipart')) {
bodyType = 'form-data';
} else {
bodyType = 'raw';
}
} else {
console.log('No contentType found, defaulting to json');
bodyType = 'json';
}
console.log('Determined bodyType:', bodyType);
// 2. 检查inParams
if (selectedInterface.inParams) {
console.log('Found inParams, length:', selectedInterface.inParams.length);
console.log('inParams preview:', selectedInterface.inParams.substring(0, 100));
// 根据bodyType处理数据
if (bodyType === 'json' || bodyType === 'xml') {
console.log('→ Would display in MonacoEditor');
// 这里应该调用processInParamsData
} else if (bodyType === 'x-www-form-urlencoded') {
console.log('→ Would populate urlEncoded table');
try {
const rawData = selectedInterface.inParams.replace(/\\r\\n/g, '\n').replace(/\\n/g, '\n').replace(/\\r/g, '\r').replace(/\\t/g, '\t').replace(/\\\\/g, '\\').replace(/\\"/g, '"');
const parsedData = JSON.parse(rawData);
console.log('Parsed data:', parsedData);
// 简化的扁平化逻辑
const flattened = [];
function flatten(obj, prefix = '') {
if (typeof obj === 'object' && obj !== null) {
Object.keys(obj).forEach(key => {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof obj[key] === 'object') {
flatten(obj[key], newKey);
} else {
flattened.push({ key: newKey, value: obj[key] });
}
});
} else {
flattened.push({ key: prefix, value: obj });
}
}
flatten(parsedData);
console.log('Flattened data:', flattened);
console.log('Would create', flattened.length, 'table rows');
} catch (error) {
console.error('Failed to process for table:', error.message);
}
} else if (bodyType === 'raw') {
console.log('→ Would display raw text in MonacoEditor');
}
} else {
console.log('No inParams found');
console.log('Available fields:', Object.keys(selectedInterface));
}
console.log();
}
// 运行测试
testInterfaceProcessing(mockSelectedInterface1, 'JSON Interface');
testInterfaceProcessing(mockSelectedInterface2, 'Form-UrlEncoded Interface');
testInterfaceProcessing(mockSelectedInterface3, 'No ContentType Interface');
console.log('✅ Interface processing test completed!');