-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-quick.php
More file actions
112 lines (93 loc) · 3.39 KB
/
Copy pathtest-quick.php
File metadata and controls
112 lines (93 loc) · 3.39 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
<?php
// Simple autoloader for testing without composer
spl_autoload_register(function($class) {
$prefix = 'RpcPhpToolkit\\';
$baseDir = __DIR__ . '/src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relativeClass = substr($class, $len);
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require $file;
}
});
echo "=== RPC PHP Toolkit - Quick Test ===\n\n";
try {
// Test 1: Create endpoint
echo "1. Creating RpcEndpoint...\n";
$endpoint = new RpcPhpToolkit\RpcEndpoint('/api/rpc', ['test' => 'context']);
echo " ✓ RpcEndpoint created successfully\n\n";
// Test 2: Add method
echo "2. Adding test method...\n";
$endpoint->addMethod('test.add', function($params, $context) {
return $params['a'] + $params['b'];
});
echo " ✓ Method added successfully\n\n";
// Test 3: Handle request
echo "3. Testing RPC call...\n";
$request = json_encode([
'jsonrpc' => '2.0',
'method' => 'test.add',
'params' => ['a' => 5, 'b' => 3],
'id' => 1
]);
$response = $endpoint->handleRequest($request);
$decoded = json_decode($response, true);
if (isset($decoded['result']) && $decoded['result'] === 8) {
echo " ✓ RPC call successful: 5 + 3 = {$decoded['result']}\n\n";
} else {
echo " ✗ RPC call failed\n";
print_r($decoded);
}
// Test 4: Test client
echo "4. Testing RpcClient...\n";
$client = new RpcPhpToolkit\Client\RpcClient('http://example.com/rpc');
echo " ✓ RpcClient created successfully\n";
echo " URL: {$client->getUrl()}\n";
echo " Timeout: {$client->getTimeout()}s\n\n";
// Test 5: Test CORS middleware
echo "5. Testing CORS Middleware...\n";
$cors = new RpcPhpToolkit\Middleware\CorsMiddleware([
'origin' => '*',
'methods' => ['GET', 'POST', 'OPTIONS']
]);
echo " ✓ CORS Middleware created successfully\n";
print_r($cors->getOptions());
echo "\n";
// Test 6: Test Safe Mode
echo "6. Testing Safe Mode serialization...\n";
$safeEndpoint = new RpcPhpToolkit\RpcEndpoint('/api/rpc', null, [
'safeEnabled' => true,
'warnOnUnsafe' => false
]);
$safeEndpoint->addMethod('test.types', function($params) {
return [
'string' => 'hello',
'date' => new DateTime('2025-11-26T10:30:00Z'),
'number' => 42
];
});
$safeRequest = json_encode([
'jsonrpc' => '2.0',
'method' => 'test.types',
'params' => [],
'id' => 2
]);
$safeResponse = $safeEndpoint->handleRequest($safeRequest);
$safeDecoded = json_decode($safeResponse, true);
if (isset($safeDecoded['result'])) {
echo " ✓ Safe mode response:\n";
echo " - String: {$safeDecoded['result']['string']}\n";
echo " - Date: {$safeDecoded['result']['date']}\n";
echo " - Number: {$safeDecoded['result']['number']}\n\n";
}
echo "=== All Tests Passed! ✓ ===\n";
echo "\nThe library is working correctly!\n";
echo "You can now use: composer install && composer test\n";
} catch (Exception $e) {
echo "✗ Error: {$e->getMessage()}\n";
echo " File: {$e->getFile()}:{$e->getLine()}\n";
exit(1);
}