|
13 | 13 |
|
14 | 14 | use Psr\Log\AbstractLogger;
|
15 | 15 | use Symfony\Component\HttpClient\CurlHttpClient;
|
| 16 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 17 | +use Symfony\Component\Process\Process; |
16 | 18 | use Symfony\Contracts\HttpClient\HttpClientInterface;
|
17 | 19 |
|
18 | 20 | /**
|
| 21 | + * Some test needs HTTP2 Push capability and this feature yet is not available on ubuntu with ondrej PPA. |
| 22 | + * You can run theses tests with docker: |
| 23 | + * docker run -it --rm -v $(pwd):/app -v /usr/local/bin/vulcain:/usr/local/bin/vulcain -w /app php:7.3-alpine ./phpunit src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php --filter testHttp2Push |
19 | 24 | * @requires extension curl
|
20 | 25 | */
|
21 | 26 | class CurlHttpClientTest extends HttpClientTestCase
|
@@ -66,4 +71,86 @@ public function log($level, $message, array $context = []): void
|
66 | 71 | ];
|
67 | 72 | $this->assertSame($expected, $logger->logs);
|
68 | 73 | }
|
| 74 | + |
| 75 | + /** |
| 76 | + * @requires PHP 7.2.17 |
| 77 | + */ |
| 78 | + public function testHttp2PushVulcain() |
| 79 | + { |
| 80 | + if (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304) { |
| 81 | + $this->markTestSkipped('PHP 7.3.0 to 7.3.3 don\'t support HTTP/2 PUSH'); |
| 82 | + } |
| 83 | + |
| 84 | + if (!\defined('CURLMOPT_PUSHFUNCTION') || 0x073d00 > ($v = curl_version())['version_number'] || !(CURL_VERSION_HTTP2 & $v['features'])) { |
| 85 | + $this->markTestSkipped('curl <7.61 is used or it is not compiled with support for HTTP/2 PUSH'); |
| 86 | + } |
| 87 | + |
| 88 | + $this->startVulcain(); |
| 89 | + |
| 90 | + $logger = new class() extends AbstractLogger { |
| 91 | + public $logs = []; |
| 92 | + |
| 93 | + public function log($level, $message, array $context = []): void |
| 94 | + { |
| 95 | + $this->logs[] = $message; |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + $client = new CurlHttpClient(['verify_peer' => false, 'verify_host' => false]); |
| 100 | + $client->setLogger($logger); |
| 101 | + |
| 102 | + $responseAsArray = $client->request('GET', 'https://127.0.0.1:3000/json', [ |
| 103 | + 'headers' => [ |
| 104 | + 'Preload' => '/documents/*/id', |
| 105 | + ] |
| 106 | + ])->toArray(); |
| 107 | + foreach ($responseAsArray['documents'] as $document) { |
| 108 | + $docAsArray = $client->request('GET', 'https://127.0.0.1:3000' . $document['id'])->toArray(); |
| 109 | + } |
| 110 | + |
| 111 | + $expected = [ |
| 112 | + 'Request: "GET https://127.0.0.1:3000/json"', |
| 113 | + 'Queueing pushed response: "https://127.0.0.1:3000/json/1"', |
| 114 | + 'Queueing pushed response: "https://127.0.0.1:3000/json/2"', |
| 115 | + 'Queueing pushed response: "https://127.0.0.1:3000/json/3"', |
| 116 | + 'Response: "200 https://127.0.0.1:3000/json"', |
| 117 | + 'Unused pushed response: "https://127.0.0.1:3000/json/1"', |
| 118 | + 'Unused pushed response: "https://127.0.0.1:3000/json/2"', |
| 119 | + 'Unused pushed response: "https://127.0.0.1:3000/json/3"', |
| 120 | + 'Accepting pushed response: "GET https://127.0.0.1:3000/json/1"', |
| 121 | + 'Response: "200 https://127.0.0.1:3000/json/1"', |
| 122 | + 'Unused pushed response: "https://127.0.0.1:3000/json/2"', |
| 123 | + 'Unused pushed response: "https://127.0.0.1:3000/json/3"', |
| 124 | + 'Accepting pushed response: "GET https://127.0.0.1:3000/json/2"', |
| 125 | + 'Response: "200 https://127.0.0.1:3000/json/2"', |
| 126 | + 'Unused pushed response: "https://127.0.0.1:3000/json/3"', |
| 127 | + 'Accepting pushed response: "GET https://127.0.0.1:3000/json/3"', |
| 128 | + 'Response: "200 https://127.0.0.1:3000/json/3"', |
| 129 | + ]; |
| 130 | + $this->assertSame($expected, $logger->logs); |
| 131 | + } |
| 132 | + |
| 133 | + private function startVulcain() |
| 134 | + { |
| 135 | + $process = new Process(['vulcain'], null, [ |
| 136 | + 'DEBUG' => 1, |
| 137 | + 'UPSTREAM' => 'http://127.0.0.1:8057', |
| 138 | + 'ADDR' => ':3000', |
| 139 | + 'KEY_FILE' => 'src/Symfony/Contracts/HttpClient/Test/Fixtures/tls/server.key', |
| 140 | + 'CERT_FILE'=> 'src/Symfony/Contracts/HttpClient/Test/Fixtures/tls/server.crt', |
| 141 | + ]); |
| 142 | + $process->start(); |
| 143 | + |
| 144 | + register_shutdown_function(function() use ($process) { |
| 145 | + $process->stop(); |
| 146 | + |
| 147 | + echo $process->getOutput(); |
| 148 | + }); |
| 149 | + |
| 150 | + usleep(1000000); |
| 151 | + |
| 152 | + if (!$process->isRunning()) { |
| 153 | + throw new ProcessFailedException($process); |
| 154 | + } |
| 155 | + } |
69 | 156 | }
|
0 commit comments