Repository navigation
Expand file tree
/
Copy pathCURLRequestExceptionTest.php
More file actions
309 lines (250 loc) · 11.1 KB
/
Copy pathCURLRequestExceptionTest.php
File metadata and controls
309 lines (250 loc) · 11.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\Config\Factories;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockCURLRequest;
use Config\App;
use Config\CURLRequest as ConfigCURLRequest;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Throwable;
/**
* @internal
*/
#[Group('Others')]
final class CURLRequestExceptionTest extends CIUnitTestCase
{
#[DataProvider('provideFailureResetsSetterOptions')]
public function testFailureResetsSetterOptions(string $failure, string $setter): void
{
$this->configureSharing(false);
$app = new App();
$request = $failure === 'transport'
? $this->failingRequest()
: new MockCURLRequest($app, new URI(), new Response());
$request->setHeader('Authorization', 'Bearer secret');
match ($setter) {
'auth' => $request->setAuth('user', 'password'),
'body' => $request->setBody('private body'),
'form' => $request->setForm(['private' => 'data']),
'multipart' => $request->setForm(['private' => 'data'], true),
'json' => $request->setJSON(['private' => 'data']),
default => throw new InvalidArgumentException('Unknown setter: ' . $setter),
};
$options = match ($failure) {
'header' => ['headers' => ['Authorization' => 'Bearer secret', 'X-Invalid' => "invalid\0value"]],
'uri' => ['baseURI' => 'https://private.example:invalid/'],
'cert' => ['cert' => 'missing-certificate.pem'],
'verify' => ['verify' => 'missing-ca-bundle.pem'],
default => [],
};
$request->setOutput(match ($failure) {
'response header' => "HTTP/1.1 200 OK\r\nX-Invalid: invalid\0value\r\n\r\nprivate body",
'response status' => "HTTP/1.1 999 Invalid\r\n\r\nprivate body",
default => '',
});
$exception = null;
try {
$request->post('https://private.example/upload', $options);
} catch (Throwable $e) {
$exception = $e;
}
$expectedException = in_array($failure, ['header', 'response header'], true)
? InvalidArgumentException::class
: HTTPException::class;
$this->assertInstanceOf($expectedException, $exception);
$this->assertNull($request->header('Authorization'));
$this->assertNull($request->getBody());
$request->setOutput('');
$request->get('https://other.example/');
$options = $request->curl_options;
$this->assertSame('https://other.example/', $options[CURLOPT_URL]);
$this->assertArrayNotHasKey(CURLOPT_HTTPHEADER, $options);
$this->assertArrayNotHasKey(CURLOPT_POSTFIELDS, $options);
$this->assertArrayNotHasKey(CURLOPT_USERPWD, $options);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFailureResetsSetterOptions(): iterable
{
foreach (['header', 'uri', 'cert', 'verify', 'transport', 'response header', 'response status'] as $failure) {
foreach (['auth', 'body', 'form', 'multipart', 'json'] as $setter) {
yield $failure . ' with ' . $setter => [$failure, $setter];
}
}
}
#[DataProvider('provideRequestRestoresBaseURIAndDelay')]
public function testRequestRestoresBaseURIAndDelay(bool $fail, string $defaults): void
{
$this->configureSharing(false);
$baseURI = 'https://default.example/api/?default=value';
$options = $defaults === 'options' ? ['baseURI' => $baseURI, 'delay' => 100] : [];
$uri = $defaults === 'uri' ? new URI($baseURI) : new URI();
$app = new App();
$request = $fail
? $this->failingRequest($options, $uri)
: new MockCURLRequest($app, $uri, new Response(), $options);
try {
$request->get('reports', [
'baseURI' => 'https://alice@private.example:8443/v2/?api_key=secret#private',
'delay' => 200,
]);
$this->assertFalse($fail, 'Expected a transport exception.');
} catch (HTTPException $e) {
$this->assertTrue($fail);
$this->assertStringContainsString('simulated transport failure', $e->getMessage());
}
$expectedURI = $defaults === 'none' ? (string) new URI() : $baseURI;
$this->assertSame($expectedURI, (string) $request->getBaseURI());
$this->assertEqualsWithDelta($defaults === 'options' ? 0.1 : 0.0, $request->getDelay(), PHP_FLOAT_EPSILON);
// Mutating the restored URI must not mutate the saved constructor defaults.
$request->get('reports', ['baseURI' => 'https://bob@private.example:9443/v3/?token=secret']);
$this->assertSame($expectedURI, (string) $request->getBaseURI());
$request->get('', ['baseURI' => 'https://other.example/']);
$this->assertSame('https://other.example/', $request->curl_options[CURLOPT_URL]);
$this->assertSame($expectedURI, (string) $request->getBaseURI());
}
/**
* @return iterable<string, array{bool, string}>
*/
public static function provideRequestRestoresBaseURIAndDelay(): iterable
{
foreach ([false, true] as $fail) {
foreach (['none', 'uri', 'options'] as $defaults) {
yield ($fail ? 'failure' : 'success') . ' with ' . $defaults => [$fail, $defaults];
}
}
}
#[DataProvider('provideSharingPreservesBaseURIAndDelay')]
public function testSharingPreservesBaseURIAndDelay(bool $fail): void
{
$this->configureSharing(true);
$app = new App();
$request = $fail
? $this->failingRequest()
: new MockCURLRequest($app, new URI(), new Response());
$baseURI = 'https://alice@private.example:8443/v2/?api_key=secret';
try {
$request->get('reports', ['baseURI' => $baseURI, 'delay' => 200]);
$this->assertFalse($fail, 'Expected a transport exception.');
} catch (HTTPException $e) {
$this->assertTrue($fail);
$this->assertStringContainsString('simulated transport failure', $e->getMessage());
}
$this->assertSame($baseURI, (string) $request->getBaseURI());
$this->assertEqualsWithDelta(0.2, $request->getDelay(), PHP_FLOAT_EPSILON);
}
/**
* @return iterable<string, array{bool}>
*/
public static function provideSharingPreservesBaseURIAndDelay(): iterable
{
yield 'success' => [false];
yield 'failure' => [true];
}
public function testTransportFailureResetsRequestOptions(): void
{
$this->configureSharing(false);
$request = $this->failingRequest([
'headers' => ['X-Default' => 'configured'],
'user_agent' => 'DefaultAgent',
]);
try {
$request->post('https://private.example/upload', [
'headers' => ['Authorization' => 'Bearer secret'],
'body' => 'private body',
'auth' => ['user', 'password'],
'proxy' => 'http://proxy.example:3128',
]);
$this->fail('Expected a transport exception.');
} catch (HTTPException $e) {
$this->assertStringContainsString('simulated transport failure', $e->getMessage());
}
$this->assertNull($request->header('Authorization'));
$this->assertNull($request->getBody());
$request->get('https://other.example/');
$options = $request->curl_options;
$this->assertSame('https://other.example/', $options[CURLOPT_URL]);
$this->assertSame(['X-Default: configured'], $options[CURLOPT_HTTPHEADER]);
$this->assertSame('DefaultAgent', $options[CURLOPT_USERAGENT]);
$this->assertArrayNotHasKey(CURLOPT_POSTFIELDS, $options);
$this->assertArrayNotHasKey(CURLOPT_USERPWD, $options);
$this->assertArrayNotHasKey(CURLOPT_PROXY, $options);
}
public function testOptionValidationFailureResetsRequestOptions(): void
{
$this->configureSharing(false);
$app = new App();
$request = new MockCURLRequest($app, new URI(), new Response());
try {
$request->post('https://private.example/upload', [
'headers' => ['Authorization' => 'Bearer secret'],
'body' => 'private body',
'cert' => 'missing-certificate.pem',
]);
$this->fail('Expected an invalid certificate exception.');
} catch (HTTPException $e) {
$this->assertStringContainsString('missing-certificate.pem', $e->getMessage());
}
$this->assertNull($request->header('Authorization'));
$this->assertNull($request->getBody());
$request->get('https://other.example/');
$options = $request->curl_options;
$this->assertArrayNotHasKey(CURLOPT_HTTPHEADER, $options);
$this->assertArrayNotHasKey(CURLOPT_POSTFIELDS, $options);
}
public function testTransportFailureKeepsOptionsWhenSharingEnabled(): void
{
$this->configureSharing(true);
$request = $this->failingRequest();
try {
$request->post('https://private.example/upload', [
'headers' => ['Authorization' => 'Bearer secret'],
'body' => 'private body',
]);
$this->fail('Expected a transport exception.');
} catch (HTTPException $e) {
$this->assertStringContainsString('simulated transport failure', $e->getMessage());
}
$request->get('https://other.example/');
$options = $request->curl_options;
$this->assertSame(['Authorization: Bearer secret'], $options[CURLOPT_HTTPHEADER]);
$this->assertSame('private body', $options[CURLOPT_POSTFIELDS]);
}
/**
* @param array<string, mixed> $options
*/
private function failingRequest(array $options = [], ?URI $uri = null): MockCURLRequest
{
$app = new App();
return new class ($app, $uri ?? new URI(), new Response(), $options) extends MockCURLRequest {
private bool $failNextRequest = true;
protected function sendRequest(array $curlOptions = []): string
{
if ($this->failNextRequest) {
$this->failNextRequest = false;
throw HTTPException::forCurlError('7', 'simulated transport failure');
}
return parent::sendRequest($curlOptions);
}
};
}
private function configureSharing(bool $shareOptions): void
{
$config = new ConfigCURLRequest();
$config->shareOptions = $shareOptions;
Factories::injectMock('config', 'CURLRequest', $config);
}
}