diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 212fdc9..3b937dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,3 +45,22 @@ jobs: - name: Vet working-directory: src-go run: go vet ./... + + integration: + name: Integration (real worker) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + extensions: mbstring, sockets + coverage: none + tools: composer:v2 + - run: composer install --prefer-dist --no-interaction --no-progress + # Downloads the Go worker on demand and exercises it against a TLS + # fingerprint reflector. Tests skip (not fail) if the worker or the + # reflector is unavailable; a reachable reflector reporting the wrong + # fingerprint fails the job. + - name: Integration tests + run: vendor/bin/phpunit tests/Integration diff --git a/.gitignore b/.gitignore index 25b6b42..3cfb618 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ exec/ vendor/ composer.lock .phpunit.cache/ +/src-go/worker diff --git a/README.md b/README.md index 26fabf5..20a5629 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # AstraHTTP PHP +[![CI](https://github.com/astrahttp/http/actions/workflows/ci.yml/badge.svg)](https://github.com/astrahttp/http/actions/workflows/ci.yml) +[![Latest Stable Version](https://img.shields.io/packagist/v/astrahttp/http.svg)](https://packagist.org/packages/astrahttp/http) +[![License](https://img.shields.io/packagist/l/astrahttp/http.svg)](LICENSE) + AstraHTTP PHP is a production-oriented wrapper around a Go-based transport engine. It is designed for high-concurrency HTTP traffic with shared worker management, TLS fingerprint customization, streaming response handling, multipart form encoding, retry support, and WebSocket / SSE protocol hooks. This document explains the package in detail: installation, architecture, API surface, request options, response methods, streaming, retries, WebSocket / SSE usage, multipart uploads, and practical examples. diff --git a/phpunit.xml b/phpunit.xml index bf6fcb5..bd64719 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,6 +9,7 @@ tests + tests/Integration diff --git a/tests/Integration/FingerprintTest.php b/tests/Integration/FingerprintTest.php new file mode 100644 index 0000000..55c9473 --- /dev/null +++ b/tests/Integration/FingerprintTest.php @@ -0,0 +1,143 @@ +getMessage()); + } + } + + private static function skip(string $reason): never + { + // Surface the reason in CI logs even without --display-skipped. + fwrite(STDERR, "[integration skipped] {$reason}\n"); + self::markTestSkipped($reason); + } + + /** + * Fetch the observed ja3_hash for a given JA3 input, trying each reflector + * until one responds with a usable value. Throws if none do. + */ + private function observedJa3Hash(Client $client, string $ja3): string + { + $errors = []; + + foreach (self::REFLECTORS as $url) { + try { + $response = $client->get($url, new RequestOptions([ + 'ja3' => $ja3, + 'disableGrease' => true, + 'responseType' => 'json', + 'timeout' => 30000, // milliseconds + 'userAgent' => 'AstraHTTP-IntegrationTest', + ]))->await(); + + if ($response->status !== 200) { + $errors[] = "{$url}: HTTP {$response->status}"; + continue; + } + + $data = $response->json(); + if (is_array($data) && isset($data['ja3_hash']) && is_string($data['ja3_hash']) && $data['ja3_hash'] !== '') { + return $data['ja3_hash']; + } + + $errors[] = "{$url}: response missing ja3_hash"; + } catch (\Throwable $e) { + $errors[] = "{$url}: " . $e->getMessage(); + } + } + + throw new \RuntimeException('no reflector usable (' . implode(' | ', $errors) . ')'); + } + + public function testWorkerPerformsHttpsRequestEndToEnd(): void + { + $client = $this->makeClientOrSkip(); + + $status = null; + $body = null; + $errors = []; + foreach (self::REFLECTORS as $url) { + try { + $response = $client->get($url, new RequestOptions([ + 'responseType' => 'json', + 'timeout' => 30000, // milliseconds + 'userAgent' => 'AstraHTTP-IntegrationTest', + ]))->await(); + $status = $response->status; + $body = $response->body; + break; + } catch (\Throwable $e) { + $errors[] = "{$url}: " . $e->getMessage(); + } + } + + if ($status === null) { + $client->close(); + self::skip('No reflector reachable: ' . implode(' | ', $errors)); + } + + $client->close(); + + // Proves the PHP -> Go worker -> network -> response path works. + self::assertSame(200, $status); + self::assertNotSame('', (string) $body); + } + + public function testDistinctJa3InputsProduceDistinctObservedFingerprints(): void + { + $client = $this->makeClientOrSkip(); + + try { + $hashA = $this->observedJa3Hash($client, self::JA3_A); + $hashB = $this->observedJa3Hash($client, self::JA3_B); + $hashA2 = $this->observedJa3Hash($client, self::JA3_A); + } catch (\Throwable $e) { + $client->close(); + self::skip('Reflector unreachable: ' . $e->getMessage()); + } + + $client->close(); + + // The worker actually applied our fingerprint: different JA3 in -> different + // fingerprint observed on the wire; the same JA3 in -> the same fingerprint. + self::assertNotSame($hashA, $hashB, 'Different JA3 inputs must yield different observed fingerprints'); + self::assertSame($hashA, $hashA2, 'The same JA3 input must yield the same observed fingerprint'); + } +}