Import initial din SVN ROAAUTO/Trunk @HEAD
This commit is contained in:
263
COMUN/utile/chatbot/test_final_solution.html
Normal file
263
COMUN/utile/chatbot/test_final_solution.html
Normal file
@@ -0,0 +1,263 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Final Solution Test - URL Parameter Decoding</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 20px;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
.test-container {
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
.success { color: #28a745; font-weight: bold; }
|
||||
.error { color: #dc3545; font-weight: bold; }
|
||||
.warning { color: #ffc107; font-weight: bold; }
|
||||
.code {
|
||||
background: #f8f9fa;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
font-family: monospace;
|
||||
border-left: 4px solid #007bff;
|
||||
margin: 10px 0;
|
||||
word-break: break-all;
|
||||
}
|
||||
.result {
|
||||
background: #e9ecef;
|
||||
padding: 15px;
|
||||
border-radius: 4px;
|
||||
margin: 10px 0;
|
||||
word-break: break-word;
|
||||
}
|
||||
.test-case {
|
||||
margin: 15px 0;
|
||||
padding: 15px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Final Solution Test - URL Parameter Decoding</h1>
|
||||
<p>Testing the improved URL parameter decoding function that handles the %B issue gracefully.</p>
|
||||
|
||||
<div class="test-container">
|
||||
<h2>Test Cases</h2>
|
||||
<div id="testResults"></div>
|
||||
</div>
|
||||
|
||||
<div class="test-container">
|
||||
<h2>Summary</h2>
|
||||
<div id="summary"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Improved URL parameter function (same as updated in chatbot_maria.html)
|
||||
function getUrlParameter(name, searchString) {
|
||||
try {
|
||||
// Folosește URLSearchParams pentru o decodificare mai sigură
|
||||
const urlParams = new URLSearchParams(searchString);
|
||||
const value = urlParams.get(name);
|
||||
|
||||
if (value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
console.log("Parametru decodat cu URLSearchParams:", value);
|
||||
return value;
|
||||
} catch (error) {
|
||||
console.error("Eroare cu URLSearchParams:", error);
|
||||
|
||||
// Fallback la metoda manuală cu decodificare sigură
|
||||
try {
|
||||
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
||||
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
||||
var results = regex.exec(searchString);
|
||||
|
||||
if (results === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
let rawValue = results[1];
|
||||
console.log("Valoare brută din URL (fallback):", rawValue);
|
||||
|
||||
// Funcție de decodificare sigură care gestionează secvențe invalide
|
||||
function safeDecodeURIComponent(str) {
|
||||
// Primul pas: înlocuiește + cu spații
|
||||
str = str.replace(/\+/g, ' ');
|
||||
|
||||
// Al doilea pas: procesează secvențele % chunk cu chunk
|
||||
const chunks = str.split('%');
|
||||
let result = chunks[0]; // Primul chunk nu este niciodată codat
|
||||
|
||||
for (let i = 1; i < chunks.length; i++) {
|
||||
const chunk = chunks[i];
|
||||
if (chunk.length >= 2) {
|
||||
const hexCode = chunk.substring(0, 2);
|
||||
const rest = chunk.substring(2);
|
||||
|
||||
// Verifică dacă este un cod hex valid
|
||||
if (/^[0-9A-Fa-f]{2}$/.test(hexCode)) {
|
||||
try {
|
||||
result += decodeURIComponent('%' + hexCode) + rest;
|
||||
} catch (e) {
|
||||
// Dacă decodificarea eșuează, păstrează originalul
|
||||
result += '%' + chunk;
|
||||
}
|
||||
} else {
|
||||
// Cod hex invalid (cum ar fi %B), păstrează originalul
|
||||
result += '%' + chunk;
|
||||
}
|
||||
} else {
|
||||
// Cod incomplet, păstrează originalul
|
||||
result += '%' + chunk;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const decoded = safeDecodeURIComponent(rawValue);
|
||||
console.log("Parametru decodat cu metoda sigură:", decoded);
|
||||
return decoded;
|
||||
|
||||
} catch (fallbackError) {
|
||||
console.error("Eroare și la fallback sigur:", fallbackError);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test cases
|
||||
const testCases = [
|
||||
{
|
||||
name: "Original Problematic URL",
|
||||
search: "?message=F:%20Header%20(1)%20sectiune%20Company%20(1)%20sectiune%20BankAccount%20(1)%20eroare%20structura:%20grupul%20%27%BankAccount_choice0%27%20ar%20fi%20trebuit%20sa%20apara%20de%20minimum%201%20ori,%20dar%20apare%20efectiv%20de%200%20ori",
|
||||
expected: "F: Header (1) sectiune Company (1) sectiune BankAccount (1) eroare structura: grupul '%BankAccount_choice0' ar fi trebuit sa apara de minimum 1 ori, dar apare efectiv de 0 ori"
|
||||
},
|
||||
{
|
||||
name: "Normal URL encoding",
|
||||
search: "?message=Hello%20World%20%28test%29",
|
||||
expected: "Hello World (test)"
|
||||
},
|
||||
{
|
||||
name: "URL with plus signs",
|
||||
search: "?message=Hello+World+Test",
|
||||
expected: "Hello World Test"
|
||||
},
|
||||
{
|
||||
name: "URL with multiple invalid sequences",
|
||||
search: "?message=Test%B%G%20and%20%27normal%27",
|
||||
expected: "Test%B%G and 'normal'"
|
||||
},
|
||||
{
|
||||
name: "Empty message",
|
||||
search: "?message=",
|
||||
expected: ""
|
||||
},
|
||||
{
|
||||
name: "No message parameter",
|
||||
search: "?other=value",
|
||||
expected: ""
|
||||
}
|
||||
];
|
||||
|
||||
function runTests() {
|
||||
const resultsDiv = document.getElementById('testResults');
|
||||
const summaryDiv = document.getElementById('summary');
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
let results = [];
|
||||
|
||||
testCases.forEach((testCase, index) => {
|
||||
const testDiv = document.createElement('div');
|
||||
testDiv.className = 'test-case';
|
||||
|
||||
try {
|
||||
const result = getUrlParameter('message', testCase.search);
|
||||
const success = result === testCase.expected;
|
||||
|
||||
if (success) {
|
||||
passed++;
|
||||
} else {
|
||||
failed++;
|
||||
}
|
||||
|
||||
testDiv.innerHTML = `
|
||||
<h3>Test ${index + 1}: ${testCase.name}</h3>
|
||||
<div class="code"><strong>Input:</strong> ${testCase.search}</div>
|
||||
<div class="result"><strong>Expected:</strong><br>${testCase.expected}</div>
|
||||
<div class="result"><strong>Got:</strong><br>${result}</div>
|
||||
<div class="${success ? 'success' : 'error'}">
|
||||
${success ? '✅ PASSED' : '❌ FAILED'}
|
||||
</div>
|
||||
`;
|
||||
|
||||
results.push({
|
||||
name: testCase.name,
|
||||
success: success,
|
||||
expected: testCase.expected,
|
||||
got: result
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
failed++;
|
||||
testDiv.innerHTML = `
|
||||
<h3>Test ${index + 1}: ${testCase.name}</h3>
|
||||
<div class="code"><strong>Input:</strong> ${testCase.search}</div>
|
||||
<div class="error">❌ ERROR: ${error.message}</div>
|
||||
`;
|
||||
|
||||
results.push({
|
||||
name: testCase.name,
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
|
||||
resultsDiv.appendChild(testDiv);
|
||||
});
|
||||
|
||||
// Summary
|
||||
const totalTests = testCases.length;
|
||||
const successRate = Math.round((passed / totalTests) * 100);
|
||||
|
||||
summaryDiv.innerHTML = `
|
||||
<h3>Test Results Summary</h3>
|
||||
<p><strong>Total Tests:</strong> ${totalTests}</p>
|
||||
<p><strong>Passed:</strong> <span class="success">${passed}</span></p>
|
||||
<p><strong>Failed:</strong> <span class="error">${failed}</span></p>
|
||||
<p><strong>Success Rate:</strong> ${successRate}%</p>
|
||||
|
||||
<h3>Key Improvements:</h3>
|
||||
<ul>
|
||||
<li>✅ Handles invalid URL encoding sequences like %B gracefully</li>
|
||||
<li>✅ Preserves original text when decoding fails</li>
|
||||
<li>✅ Maintains compatibility with valid URL encoding</li>
|
||||
<li>✅ Provides robust fallback mechanism</li>
|
||||
<li>✅ No more "URI malformed" errors</li>
|
||||
</ul>
|
||||
|
||||
<h3>How it works:</h3>
|
||||
<ol>
|
||||
<li>First tries URLSearchParams (works for most cases)</li>
|
||||
<li>If URLSearchParams fails, uses safe chunk-by-chunk processing</li>
|
||||
<li>Only attempts to decode valid hex sequences (%XX format)</li>
|
||||
<li>Preserves invalid sequences like %B instead of corrupting them</li>
|
||||
<li>Handles incomplete sequences gracefully</li>
|
||||
</ol>
|
||||
`;
|
||||
}
|
||||
|
||||
// Run tests when page loads
|
||||
window.addEventListener('load', runTests);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user