1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http\Client; |
4
|
|
|
|
5
|
|
|
use Koded\Http\Client\{ClientFactory, CurlClient}; |
6
|
|
|
use Koded\Http\Interfaces\{ClientType, HttpMethod, HttpStatus}; |
7
|
|
|
use Koded\Http\ServerResponse; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Tests\Koded\Http\AssertionTestSupportTrait; |
10
|
|
|
|
11
|
|
|
class CurlClientTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
use ClientTestCaseTrait, AssertionTestSupportTrait; |
14
|
|
|
|
15
|
|
|
public function test_php_factory() |
16
|
|
|
{ |
17
|
|
|
$options = $this->getObjectProperty($this->SUT, 'options'); |
|
|
|
|
18
|
|
|
// keys |
19
|
|
|
$this->assertArrayNotHasKey(CURLOPT_HTTPHEADER, $options, 'The header is not built yet'); |
20
|
|
|
$this->assertArrayHasKey(CURLOPT_MAXREDIRS, $options); |
21
|
|
|
$this->assertArrayHasKey(CURLOPT_RETURNTRANSFER, $options); |
22
|
|
|
$this->assertArrayHasKey(CURLOPT_FOLLOWLOCATION, $options); |
23
|
|
|
$this->assertArrayHasKey(CURLOPT_SSL_VERIFYPEER, $options); |
24
|
|
|
$this->assertArrayHasKey(CURLOPT_SSL_VERIFYHOST, $options); |
25
|
|
|
$this->assertArrayHasKey(CURLOPT_USERAGENT, $options); |
26
|
|
|
$this->assertArrayHasKey(CURLOPT_FAILONERROR, $options); |
27
|
|
|
$this->assertArrayHasKey(CURLOPT_HTTP_VERSION, $options); |
28
|
|
|
$this->assertArrayHasKey(CURLOPT_TIMEOUT, $options); |
29
|
|
|
|
30
|
|
|
// values |
31
|
|
|
$this->assertSame(20, $options[CURLOPT_MAXREDIRS]); |
32
|
|
|
$this->assertSame(true, $options[CURLOPT_RETURNTRANSFER]); |
33
|
|
|
$this->assertSame(true, $options[CURLOPT_FOLLOWLOCATION]); |
34
|
|
|
$this->assertSame(1, $options[CURLOPT_SSL_VERIFYPEER]); |
35
|
|
|
$this->assertSame(2, $options[CURLOPT_SSL_VERIFYHOST]); |
36
|
|
|
$this->assertStringContainsString(' curl/' . curl_version()['version'], $options[CURLOPT_USERAGENT]); |
37
|
|
|
$this->assertSame(0, $options[CURLOPT_FAILONERROR]); |
38
|
|
|
$this->assertSame(CURL_HTTP_VERSION_1_1, $options[CURLOPT_HTTP_VERSION]); |
39
|
|
|
$this->assertSame(3.0, $options[CURLOPT_TIMEOUT]); |
40
|
|
|
$this->assertSame('', (string)$this->SUT->getBody(), 'The body is empty'); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function test_setting_the_client_with_methods() |
44
|
|
|
{ |
45
|
|
|
$this->SUT |
46
|
|
|
->ignoreErrors(true) |
47
|
|
|
->timeout(5) |
48
|
|
|
->followLocation(false) |
49
|
|
|
->returnTransfer(false) |
50
|
|
|
->maxRedirects(2) |
51
|
|
|
->userAgent('foo') |
52
|
|
|
->verifySslHost(false) |
53
|
|
|
->verifySslPeer(false); |
54
|
|
|
|
55
|
|
|
$options = $this->getObjectProperty($this->SUT, 'options'); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->assertSame('foo', $options[CURLOPT_USERAGENT]); |
58
|
|
|
$this->assertSame(5.0, $options[CURLOPT_TIMEOUT], 'Expects float (timeout)'); |
59
|
|
|
$this->assertSame(2, $options[CURLOPT_MAXREDIRS]); |
60
|
|
|
$this->assertSame(false, $options[CURLOPT_FOLLOWLOCATION]); |
61
|
|
|
$this->assertSame(false, $options[CURLOPT_RETURNTRANSFER]); |
62
|
|
|
$this->assertSame(0, $options[CURLOPT_FAILONERROR]); |
63
|
|
|
$this->assertSame(0, $options[CURLOPT_SSL_VERIFYHOST]); |
64
|
|
|
$this->assertSame(0, $options[CURLOPT_SSL_VERIFYPEER]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_protocol_version() |
68
|
|
|
{ |
69
|
|
|
$options = $this->getObjectProperty($this->SUT, 'options'); |
|
|
|
|
70
|
|
|
$this->assertSame(CURL_HTTP_VERSION_1_1, $options[CURLOPT_HTTP_VERSION]); |
71
|
|
|
|
72
|
|
|
$this->SUT = $this->SUT->withProtocolVersion('1.0'); |
73
|
|
|
$options = $this->getObjectProperty($this->SUT, 'options'); |
74
|
|
|
$this->assertSame(CURL_HTTP_VERSION_1_0, $options[CURLOPT_HTTP_VERSION]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test_when_curl_returns_error() |
78
|
|
|
{ |
79
|
|
|
$SUT = new class(HttpMethod::GET, 'http://example.com') extends CurlClient |
80
|
|
|
{ |
81
|
|
|
protected function hasError($resource): bool |
82
|
|
|
{ |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
}; |
86
|
|
|
$response = $SUT->read(); |
87
|
|
|
|
88
|
|
|
$this->assertInstanceOf(ServerResponse::class, $response); |
89
|
|
|
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json'); |
90
|
|
|
$this->assertSame(HttpStatus::FAILED_DEPENDENCY, $response->getStatusCode(), |
91
|
|
|
(string)$response->getBody()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function test_when_creating_resource_fails() |
95
|
|
|
{ |
96
|
|
|
$SUT = new class(HttpMethod::GET, 'http://example.com') extends CurlClient |
97
|
|
|
{ |
98
|
|
|
protected function createResource(): \CurlHandle|bool |
99
|
|
|
{ |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
}; |
103
|
|
|
$response = $SUT->read(); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf(ServerResponse::class, $response); |
106
|
|
|
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json'); |
107
|
|
|
$this->assertSame(HttpStatus::FAILED_DEPENDENCY, $response->getStatusCode()); |
108
|
|
|
$this->assertStringContainsString('The HTTP client is not created therefore cannot read anything', |
109
|
|
|
(string)$response->getBody()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function test_on_exception() |
113
|
|
|
{ |
114
|
|
|
$SUT = new class(HttpMethod::GET, 'http://example.com') extends CurlClient |
115
|
|
|
{ |
116
|
|
|
protected function createResource(): \CurlHandle|bool |
117
|
|
|
{ |
118
|
|
|
throw new \Exception('Exception message'); |
119
|
|
|
} |
120
|
|
|
}; |
121
|
|
|
$response = $SUT->read(); |
122
|
|
|
|
123
|
|
|
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json'); |
124
|
|
|
$this->assertSame(HttpStatus::INTERNAL_SERVER_ERROR, $response->getStatusCode()); |
125
|
|
|
$this->assertStringContainsString('Exception message', (string)$response->getBody()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @group internet |
130
|
|
|
*/ |
131
|
|
|
protected function setUp(): void |
132
|
|
|
{ |
133
|
|
|
if (false === extension_loaded('curl')) { |
134
|
|
|
$this->markTestSkipped('cURL extension is not installed on the testing environment'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->SUT = (new ClientFactory(ClientType::CURL)) |
138
|
|
|
->get('http://example.com') |
139
|
|
|
->timeout(3); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|