/
CurlHttpClientTest.php
121 lines (97 loc) · 4.04 KB
/
CurlHttpClientTest.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?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 Psr\Log\AbstractLogger;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @requires extension curl
*/
class CurlHttpClientTest extends HttpClientTestCase
{
protected function getHttpClient(string $testCase): HttpClientInterface
{
return new CurlHttpClient();
}
/**
* @requires PHP 7.2.17
*/
public function testHttp2Push()
{
if (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304) {
$this->markTestSkipped('PHP 7.3.0 to 7.3.3 don\'t support HTTP/2 PUSH');
}
if (!\defined('CURLMOPT_PUSHFUNCTION') || 0x073d00 > ($v = curl_version())['version_number'] || !(CURL_VERSION_HTTP2 & $v['features'])) {
$this->markTestSkipped('curl <7.61 is used or it is not compiled with support for HTTP/2 PUSH');
}
$logger = new class() extends AbstractLogger {
public $logs = [];
public function log($level, $message, array $context = [])
{
$this->logs[] = $message;
}
};
$client = new CurlHttpClient();
$client->setLogger($logger);
$index = $client->request('GET', 'https://http2-push.io');
$index->getContent();
$css = $client->request('GET', 'https://http2-push.io/css/style.css');
$js = $client->request('GET', 'https://http2-push.io/js/http2-push.js');
$css->getHeaders();
$js->getHeaders();
$expected = [
'Request: "GET https://http2-push.io/"',
'Queueing pushed response: "https://http2-push.io/css/style.css"',
'Queueing pushed response: "https://http2-push.io/js/http2-push.js"',
'Response: "200 https://http2-push.io/"',
'Connecting request to pushed response: "GET https://http2-push.io/css/style.css"',
'Connecting request to pushed response: "GET https://http2-push.io/js/http2-push.js"',
'Response: "200 https://http2-push.io/css/style.css"',
'Response: "200 https://http2-push.io/js/http2-push.js"',
];
$this->assertSame($expected, $logger->logs);
}
public function testJsonRegexBuffer(): void
{
$client = new CurlHttpClient(['buffer' => '/([a-z]+\/json$)/']);
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
$firstContent = $response->getContent();
$secondContent = $response->getContent();
$this->assertSame($firstContent, $secondContent);
}
public function testPdfRegexBuffer(): void
{
$client = new CurlHttpClient(['buffer' => '/application\/pdf$/']);
$response = $client->request('GET', 'http://s2.q4cdn.com/235752014/files/doc_downloads/test.pdf');
$firstContent = $response->getContent();
$secondContent = $response->getContent();
$this->assertSame($firstContent, $secondContent);
}
/**
* @expectedException \Symfony\Component\HttpClient\Exception\TransportException
*/
public function testFalseBuffer(): void
{
$client = new CurlHttpClient(['buffer' => false]);
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
$firstPosts = $response->getContent();
$secondPosts = $response->getContent();
}
public function testTrueBuffer(): void
{
$client = new CurlHttpClient(['buffer' => true]);
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
$firstContent = $response->getContent();
$secondContent = $response->getContent();
$this->assertSame($firstContent, $secondContent);
}
}