| 1 |
/* global metasyncHostBlockingData, jQuery */ |
| 2 |
/** |
| 3 |
* MetaSync Host Blocking Test |
| 4 |
* |
| 5 |
* Extracted for Phase 5, #887. |
| 6 |
* Handles GET/POST/BOTH host blocking tests on the settings page |
| 7 |
* and the dashboard "Test Both" variant. |
| 8 |
* |
| 9 |
* Localized object: metasyncHostBlockingData |
| 10 |
* - ajaxUrl (string) |
| 11 |
* |
| 12 |
* @since Phase 5 |
| 13 |
*/ |
| 14 |
(function () { |
| 15 |
// Wait for jQuery to be available |
| 16 |
function initHostBlockingTest() { |
| 17 |
if (typeof jQuery === 'undefined') { |
| 18 |
setTimeout(initHostBlockingTest, 100); |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
jQuery(document).ready(function ($) { |
| 23 |
// Ensure ajax URL is available in this scope |
| 24 |
var ajaxUrl = (typeof window.ajaxurl !== 'undefined' && window.ajaxurl) |
| 25 |
? window.ajaxurl |
| 26 |
: metasyncHostBlockingData.ajaxUrl; |
| 27 |
|
| 28 |
// --- Settings page buttons (GET / POST / BOTH) --- |
| 29 |
$(document).on('click', '#test-get-request', function (e) { |
| 30 |
e.preventDefault(); |
| 31 |
e.stopPropagation(); |
| 32 |
runHostTest('GET'); |
| 33 |
return false; |
| 34 |
}); |
| 35 |
|
| 36 |
$(document).on('click', '#test-post-request', function (e) { |
| 37 |
e.preventDefault(); |
| 38 |
e.stopPropagation(); |
| 39 |
runHostTest('POST'); |
| 40 |
return false; |
| 41 |
}); |
| 42 |
|
| 43 |
// --- Shared "Test Both" button (settings + dashboard) --- |
| 44 |
var $btn = $('#test-both-requests'); |
| 45 |
|
| 46 |
$btn.on('click', function (e) { |
| 47 |
e.preventDefault(); |
| 48 |
e.stopPropagation(); |
| 49 |
runHostTest('BOTH'); |
| 50 |
return false; |
| 51 |
}); |
| 52 |
|
| 53 |
$(document).on('click', '#test-both-requests', function (e) { |
| 54 |
e.preventDefault(); |
| 55 |
e.stopPropagation(); |
| 56 |
runHostTest('BOTH'); |
| 57 |
return false; |
| 58 |
}); |
| 59 |
|
| 60 |
// Expose function globally for debugging |
| 61 |
window.runHostBlockingTest = function () { |
| 62 |
runHostTest('BOTH'); |
| 63 |
}; |
| 64 |
|
| 65 |
function runHostTest(method) { |
| 66 |
var buttonId = (method === 'BOTH' ? 'test-both-requests' : 'test-' + method.toLowerCase() + '-request'); |
| 67 |
var $button = $('#' + buttonId); |
| 68 |
|
| 69 |
if ($button.length === 0) { |
| 70 |
alert('Error: Test button not found. Please refresh the page.'); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
var originalText = $button.text(); |
| 75 |
|
| 76 |
// Disable button and show loading |
| 77 |
$button.prop('disabled', true); |
| 78 |
$button.text('\uD83D\uDD04 Testing...'); |
| 79 |
|
| 80 |
// Prepare results area |
| 81 |
var $resultsDiv = $('#host-test-results'); |
| 82 |
var $resultsContent = $('#test-results-content'); |
| 83 |
$resultsDiv.show(); |
| 84 |
$resultsContent.html('<div class="notice notice-info"><p>Running ' + (method === 'BOTH' ? 'GET and POST' : method) + ' test(s)...</p></div>'); |
| 85 |
|
| 86 |
var testsToRun = (method === 'BOTH') ? ['GET', 'POST'] : [method]; |
| 87 |
var completedTests = 0; |
| 88 |
var allResults = []; |
| 89 |
|
| 90 |
testsToRun.forEach(function (testMethod) { |
| 91 |
var action = 'metasync_test_host_blocking_' + testMethod.toLowerCase(); |
| 92 |
|
| 93 |
$.ajax({ |
| 94 |
url: ajaxUrl, |
| 95 |
type: 'POST', |
| 96 |
dataType: 'json', |
| 97 |
data: { action: action }, |
| 98 |
timeout: 35000, |
| 99 |
success: function (response, textStatus, xhr) { |
| 100 |
try { |
| 101 |
if (response && response.success && response.data) { |
| 102 |
allResults.push(response.data); |
| 103 |
} else { |
| 104 |
allResults.push({ |
| 105 |
method: testMethod, |
| 106 |
status: 'error', |
| 107 |
error: (response && response.data) ? response.data : 'Unexpected response', |
| 108 |
blocked: true, |
| 109 |
details: 'Received non-success response from server.' |
| 110 |
}); |
| 111 |
} |
| 112 |
} catch (e) { |
| 113 |
allResults.push({ |
| 114 |
method: testMethod, |
| 115 |
status: 'error', |
| 116 |
error: 'Response parse error: ' + (e && e.message ? e.message : e), |
| 117 |
blocked: true |
| 118 |
}); |
| 119 |
} |
| 120 |
finalizeOne(); |
| 121 |
}, |
| 122 |
error: function (xhr, status, error) { |
| 123 |
var payload = (xhr && xhr.responseText) ? xhr.responseText.substring(0, 500) : ''; |
| 124 |
allResults.push({ |
| 125 |
method: testMethod, |
| 126 |
status: 'error', |
| 127 |
error: 'AJAX failed: ' + error + (payload ? ' \u2014 ' + payload : ''), |
| 128 |
blocked: true, |
| 129 |
details: 'Request did not complete successfully. Status: ' + status |
| 130 |
}); |
| 131 |
finalizeOne(); |
| 132 |
} |
| 133 |
}); |
| 134 |
}); |
| 135 |
|
| 136 |
function finalizeOne() { |
| 137 |
completedTests++; |
| 138 |
if (completedTests === testsToRun.length) { |
| 139 |
displayResults(allResults); |
| 140 |
resetButtons(); |
| 141 |
// Scroll results into view for clarity |
| 142 |
var $container = $('#host-test-results'); |
| 143 |
if ($container && $container[0] && $container[0].scrollIntoView) { |
| 144 |
$container[0].scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
function resetButtons() { |
| 151 |
$('#test-get-request, #test-post-request, #test-both-requests').prop('disabled', false); |
| 152 |
$('#test-get-request').text('\uD83D\uDD0D Test GET Request'); |
| 153 |
$('#test-post-request').text('\uD83D\uDCE4 Test POST Request'); |
| 154 |
$('#test-both-requests').text('\uD83D\uDD04 Test Both Requests'); |
| 155 |
} |
| 156 |
|
| 157 |
function displayResults(results) { |
| 158 |
var html = ''; |
| 159 |
|
| 160 |
results.forEach(function (result) { |
| 161 |
var statusClass = result.status === 'success' ? 'success' : 'error'; |
| 162 |
var statusIcon = result.status === 'success' ? '\u2705' : '\u274C'; |
| 163 |
var blockedStatus = result.blocked ? 'BLOCKED' : 'ALLOWED'; |
| 164 |
var blockedClass = result.blocked ? 'blocked' : 'allowed'; |
| 165 |
|
| 166 |
html += '<div class="test-result-item ' + statusClass + '">'; |
| 167 |
html += '<div class="test-result-header">'; |
| 168 |
html += '<h4>' + statusIcon + ' ' + result.method + ' Request - ' + blockedStatus + '</h4>'; |
| 169 |
html += '<span class="test-status ' + blockedClass + '">' + blockedStatus + '</span>'; |
| 170 |
html += '</div>'; |
| 171 |
|
| 172 |
html += '<div class="test-result-details">'; |
| 173 |
html += '<p><strong>Response Time:</strong> ' + result.response_time + '</p>'; |
| 174 |
html += '<p><strong>Status:</strong> ' + result.status + '</p>'; |
| 175 |
|
| 176 |
if (result.status_code) { |
| 177 |
html += '<p><strong>HTTP Status Code:</strong> <span class="status-code">' + result.status_code + '</span></p>'; |
| 178 |
} |
| 179 |
|
| 180 |
if (result.error) { |
| 181 |
html += '<p><strong>Error:</strong> <span class="error-message">' + result.error + '</span></p>'; |
| 182 |
} |
| 183 |
|
| 184 |
if (result.body) { |
| 185 |
html += '<p><strong>Response Body:</strong></p>'; |
| 186 |
html += '<pre class="response-body">' + escapeHtml(result.body) + '</pre>'; |
| 187 |
} |
| 188 |
|
| 189 |
if (result.headers && Object.keys(result.headers).length > 0) { |
| 190 |
html += '<p><strong>Response Headers:</strong></p>'; |
| 191 |
html += '<pre class="response-headers">'; |
| 192 |
Object.keys(result.headers).forEach(function (key) { |
| 193 |
html += key + ': ' + result.headers[key] + '\n'; |
| 194 |
}); |
| 195 |
html += '</pre>'; |
| 196 |
} |
| 197 |
|
| 198 |
if (result.sent_data) { |
| 199 |
html += '<p><strong>Sent Data:</strong></p>'; |
| 200 |
html += '<pre class="sent-data">' + escapeHtml(JSON.stringify(result.sent_data, null, 2)) + '</pre>'; |
| 201 |
} |
| 202 |
|
| 203 |
// Show parsed response data if available |
| 204 |
if (result.parsed_response) { |
| 205 |
html += '<p><strong>Parsed Response:</strong></p>'; |
| 206 |
html += '<pre class="parsed-response">' + escapeHtml(JSON.stringify(result.parsed_response, null, 2)) + '</pre>'; |
| 207 |
} |
| 208 |
|
| 209 |
html += '<p><strong>Details:</strong> ' + result.details + '</p>'; |
| 210 |
html += '</div>'; |
| 211 |
html += '</div>'; |
| 212 |
}); |
| 213 |
|
| 214 |
$('#test-results-content').html(html); |
| 215 |
$('#host-test-results').show(); |
| 216 |
} |
| 217 |
|
| 218 |
function escapeHtml(text) { |
| 219 |
var map = { |
| 220 |
'&': '&', |
| 221 |
'<': '<', |
| 222 |
'>': '>', |
| 223 |
'"': '"', |
| 224 |
"'": ''' |
| 225 |
}; |
| 226 |
return text.replace(/[&<>"']/g, function (m) { return map[m]; }); |
| 227 |
} |
| 228 |
|
| 229 |
// Verify button exists on page load |
| 230 |
setTimeout(function () { |
| 231 |
var btnBoth = $('#test-both-requests'); |
| 232 |
}, 500); |
| 233 |
}); |
| 234 |
} |
| 235 |
|
| 236 |
// Start initialization |
| 237 |
initHostBlockingTest(); |
| 238 |
})(); |
| 239 |
|