-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-worker.php
More file actions
230 lines (191 loc) · 5.64 KB
/
Copy pathtest-worker.php
File metadata and controls
230 lines (191 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Test script for WorkerClient integration.
*
* Usage: php test-worker.php <url-to-analyze>
* Example: php test-worker.php https://example.com
*
* @package CacheChecker
*/
namespace CacheChecker;
// Prevent web access.
if ( php_sapi_name() !== 'cli' ) {
die( 'This script can only be run from the command line.' );
}
// Check for URL argument.
if ( $argc < 2 ) {
echo "Usage: php test-worker.php <url>\n";
echo "Example: php test-worker.php https://example.com\n";
exit( 1 );
}
$url = $argv[1];
// Add https:// if protocol is omitted.
if ( ! preg_match( '/^https?:\/\//i', $url ) ) {
$url = 'https://' . $url;
}
// Load environment from .env file.
$env_file = __DIR__ . '/.env';
if ( file_exists( $env_file ) ) {
$lines = file( $env_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $lines as $line ) {
// Skip comments.
if ( strpos( trim( $line ), '#' ) === 0 ) {
continue;
}
// Parse KEY=value.
if ( strpos( $line, '=' ) !== false ) {
list( $key, $value ) = explode( '=', $line, 2 );
$_ENV[ trim( $key ) ] = trim( $value );
}
}
}
// Worker configuration from environment.
$worker_url = $_ENV['WORKER_URL'] ?? '';
$api_key = $_ENV['WORKER_API_KEY'] ?? '';
if ( empty( $worker_url ) ) {
echo "Error: WORKER_URL not set.\n";
echo "Create a .env file with WORKER_URL=https://cache-checker.your-subdomain.workers.dev\n";
exit( 1 );
}
// Simulate WordPress functions for standalone usage.
if ( ! function_exists( 'wp_remote_post' ) ) {
/**
* Make a POST request using cURL.
*
* @param string $url Request URL.
* @param array $args Request arguments.
*
* @return array|StandaloneError Response or error.
*/
function wp_remote_post( string $url, array $args = [] ) {
$ch = curl_init();
$headers = [];
foreach ( $args['headers'] ?? [] as $key => $value ) {
$headers[] = "{$key}: {$value}";
}
curl_setopt_array(
$ch,
[
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $args['body'] ?? '',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => $args['timeout'] ?? 60,
CURLOPT_SSL_VERIFYPEER => true,
]
);
$response = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
$error = new StandaloneError( 'http_request_failed', curl_error( $ch ) );
curl_close( $ch );
return $error;
}
$status_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
return [
'body' => $response,
'response' => [
'code' => $status_code,
],
];
}
}
if ( ! function_exists( 'wp_remote_retrieve_response_code' ) ) {
function wp_remote_retrieve_response_code( array $response ): int {
return $response['response']['code'] ?? 0;
}
}
if ( ! function_exists( 'wp_remote_retrieve_body' ) ) {
function wp_remote_retrieve_body( array $response ): string {
return $response['body'] ?? '';
}
}
if ( ! function_exists( 'is_wp_error' ) ) {
function is_wp_error( $thing ): bool {
return $thing instanceof StandaloneError;
}
}
if ( ! function_exists( 'wp_json_encode' ) ) {
function wp_json_encode( $data, int $options = 0 ) {
return json_encode( $data, $options );
}
}
if ( ! class_exists( 'WP_Error' ) ) {
class WP_Error {
private string $code;
private string $message;
public function __construct( string $code, string $message ) {
$this->code = $code;
$this->message = $message;
}
public function get_error_message(): string {
return $this->message;
}
}
}
// Load classes.
require_once __DIR__ . '/src/WorkerClient.php';
require_once __DIR__ . '/src/Report.php';
// Also need StandaloneError from standalone.php.
if ( ! class_exists( __NAMESPACE__ . '\\StandaloneError' ) ) {
class StandaloneError {
private string $code;
private string $message;
public function __construct( string $code, string $message ) {
$this->code = $code;
$this->message = $message;
}
public function get_error_message(): string {
return $this->message;
}
}
}
// Run test.
echo "Testing WorkerClient integration\n";
echo "================================\n\n";
echo "Worker URL: {$worker_url}\n";
echo "Target URL: {$url}\n";
echo "API Key: " . ( empty( $api_key ) ? '(none - dev mode)' : '(set)' ) . "\n\n";
$client = new WorkerClient( $worker_url, $api_key );
echo "Checking worker health... ";
$start = microtime( true );
// Simple health check via cURL.
$health_response = file_get_contents( $worker_url . '/health' );
if ( $health_response === false ) {
echo "FAILED\n";
echo "Could not connect to worker. Is it deployed?\n";
exit( 1 );
}
$health = json_decode( $health_response, true );
if ( ( $health['status'] ?? '' ) === 'ok' ) {
echo "OK\n\n";
} else {
echo "UNEXPECTED RESPONSE\n";
print_r( $health );
exit( 1 );
}
echo "Analyzing {$url}...\n\n";
$result = $client->analyze( $url, true, 20 );
$elapsed = round( microtime( true ) - $start, 2 );
if ( is_wp_error( $result ) || ( isset( $result->message ) && $result instanceof StandaloneError ) ) {
echo "ERROR: " . $result->get_error_message() . "\n";
exit( 1 );
}
if ( isset( $result['error'] ) ) {
echo "Worker Error: " . $result['error'] . "\n";
exit( 1 );
}
// Display report.
$report = new Report( $result );
echo $report->to_text() . "\n";
echo "\nCompleted in {$elapsed} seconds (via Cloudflare Worker).\n";
// Compare summary.
$summary = $result['summary'];
echo "\n--- Test Summary ---\n";
echo "Total assets checked: {$summary['total_assets']}\n";
echo "Total issues found: {$summary['total_issues']}\n";
echo " Errors: {$summary['issues_by_level']['error']}\n";
echo " Warnings: {$summary['issues_by_level']['warning']}\n";
echo " Info: {$summary['issues_by_level']['info']}\n";
exit( 0 );