доработки в рамках трейдингового бота и поднятие версии до 1.4.0
This commit is contained in:
1 parent
41a0c8878c
commit
f3bfef9788
30 files changed
+2082
-69
No files matched your search
@@ -69,3 +69,155 @@ test('HttpClient: ходит только по http и https', function (): void
|
||||
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'] ?? '');
|
||||
});
|
||||
Reference in new issue
Block a user