Repository navigation
Expand file tree
/
Copy pathGuzzleClientTest.php
More file actions
278 lines (244 loc) · 11.8 KB
/
Copy pathGuzzleClientTest.php
File metadata and controls
278 lines (244 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
declare(strict_types=1);
namespace Docker\Tests;
use Docker\API\Exception\ContainerInspectNotFoundException;
use Docker\API\Model\ContainersCreatePostBody;
use Docker\Docker;
use Docker\DockerClientFactory;
use Docker\Stream\DockerRawStream;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\InvalidArgumentException as GuzzleInvalidArgumentException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\TransferStats;
use Http\Client\Common\PluginClient;
use Nyholm\Psr7\Request;
use PHPUnit\Framework\Attributes\DataProvider;
class GuzzleClientTest extends TransportTestCase
{
public static function connectionExamples(): array
{
return [
'Custom Unix socket' => ['Guzzle Unix socket', 'unix'],
'HTTP endpoint' => ['Guzzle Unix socket', 'http'],
'Private CA' => ['Guzzle HTTPS', 'https'],
'Mutual TLS' => ['Guzzle HTTPS', 'mutual-tls'],
];
}
#[DataProvider('connectionExamples')]
public function testDocumentationExampleUsesConfiguredConnection(string $example, string $transport): void
{
$serverOptions = ['responses' => [$this->jsonResponse('{}'), $this->jsonResponse('[]')]];
if ('https' === $transport || 'mutual-tls' === $transport) {
$this->createCertificates();
$serverOptions['ssl'] = $this->serverTlsOptions();
}
if ('mutual-tls' === $transport) {
$serverOptions['ssl'] += [
'verify_peer' => true,
'verify_peer_name' => false,
'cafile' => $this->directory.'/ca.pem',
'capture_peer_cert' => true,
];
}
$address = $this->startServer($serverOptions, 'unix' === $transport);
// A supplied client must not read the bundled factory's connection settings.
putenv('DOCKER_HOST=unix:///does-not-exist.sock');
putenv('DOCKER_TLS_VERIFY=1');
putenv('DOCKER_API_VERSION=v0.0');
$docker = $this->runDocumentationExample($example, $transport, $address);
$this->assertSame([], $docker->containerList());
$this->assertStringStartsWith('GET /v'.DockerClientFactory::defaultApiVersion().'/info HTTP/1.1', $this->serverResult()['request']);
$request = $this->serverResult(1);
$this->assertStringStartsWith('GET /v'.DockerClientFactory::defaultApiVersion().'/containers/json?', $request['request']);
$host = 'unix' === $transport ? 'localhost' : $address;
$this->assertStringContainsString('Host: '.$host."\r\n", $request['request']);
if ('mutual-tls' === $transport) {
$this->assertSame('docker-client', $request['peer']);
}
}
public function testHttpsRejectsUntrustedCertificate(): void
{
$this->createCertificates();
$address = $this->startServer(['ssl' => $this->serverTlsOptions()]);
$this->assertCertificateRejected('https://'.$address);
}
public function testHttpsRejectsWrongHostname(): void
{
$this->createCertificates('DNS:not-docker.test');
$address = $this->startServer(['ssl' => $this->serverTlsOptions()]);
$this->assertCertificateRejected('https://'.$address, ['verify' => $this->directory.'/ca.pem']);
}
public function testMutualTlsRejectsMissingClientCertificate(): void
{
$this->createCertificates();
$address = $this->startServer(['ssl' => $this->serverTlsOptions() + [
'verify_peer' => true,
'verify_peer_name' => false,
'cafile' => $this->directory.'/ca.pem',
]]);
$this->expectException(GuzzleException::class);
Docker::create($this->guzzle('https://'.$address, ['verify' => $this->directory.'/ca.pem']));
}
public function testRequestBodyReachesDaemon(): void
{
$address = $this->startServer(['responses' => [
$this->jsonResponse('{}'),
$this->jsonResponse('{"Id":"created","Warnings":[]}', 201),
]]);
$docker = Docker::create($this->guzzle('http://'.$address));
$body = new ContainersCreatePostBody();
$body->setImage('busybox:latest');
$body->setEnv(['MODE=test']);
$this->assertSame('created', $docker->containerCreate($body)->getId());
$request = $this->serverResult(1);
[$method, $uri] = explode(' ', $request['request'], 3);
$this->assertSame('POST', $method);
$this->assertSame('/v'.DockerClientFactory::defaultApiVersion().'/containers/create', parse_url($uri, \PHP_URL_PATH));
$json = json_decode($request['body'], true, 512, \JSON_THROW_ON_ERROR);
$this->assertSame('busybox:latest', $json['Image']);
$this->assertSame(['MODE=test'], $json['Env']);
}
public function testApiErrorUsesGeneratedException(): void
{
$address = $this->startServer(['responses' => [
$this->jsonResponse('{}'),
$this->jsonResponse('{"message":"No such container: missing"}', 404),
]]);
$docker = Docker::create($this->guzzle('http://'.$address));
$this->expectException(ContainerInspectNotFoundException::class);
$docker->containerInspect('missing');
}
public function testCompletedLogResponseDecodesStdoutAndStderr(): void
{
$frames = pack('CxxxN', 1, 6)."hello\n".pack('CxxxN', 2, 5)."warn\n";
$address = $this->startServer(['responses' => [
$this->jsonResponse('{}'),
['body_base64' => base64_encode($frames), 'headers' => ['Content-Type' => DockerRawStream::MULTIPLEXED_HEADER]],
]]);
$docker = Docker::create($this->guzzle('http://'.$address));
$logs = $docker->containerLogs('test', ['stdout' => true, 'stderr' => true]);
$stdout = '';
$stderr = '';
$logs->onStdout(static function (string $chunk) use (&$stdout): void {
$stdout .= $chunk;
});
$logs->onStderr(static function (string $chunk) use (&$stderr): void {
$stderr .= $chunk;
});
$logs->wait();
$this->assertSame("hello\n", $stdout);
$this->assertSame("warn\n", $stderr);
$this->assertStringContainsString('/v'.DockerClientFactory::defaultApiVersion().'/containers/test/logs?', $this->serverResult(1)['request']);
}
public function testExplicitCurlHandlerBuffersDespiteStreamOption(): void
{
$address = $this->startServer(['negotiation' => false]);
$client = new PluginClient($this->guzzle('http://'.$address, ['stream' => true]));
$response = $client->sendRequest(new Request('GET', '/_ping'));
$this->assertTrue($response->getBody()->isSeekable());
$this->assertSame('php://temp', $response->getBody()->getMetadata('uri'));
$this->assertSame('OK', $response->getBody()->getContents());
$this->assertStringStartsWith('GET /_ping HTTP/1.1', $this->serverResult()['request']);
}
public function testDefaultStreamingHandlerReadsIncrementally(): void
{
if (!\ini_get('allow_url_fopen')) {
$this->markTestSkipped('The default PHP stream handler requires allow_url_fopen.');
}
$address = $this->startServer(['negotiation' => false]);
$client = new PluginClient(new GuzzleClient([
'base_uri' => 'http://'.$address,
'stream' => true,
'proxy' => '',
'timeout' => 3,
]));
$response = $client->sendRequest(new Request('GET', '/_ping'));
$this->assertFalse($response->getBody()->isSeekable());
$this->assertSame('http', $response->getBody()->getMetadata('wrapper_type'));
$this->assertSame('OK', $response->getBody()->getContents());
$this->assertStringContainsString('Host: '.$address."\r\n", $this->serverResult()['request']);
}
public function testDefaultStreamingHandlerDoesNotUseCurlSocketOption(): void
{
if (!\ini_get('allow_url_fopen')) {
$this->markTestSkipped('The default PHP stream handler requires allow_url_fopen.');
}
$options = [
'stream' => true,
'curl' => [\CURLOPT_UNIX_SOCKET_PATH => $this->directory.'/does-not-exist.sock'],
'proxy' => '',
'timeout' => 3,
];
if (ClientInterface::MAJOR_VERSION >= 8) {
// Guzzle 8 rejects cURL-only options on the stream handler.
$client = new PluginClient(new GuzzleClient($options + ['base_uri' => 'http://localhost']));
$this->expectException(GuzzleInvalidArgumentException::class);
$client->sendRequest(new Request('GET', '/_ping'));
return;
}
// Guzzle 7 ignores them and connects to base_uri instead of the socket.
$address = $this->startServer(['negotiation' => false]);
$client = new PluginClient(new GuzzleClient($options + ['base_uri' => 'http://'.$address]));
$response = $client->sendRequest(new Request('GET', '/_ping'));
$this->assertSame('OK', $response->getBody()->getContents());
$this->assertStringContainsString('Host: '.$address."\r\n", $this->serverResult()['request']);
}
private function guzzle(string $url, array $options = []): GuzzleClient
{
return new GuzzleClient($options + [
'base_uri' => $url,
'handler' => HandlerStack::create(new CurlHandler()),
'proxy' => '',
'timeout' => 3,
]);
}
private function jsonResponse(string $body, int $status = 200): array
{
return ['status' => $status, 'body' => $body, 'headers' => ['Content-Type' => 'application/json']];
}
private function assertCertificateRejected(string $url, array $options = []): void
{
// Guzzle 8 removed the exception handler context; transfer stats report the cURL errno in both majors.
$errno = null;
$client = $this->guzzle($url, $options + [
'on_stats' => static function (TransferStats $stats) use (&$errno): void {
$errno = $stats->getHandlerErrorData();
},
]);
try {
Docker::create($client);
$this->fail('The server certificate should have been rejected.');
} catch (GuzzleException) {
$this->assertSame(\CURLE_SSL_CACERT, $errno);
}
}
private function runDocumentationExample(string $title, string $transport, string $address): Docker
{
$markdown = file_get_contents(\dirname(__DIR__).'/docs/guides/guzzle.mdx');
preg_match_all('/^```php ([^\r\n]+)\R(.*?)^```/ms', $markdown, $blocks);
$examples = array_combine($blocks[1], $blocks[2]);
$this->assertArrayHasKey($title, $examples);
$code = preg_replace('/^\s*<\?php\s*/', '', $examples[$title]);
$code = str_replace("require __DIR__ . '/vendor/autoload.php';", '', $code);
// Exercise the HTTP and CA-only variations described below the examples.
if ('http' === $transport) {
$code = preg_replace("/ 'curl' => \\[\\R.*? \\],\\R/s", '', $code);
$code = str_replace("'http://localhost'", "'http://docker.example.com:2375'", $code);
}
if ('https' === $transport) {
$code = preg_replace("/^ '(cert|ssl_key)' => [^\\r\\n]+\\R/m", '', $code);
}
$code = strtr($code, [
'/run/custom/docker.sock' => $address,
'http://docker.example.com:2375' => 'http://'.$address,
'https://docker.example.com:2376' => 'https://'.$address,
'/path/to/ca.pem' => $this->directory.'/ca.pem',
'/path/to/cert.pem' => $this->directory.'/cert.pem',
'/path/to/key.pem' => $this->directory.'/key.pem',
]);
return (static fn (string $code): Docker => eval("namespace {\n".$code."\nreturn \$docker;\n}"))($code);
}
}