igengx
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\assistantservice {
|
||||
// 只截取客户端给 cURL 的 URL 和请求头,全程不创建连接。
|
||||
function curl_init($url) { $GLOBALS['requests'][] = ['url' => $url]; return count($GLOBALS['requests']) - 1; }
|
||||
function curl_setopt_array($handle, $options) { $GLOBALS['requests'][$handle] += ['headers' => $options[CURLOPT_HTTPHEADER], 'options' => $options]; return true; }
|
||||
function curl_exec($handle) {
|
||||
$callback = $GLOBALS['requests'][$handle]['options'][CURLOPT_HEADERFUNCTION];
|
||||
foreach ($GLOBALS['response_headers'] as $header) { $callback($handle, $header . "\r\n"); }
|
||||
return '{}';
|
||||
}
|
||||
function curl_getinfo($handle, $option) { return $GLOBALS['upstream_status']; }
|
||||
function curl_close($handle) {}
|
||||
}
|
||||
|
||||
namespace {
|
||||
use app\common\service\assistantservice\WecomArchiveHttp as Feed;
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
$settings = ['wecom_archive_base_url' => 'https://archive.example.invalid', 'wecom_archive_token' => str_repeat('synthetic', 5)];
|
||||
function config($key, $default = '') { return $GLOBALS['settings'][substr($key, strlen('assistant_service.'))] ?? $default; }
|
||||
$requests = [];
|
||||
$upstream_status = 200;
|
||||
$response_headers = ['HTTP/1.1 100 Continue', 'X-Archive-Tenant: ignored', 'HTTP/1.1 200 OK', 'X-Archive-Tenant: tenant-a'];
|
||||
foreach (['', '*', "one\r\nX-Test: two"] as $tenant) {
|
||||
$settings['wecom_archive_tenant'] = $tenant;
|
||||
if (Feed::configured() || Feed::page('', 1)['configured']) { throw new \RuntimeException('Invalid tenant enabled HTTP'); }
|
||||
try { Feed::accounts(); throw new \LogicException('Expected invalid configuration'); }
|
||||
catch (\RuntimeException $e) { if ($e->getMessage() !== 'WECOM_ARCHIVE') { throw $e; } }
|
||||
}
|
||||
if ($requests) { throw new \RuntimeException('Request sent without a tenant'); }
|
||||
$settings['wecom_archive_tenant'] = 'tenant-a';
|
||||
foreach (['http://archive.example.invalid', 'file:///tmp/example', 'http://localhost@archive.example.invalid',
|
||||
'http://archive.example.invalid\@localhost', 'https://example.invalid/?q=conflict'] as $url) {
|
||||
$settings['wecom_archive_base_url'] = $url;
|
||||
if (Feed::configured()) { throw new \RuntimeException('Unsafe transport accepted'); }
|
||||
try { Feed::accounts(); throw new \LogicException('Expected transport refusal'); }
|
||||
catch (\RuntimeException $e) { if ($e->getMessage() !== 'WECOM_ARCHIVE') { throw $e; } }
|
||||
}
|
||||
foreach (['http://localhost:3210', 'http://[::1]:3210', 'http://127.0.0.1:3210'] as $url) {
|
||||
$settings['wecom_archive_base_url'] = $url;
|
||||
if (!Feed::configured()) { throw new \RuntimeException('Loopback HTTP rejected'); }
|
||||
}
|
||||
Feed::accounts();
|
||||
$settings['wecom_archive_base_url'] = 'https://archive.example.invalid';
|
||||
Feed::conversations('staff-a');
|
||||
Feed::messages('chat-a');
|
||||
$upstream_status = 403;
|
||||
try { Feed::page('', 1); throw new \LogicException('Expected upstream refusal'); }
|
||||
catch (\RuntimeException $e) { if ($e->getMessage() !== 'WECOM_ARCHIVE') { throw $e; } }
|
||||
if (count($requests) !== 4) { throw new \RuntimeException('Unexpected requests'); }
|
||||
foreach ($requests as $request) {
|
||||
if (!in_array('X-Archive-Tenant: tenant-a', $request['headers'], true)
|
||||
|| !in_array('X-Archive-Feed-Token: ' . $settings['wecom_archive_token'], $request['headers'], true)) {
|
||||
throw new \RuntimeException('Missing archive service headers');
|
||||
}
|
||||
}
|
||||
if (!array_key_exists(CURLOPT_PROXY, $requests[0]['options']) || $requests[0]['options'][CURLOPT_PROXY] !== ''
|
||||
|| $requests[0]['options'][CURLOPT_FOLLOWLOCATION] !== false) { throw new \RuntimeException('Loopback transport options'); }
|
||||
$upstream_status = 200;
|
||||
foreach ([[], ['X-Archive-Tenant: tenant-b'], ['X-Archive-Tenant: tenant-a', 'X-Archive-Tenant: tenant-a'],
|
||||
['X-Archive-Tenant: tenant-a', 'HTTP/1.1 200 OK'], ['', 'X-Archive-Tenant: tenant-a']] as $headers) {
|
||||
$response_headers = array_merge(['HTTP/1.1 200 OK'], $headers);
|
||||
try { Feed::accounts(); throw new \LogicException('Expected missing or conflicting response tenant refusal'); }
|
||||
catch (\RuntimeException $e) { if ($e->getMessage() !== 'WECOM_ARCHIVE') { throw $e; } }
|
||||
}
|
||||
echo "WECOM_ARCHIVE_HTTP PASS mocked_transport=1 network=0\n";
|
||||
}
|
||||
Reference in New Issue
Block a user