Skip to content

Commit d2fbd63

Browse files
[HttpClient] add MockHttpClient
1 parent 4221fd4 commit d2fbd63

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

Test/HttpClientTestCase.php

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public static function setUpBeforeClass()
3131
TestHttpServer::start();
3232
}
3333

34-
abstract protected function getHttpClient(): HttpClientInterface;
34+
abstract protected function getHttpClient(string $testCase): HttpClientInterface;
3535

3636
public function testGetRequest()
3737
{
38-
$client = $this->getHttpClient();
38+
$client = $this->getHttpClient(__FUNCTION__);
3939
$response = $client->request('GET', 'http://localhost:8057', [
4040
'headers' => ['Foo' => 'baR'],
4141
'user_data' => $data = new \stdClass(),
@@ -74,7 +74,7 @@ public function testGetRequest()
7474

7575
public function testNonBufferedGetRequest()
7676
{
77-
$client = $this->getHttpClient();
77+
$client = $this->getHttpClient(__FUNCTION__);
7878
$response = $client->request('GET', 'http://localhost:8057', [
7979
'buffer' => false,
8080
'headers' => ['Foo' => 'baR'],
@@ -89,7 +89,7 @@ public function testNonBufferedGetRequest()
8989

9090
public function testUnsupportedOption()
9191
{
92-
$client = $this->getHttpClient();
92+
$client = $this->getHttpClient(__FUNCTION__);
9393

9494
$this->expectException(\InvalidArgumentException::class);
9595
$client->request('GET', 'http://localhost:8057', [
@@ -99,7 +99,7 @@ public function testUnsupportedOption()
9999

100100
public function testHttpVersion()
101101
{
102-
$client = $this->getHttpClient();
102+
$client = $this->getHttpClient(__FUNCTION__);
103103
$response = $client->request('GET', 'http://localhost:8057', [
104104
'http_version' => 1.0,
105105
]);
@@ -116,7 +116,7 @@ public function testHttpVersion()
116116

117117
public function testChunkedEncoding()
118118
{
119-
$client = $this->getHttpClient();
119+
$client = $this->getHttpClient(__FUNCTION__);
120120
$response = $client->request('GET', 'http://localhost:8057/chunked');
121121

122122
$this->assertSame(['chunked'], $response->getHeaders()['transfer-encoding']);
@@ -130,7 +130,7 @@ public function testChunkedEncoding()
130130

131131
public function testClientError()
132132
{
133-
$client = $this->getHttpClient();
133+
$client = $this->getHttpClient(__FUNCTION__);
134134
$response = $client->request('GET', 'http://localhost:8057/404');
135135

136136
$client->stream($response)->valid();
@@ -156,15 +156,15 @@ public function testClientError()
156156

157157
public function testIgnoreErrors()
158158
{
159-
$client = $this->getHttpClient();
159+
$client = $this->getHttpClient(__FUNCTION__);
160160
$response = $client->request('GET', 'http://localhost:8057/404');
161161

162162
$this->assertSame(404, $response->getStatusCode());
163163
}
164164

165165
public function testDnsError()
166166
{
167-
$client = $this->getHttpClient();
167+
$client = $this->getHttpClient(__FUNCTION__);
168168
$response = $client->request('GET', 'http://localhost:8057/301/bad-tld');
169169

170170
try {
@@ -201,7 +201,7 @@ public function testDnsError()
201201

202202
public function testInlineAuth()
203203
{
204-
$client = $this->getHttpClient();
204+
$client = $this->getHttpClient(__FUNCTION__);
205205
$response = $client->request('GET', 'http://foo:bar%3Dbar@localhost:8057');
206206

207207
$body = $response->toArray();
@@ -210,9 +210,22 @@ public function testInlineAuth()
210210
$this->assertSame('bar=bar', $body['PHP_AUTH_PW']);
211211
}
212212

213+
public function testBadRequestBody()
214+
{
215+
$client = $this->getHttpClient(__FUNCTION__);
216+
217+
$this->expectException(TransportExceptionInterface::class);
218+
219+
$response = $client->request('POST', 'http://localhost:8057/', [
220+
'body' => function () { yield []; },
221+
]);
222+
223+
$response->getStatusCode();
224+
}
225+
213226
public function testRedirects()
214227
{
215-
$client = $this->getHttpClient();
228+
$client = $this->getHttpClient(__FUNCTION__);
216229
$response = $client->request('POST', 'http://localhost:8057/301', [
217230
'auth_basic' => 'foo:bar',
218231
'body' => function () {
@@ -248,7 +261,7 @@ public function testRedirects()
248261

249262
public function testRelativeRedirects()
250263
{
251-
$client = $this->getHttpClient();
264+
$client = $this->getHttpClient(__FUNCTION__);
252265
$response = $client->request('GET', 'http://localhost:8057/302/relative');
253266

254267
$body = $response->toArray();
@@ -266,7 +279,7 @@ public function testRelativeRedirects()
266279

267280
public function testRedirect307()
268281
{
269-
$client = $this->getHttpClient();
282+
$client = $this->getHttpClient(__FUNCTION__);
270283

271284
$response = $client->request('POST', 'http://localhost:8057/307', [
272285
'body' => function () {
@@ -288,7 +301,7 @@ public function testRedirect307()
288301

289302
public function testMaxRedirects()
290303
{
291-
$client = $this->getHttpClient();
304+
$client = $this->getHttpClient(__FUNCTION__);
292305
$response = $client->request('GET', 'http://localhost:8057/301', [
293306
'max_redirects' => 1,
294307
'auth_basic' => 'foo:bar',
@@ -322,7 +335,7 @@ public function testMaxRedirects()
322335

323336
public function testStream()
324337
{
325-
$client = $this->getHttpClient();
338+
$client = $this->getHttpClient(__FUNCTION__);
326339

327340
$response = $client->request('GET', 'http://localhost:8057');
328341
$chunks = $client->stream($response);
@@ -354,7 +367,7 @@ public function testStream()
354367

355368
public function testAddToStream()
356369
{
357-
$client = $this->getHttpClient();
370+
$client = $this->getHttpClient(__FUNCTION__);
358371

359372
$r1 = $client->request('GET', 'http://localhost:8057');
360373

@@ -385,15 +398,15 @@ public function testAddToStream()
385398

386399
public function testCompleteTypeError()
387400
{
388-
$client = $this->getHttpClient();
401+
$client = $this->getHttpClient(__FUNCTION__);
389402

390403
$this->expectException(\TypeError::class);
391404
$client->stream(123);
392405
}
393406

394407
public function testOnProgress()
395408
{
396-
$client = $this->getHttpClient();
409+
$client = $this->getHttpClient(__FUNCTION__);
397410
$response = $client->request('POST', 'http://localhost:8057/post', [
398411
'headers' => ['Content-Length' => 14],
399412
'body' => 'foo=0123456789',
@@ -411,7 +424,7 @@ public function testOnProgress()
411424

412425
public function testPostJson()
413426
{
414-
$client = $this->getHttpClient();
427+
$client = $this->getHttpClient(__FUNCTION__);
415428

416429
$response = $client->request('POST', 'http://localhost:8057/post', [
417430
'json' => ['foo' => 'bar'],
@@ -426,7 +439,7 @@ public function testPostJson()
426439

427440
public function testPostArray()
428441
{
429-
$client = $this->getHttpClient();
442+
$client = $this->getHttpClient(__FUNCTION__);
430443

431444
$response = $client->request('POST', 'http://localhost:8057/post', [
432445
'body' => ['foo' => 'bar'],
@@ -437,7 +450,7 @@ public function testPostArray()
437450

438451
public function testPostResource()
439452
{
440-
$client = $this->getHttpClient();
453+
$client = $this->getHttpClient(__FUNCTION__);
441454

442455
$h = fopen('php://temp', 'w+');
443456
fwrite($h, 'foo=0123456789');
@@ -454,7 +467,7 @@ public function testPostResource()
454467

455468
public function testPostCallback()
456469
{
457-
$client = $this->getHttpClient();
470+
$client = $this->getHttpClient(__FUNCTION__);
458471

459472
$response = $client->request('POST', 'http://localhost:8057/post', [
460473
'body' => function () {
@@ -470,7 +483,7 @@ public function testPostCallback()
470483

471484
public function testOnProgressCancel()
472485
{
473-
$client = $this->getHttpClient();
486+
$client = $this->getHttpClient(__FUNCTION__);
474487
$response = $client->request('GET', 'http://localhost:8057/timeout-body', [
475488
'on_progress' => function ($dlNow) {
476489
if (0 < $dlNow) {
@@ -494,7 +507,7 @@ public function testOnProgressCancel()
494507

495508
public function testOnProgressError()
496509
{
497-
$client = $this->getHttpClient();
510+
$client = $this->getHttpClient(__FUNCTION__);
498511
$response = $client->request('GET', 'http://localhost:8057/timeout-body', [
499512
'on_progress' => function ($dlNow) {
500513
if (0 < $dlNow) {
@@ -518,7 +531,7 @@ public function testOnProgressError()
518531

519532
public function testResolve()
520533
{
521-
$client = $this->getHttpClient();
534+
$client = $this->getHttpClient(__FUNCTION__);
522535
$response = $client->request('GET', 'http://symfony.com:8057/', [
523536
'resolve' => ['symfony.com' => '127.0.0.1'],
524537
]);
@@ -533,7 +546,7 @@ public function testResolve()
533546

534547
public function testTimeoutOnAccess()
535548
{
536-
$client = $this->getHttpClient();
549+
$client = $this->getHttpClient(__FUNCTION__);
537550
$response = $client->request('GET', 'http://localhost:8057/timeout-header', [
538551
'timeout' => 0.1,
539552
]);
@@ -545,7 +558,7 @@ public function testTimeoutOnAccess()
545558
public function testTimeoutOnStream()
546559
{
547560
usleep(300000); // wait for the previous test to release the server
548-
$client = $this->getHttpClient();
561+
$client = $this->getHttpClient(__FUNCTION__);
549562
$response = $client->request('GET', 'http://localhost:8057/timeout-body');
550563

551564
$this->assertSame(200, $response->getStatusCode());
@@ -577,7 +590,7 @@ public function testTimeoutOnStream()
577590

578591
public function testUncheckedTimeoutThrows()
579592
{
580-
$client = $this->getHttpClient();
593+
$client = $this->getHttpClient(__FUNCTION__);
581594
$response = $client->request('GET', 'http://localhost:8057/timeout-body');
582595
$chunks = $client->stream([$response], 0.1);
583596

@@ -589,7 +602,7 @@ public function testUncheckedTimeoutThrows()
589602

590603
public function testDestruct()
591604
{
592-
$client = $this->getHttpClient();
605+
$client = $this->getHttpClient(__FUNCTION__);
593606

594607
$downloaded = 0;
595608
$start = microtime(true);
@@ -603,7 +616,7 @@ public function testDestruct()
603616

604617
public function testProxy()
605618
{
606-
$client = $this->getHttpClient();
619+
$client = $this->getHttpClient(__FUNCTION__);
607620
$response = $client->request('GET', 'http://localhost:8057/', [
608621
'proxy' => 'http://localhost:8057',
609622
]);
@@ -625,7 +638,7 @@ public function testNoProxy()
625638
putenv('no_proxy='.$_SERVER['no_proxy'] = 'example.com, localhost');
626639

627640
try {
628-
$client = $this->getHttpClient();
641+
$client = $this->getHttpClient(__FUNCTION__);
629642
$response = $client->request('GET', 'http://localhost:8057/', [
630643
'proxy' => 'http://localhost:8057',
631644
]);
@@ -646,7 +659,7 @@ public function testNoProxy()
646659
*/
647660
public function testAutoEncodingRequest()
648661
{
649-
$client = $this->getHttpClient();
662+
$client = $this->getHttpClient(__FUNCTION__);
650663
$response = $client->request('GET', 'http://localhost:8057');
651664

652665
$this->assertSame(200, $response->getStatusCode());
@@ -663,7 +676,7 @@ public function testAutoEncodingRequest()
663676

664677
public function testBaseUri()
665678
{
666-
$client = $this->getHttpClient();
679+
$client = $this->getHttpClient(__FUNCTION__);
667680
$response = $client->request('GET', '../404', [
668681
'base_uri' => 'http://localhost:8057/abc/',
669682
]);
@@ -674,7 +687,7 @@ public function testBaseUri()
674687

675688
public function testQuery()
676689
{
677-
$client = $this->getHttpClient();
690+
$client = $this->getHttpClient(__FUNCTION__);
678691
$response = $client->request('GET', 'http://localhost:8057/?a=a', [
679692
'query' => ['b' => 'b'],
680693
]);
@@ -689,7 +702,7 @@ public function testQuery()
689702
*/
690703
public function testUserlandEncodingRequest()
691704
{
692-
$client = $this->getHttpClient();
705+
$client = $this->getHttpClient(__FUNCTION__);
693706
$response = $client->request('GET', 'http://localhost:8057', [
694707
'headers' => ['Accept-Encoding' => 'gzip'],
695708
]);
@@ -711,7 +724,7 @@ public function testUserlandEncodingRequest()
711724
*/
712725
public function testGzipBroken()
713726
{
714-
$client = $this->getHttpClient();
727+
$client = $this->getHttpClient(__FUNCTION__);
715728
$response = $client->request('GET', 'http://localhost:8057/gzip-broken');
716729

717730
$this->expectException(TransportExceptionInterface::class);

0 commit comments

Comments
 (0)