333 lines
13 KiB
PHP
333 lines
13 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* HttpClient::fake() — подмена сети в тестах, реальная отправка проверена
|
|
* там, где HttpClient используется, — tests/MailTest.php (MailerServiceDriver).
|
|
*/
|
|
|
|
use Rsgrinko\Proton\Support\HttpClient;
|
|
|
|
test('HttpClient: fake() отвечает заготовкой и запоминает запрос', function (): void {
|
|
HttpClient::fake(['https://api.example.com/*' => ['status' => 201, 'body' => 'готово']]);
|
|
|
|
try {
|
|
$response = (new HttpClient())->post('https://api.example.com/orders', '{"id":1}', ['X-Test: да']);
|
|
|
|
assertSame(201, $response['status']);
|
|
assertSame('готово', $response['body']);
|
|
|
|
$recorded = HttpClient::recorded();
|
|
|
|
assertCount(1, $recorded);
|
|
assertSame('POST', $recorded[0]['method']);
|
|
assertSame('https://api.example.com/orders', $recorded[0]['url']);
|
|
assertSame('{"id":1}', $recorded[0]['body']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
test('HttpClient: адрес без своей маски отвечает пустым 200, а не падением', function (): void {
|
|
HttpClient::fake(['https://known.example.com/*' => ['status' => 500]]);
|
|
|
|
try {
|
|
$response = (new HttpClient())->get('https://unknown.example.com/');
|
|
|
|
assertSame(200, $response['status']);
|
|
assertSame('', $response['body']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
test('HttpClient: reset() выключает подмену и чистит журнал', function (): void {
|
|
HttpClient::fake();
|
|
(new HttpClient())->get('https://example.com');
|
|
|
|
assertCount(1, HttpClient::recorded());
|
|
|
|
HttpClient::reset();
|
|
|
|
assertSame([], HttpClient::recorded());
|
|
});
|
|
|
|
test('HttpClient: ходит только по http и https', function (): void {
|
|
// Даже под подменой: запрет не должен зависеть от того, тест это или бой
|
|
HttpClient::fake(['*' => ['status' => 200, 'body' => 'не должно дойти']]);
|
|
|
|
try {
|
|
foreach (['file://localhost/etc/passwd', 'gopher://127.0.0.1:6379/_x', 'ftp://example.com/a'] as $url) {
|
|
assertThrows(static fn () => (new HttpClient())->get($url), 'схема не должна пройти: ' . $url);
|
|
}
|
|
|
|
assertCount(0, HttpClient::recorded(), 'до сети ничего не ушло');
|
|
assertTrue(HttpClient::allowedScheme('https://example.com/hook'));
|
|
assertTrue(HttpClient::allowedScheme('http://10.0.0.5:8080/hook'), 'внутренние адреса по http разрешены');
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
test('HttpClient: список заготовок выдаётся по очереди, последний повторяется', function (): void {
|
|
HttpClient::fake(['https://api.example.com/*' => [['status' => 500], ['status' => 502], ['status' => 200, 'body' => 'ок']]]);
|
|
|
|
try {
|
|
$client = new HttpClient();
|
|
$statuses = [];
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
$statuses[] = $client->get('https://api.example.com/orders/1')['status'];
|
|
}
|
|
|
|
assertSame([500, 502, 200, 200], $statuses);
|
|
assertSame('ок', $client->get('https://api.example.com/orders/1')['body']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
test('HttpClient: заготовка-функция собирает ответ по запросу', function (): void {
|
|
HttpClient::fake([
|
|
'https://api.example.com/echo' => static fn (string $method, string $url, string $body): array => [
|
|
'status' => 201,
|
|
'body' => $method . ' ' . $body,
|
|
],
|
|
]);
|
|
|
|
try {
|
|
$response = (new HttpClient())->post('https://api.example.com/echo', 'привет');
|
|
|
|
assertSame(201, $response['status']);
|
|
assertSame('POST привет', $response['body']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
test('HttpClient: reset() начинает список заготовок сначала', function (): void {
|
|
$responses = ['*' => [['status' => 500], ['status' => 200]]];
|
|
|
|
HttpClient::fake($responses);
|
|
(new HttpClient())->get('https://example.com');
|
|
HttpClient::fake($responses);
|
|
|
|
try {
|
|
assertSame(500, (new HttpClient())->get('https://example.com')['status']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
test('HttpClient: прокси разбирается сразу, кривой адрес не принимается', function (): void {
|
|
$parts = HttpClient::parseProxy('socks5://bot%40home:p%3Ass@10.0.0.5:1080');
|
|
|
|
assertSame(['type' => 'socks5', 'host' => '10.0.0.5', 'port' => 1080, 'user' => 'bot@home', 'pass' => 'p:ss'], $parts);
|
|
assertSame('http://proxy.local:3128', (new HttpClient(5, ' http://proxy.local:3128 '))->proxy());
|
|
assertSame('', (new HttpClient())->proxy(), 'без прокси — напрямую');
|
|
|
|
foreach (['ftp://proxy:21', 'socks4://proxy:1080', 'http://proxy', 'proxy.local:3128'] as $proxy) {
|
|
assertThrows(static fn () => new HttpClient(5, $proxy), 'не должен пройти: ' . $proxy);
|
|
}
|
|
|
|
assertSame('socks5://bot:***@10.0.0.5:1080', HttpClient::maskProxy('socks5://bot:secret@10.0.0.5:1080'));
|
|
});
|
|
|
|
test('HttpClient: прокси попадает в записанный запрос', function (): void {
|
|
HttpClient::fake();
|
|
|
|
try {
|
|
(new HttpClient(5, 'socks5://10.0.0.5:1080'))->get('https://api.example.com/');
|
|
(new HttpClient())->get('https://api.example.com/');
|
|
|
|
assertSame('socks5://10.0.0.5:1080', HttpClient::recorded()[0]['proxy']);
|
|
assertSame('', HttpClient::recorded()[1]['proxy']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Запрос через игрушечный прокси (tests/support/fake_proxy_server.php) и что
|
|
* этот прокси увидел.
|
|
*
|
|
* @return array{response: array{status: int, body: string, duration: int}, report: array<string, mixed>}
|
|
*/
|
|
function requestThroughFakeProxy(string $mode, string $credentials, string $url): array
|
|
{
|
|
$token = bin2hex(random_bytes(4));
|
|
$portFile = APP_ROOT . '/var/tmp/proxy-test-port-' . $token . '.txt';
|
|
$logFile = APP_ROOT . '/var/tmp/proxy-test-log-' . $token . '.json';
|
|
|
|
$process = proc_open(
|
|
['php', APP_ROOT . '/tests/support/fake_proxy_server.php', $portFile, $logFile, $mode],
|
|
[1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
|
|
$pipes
|
|
);
|
|
|
|
try {
|
|
$deadline = microtime(true) + 10;
|
|
|
|
while (!is_file($portFile) && microtime(true) < $deadline) {
|
|
usleep(10000);
|
|
}
|
|
|
|
$port = (int) trim((string) @file_get_contents($portFile));
|
|
$proxy = ($mode === 'socks5' ? 'socks5' : 'http') . '://' . $credentials . '127.0.0.1:' . $port;
|
|
$response = (new HttpClient(10, $proxy))->get($url);
|
|
$report = json_decode((string) @file_get_contents($logFile), true);
|
|
|
|
return ['response' => $response, 'report' => is_array($report) ? $report : []];
|
|
} finally {
|
|
foreach ($pipes as $pipe) {
|
|
@fclose($pipe);
|
|
}
|
|
|
|
@proc_terminate($process);
|
|
@proc_close($process);
|
|
@unlink($portFile);
|
|
@unlink($logFile);
|
|
}
|
|
}
|
|
|
|
test('HttpClient: запрос уходит через http-прокси с логином', function (): void {
|
|
if (!function_exists('curl_init')) {
|
|
skipTest('нет curl');
|
|
}
|
|
|
|
// Хост выдуманный: дойти запрос может только через прокси
|
|
$result = requestThroughFakeProxy('http', 'bot:secret@', 'http://service.invalid/ping?x=1');
|
|
|
|
assertSame(200, $result['response']['status']);
|
|
assertSame('через прокси', $result['response']['body']);
|
|
assertSame('GET http://service.invalid/ping?x=1 HTTP/1.1', $result['report']['request_line'] ?? '');
|
|
assertSame('Basic ' . base64_encode('bot:secret'), $result['report']['proxy_authorization'] ?? '');
|
|
});
|
|
|
|
test('HttpClient: запрос уходит через socks5, имя хоста резолвит прокси', function (): void {
|
|
if (!function_exists('curl_init')) {
|
|
skipTest('нет curl');
|
|
}
|
|
|
|
$result = requestThroughFakeProxy('socks5', 'bot%40home:p%3Ass@', 'http://service.invalid:8080/ping');
|
|
|
|
assertSame(200, $result['response']['status']);
|
|
assertSame('через прокси', $result['response']['body']);
|
|
assertSame('bot@home', $result['report']['user'] ?? '');
|
|
assertSame('p:ss', $result['report']['pass'] ?? '');
|
|
assertSame(3, $result['report']['address_type'] ?? 0, 'адрес именем, а не IP: DNS на стороне прокси');
|
|
assertSame('service.invalid', $result['report']['target_host'] ?? '');
|
|
assertSame(8080, $result['report']['target_port'] ?? 0);
|
|
assertSame('GET /ping HTTP/1.1', $result['report']['request_line'] ?? '');
|
|
});
|
|
|
|
test('HttpClient: свой файл корневых сертификатов — нет файла, нет клиента', function (): void {
|
|
$ca = APP_ROOT . '/tests/support/tls/ca.pem';
|
|
|
|
assertSame($ca, (new HttpClient(5, '', $ca))->caFile());
|
|
|
|
withConfig(['http.ca_bundle' => ''], static function (): void {
|
|
assertSame('', (new HttpClient())->caFile(), 'без своего — общий набор, а его нет');
|
|
});
|
|
|
|
assertThrows(static fn () => new HttpClient(5, '', APP_ROOT . '/tests/support/tls/нет-такого.pem'));
|
|
|
|
HttpClient::fake();
|
|
|
|
try {
|
|
(new HttpClient(5, '', $ca))->get('https://bank.example.com/');
|
|
|
|
assertSame($ca, HttpClient::recorded()[0]['ca_bundle']);
|
|
} finally {
|
|
HttpClient::reset();
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Поднимает tests/support/fake_https_server.php и отдаёт его порт.
|
|
*
|
|
* @return array{process: resource, pipes: array<int, resource>, port: int, portFile: string}
|
|
*/
|
|
function startFakeHttps(int $connections): array
|
|
{
|
|
$portFile = APP_ROOT . '/var/tmp/https-test-port-' . bin2hex(random_bytes(4)) . '.txt';
|
|
|
|
$process = proc_open(
|
|
['php', APP_ROOT . '/tests/support/fake_https_server.php', $portFile, (string) $connections],
|
|
[1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
|
|
$pipes
|
|
);
|
|
|
|
$deadline = microtime(true) + 10;
|
|
|
|
while (!is_file($portFile) && microtime(true) < $deadline) {
|
|
usleep(10000);
|
|
}
|
|
|
|
return ['process' => $process, 'pipes' => $pipes, 'port' => (int) trim((string) @file_get_contents($portFile)), 'portFile' => $portFile];
|
|
}
|
|
|
|
/**
|
|
* @param array{process: resource, pipes: array<int, resource>, port: int, portFile: string} $server
|
|
*/
|
|
function stopFakeHttps(array $server): void
|
|
{
|
|
foreach ($server['pipes'] as $pipe) {
|
|
@fclose($pipe);
|
|
}
|
|
|
|
@proc_terminate($server['process']);
|
|
@proc_close($server['process']);
|
|
@unlink($server['portFile']);
|
|
}
|
|
|
|
test('HttpClient: сервер со своим УЦ проверяется по своему файлу, без него — отказ', function (): void {
|
|
if (!function_exists('curl_init')) {
|
|
skipTest('нет curl');
|
|
}
|
|
|
|
$server = startFakeHttps(2);
|
|
$url = 'https://127.0.0.1:' . $server['port'] . '/';
|
|
|
|
try {
|
|
// Проверку сертификата включаем явно: в .env разработки её бывает выключают
|
|
withConfig(['http.verify_peer' => true, 'http.ca_bundle' => ''], static function () use ($url): void {
|
|
$response = (new HttpClient(5, '', APP_ROOT . '/tests/support/tls/ca.pem'))->get($url);
|
|
|
|
assertSame(200, $response['status']);
|
|
assertSame('защищённо', $response['body']);
|
|
|
|
// Тот же сервер без своего файла: тестового УЦ нет ни в общем
|
|
// наборе, ни в системе — проверка сертификата не проходит
|
|
$error = assertThrows(static fn () => (new HttpClient(5))->get($url));
|
|
|
|
assertContains('Сервис недоступен', $error->getMessage());
|
|
});
|
|
} finally {
|
|
stopFakeHttps($server);
|
|
}
|
|
});
|
|
|
|
test('HttpClient: свой файл корневых сертификатов работает и без curl', function (): void {
|
|
$server = startFakeHttps(2);
|
|
$url = 'https://127.0.0.1:' . $server['port'] . '/';
|
|
|
|
// Ветка потоков — та, что работает без расширения curl
|
|
$viaStreams = static fn (HttpClient $client): array
|
|
=> (new ReflectionMethod($client, 'viaStreams'))->invoke($client, 'GET', $url, '', []);
|
|
|
|
try {
|
|
withConfig(['http.verify_peer' => true, 'http.ca_bundle' => ''], static function () use ($viaStreams): void {
|
|
$response = $viaStreams(new HttpClient(5, '', APP_ROOT . '/tests/support/tls/ca.pem'));
|
|
|
|
assertSame(200, $response['status']);
|
|
assertSame('защищённо', $response['body']);
|
|
|
|
assertThrows(static fn () => $viaStreams(new HttpClient(5)), 'без своего файла сервер не проверится');
|
|
});
|
|
} finally {
|
|
stopFakeHttps($server);
|
|
}
|
|
});
|