/
TraceableHttpClientTest.php
executable file
·99 lines (89 loc) · 3.53 KB
/
TraceableHttpClientTest.php
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
class TraceableHttpClientTest extends TestCase
{
public function testItTracesRequest()
{
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
$httpClient
->expects($this->once())
->method('request')
->with(
'GET',
'/foo/bar',
$this->callback(function ($subject) {
$onprogress = $subject['on_progress'];
unset($subject['on_progress']);
$this->assertEquals(['options1' => 'foo'], $subject);
return true;
})
)
->willReturn(MockResponse::fromRequest('GET', '/foo/bar', ['options1' => 'foo'], new MockResponse('hello')))
;
$sut = new TraceableHttpClient($httpClient);
$sut->request('GET', '/foo/bar', ['options1' => 'foo'])->getContent();
$this->assertCount(1, $tracedRequests = $sut->getTracedRequests());
$actualTracedRequest = $tracedRequests[0];
$this->assertEquals([
'method' => 'GET',
'url' => '/foo/bar',
'options' => ['options1' => 'foo'],
'info' => [],
'content' => 'hello',
], $actualTracedRequest);
}
public function testItCollectsInfoOnRealRequest()
{
$sut = new TraceableHttpClient(new MockHttpClient());
$sut->request('GET', 'http://localhost:8057');
$this->assertCount(1, $tracedRequests = $sut->getTracedRequests());
$actualTracedRequest = $tracedRequests[0];
$this->assertSame('GET', $actualTracedRequest['info']['http_method']);
$this->assertSame('http://localhost:8057/', $actualTracedRequest['info']['url']);
}
public function testItExecutesOnProgressOption()
{
$sut = new TraceableHttpClient(new MockHttpClient());
$foo = 0;
$sut->request('GET', 'http://localhost:8057', ['on_progress' => function (int $dlNow, int $dlSize, array $info) use (&$foo) {
++$foo;
}]);
$this->assertCount(1, $tracedRequests = $sut->getTracedRequests());
$actualTracedRequest = $tracedRequests[0];
$this->assertGreaterThan(0, $foo);
}
public function testItResetsTraces()
{
$sut = new TraceableHttpClient(new MockHttpClient());
$sut->request('GET', 'https://example.com/foo/bar');
$sut->reset();
$this->assertCount(0, $sut->getTracedRequests());
}
public function testStream()
{
TestHttpServer::start();
$sut = new TraceableHttpClient(new NativeHttpClient());
$chunked = $sut->request('GET', 'http://localhost:8057/chunked');
$chunks = [];
foreach ($sut->stream($chunked) as $response) {
$chunks[] = $response->getContent();
}
$this->assertGreaterThan(1, \count($chunks));
$this->assertSame('Symfony is awesome!', implode('', $chunks));
}
}