1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http\Client; |
4
|
|
|
|
5
|
|
|
use Koded\Http\Client\ClientFactory; |
6
|
|
|
use Koded\Http\Client\PhpClient; |
7
|
|
|
use Koded\Http\Interfaces\ClientType; |
8
|
|
|
use Koded\Http\Interfaces\HttpMethod; |
9
|
|
|
use Koded\Http\Interfaces\HttpStatus; |
10
|
|
|
use Koded\Http\ServerResponse; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Tests\Koded\Http\AssertionTestSupportTrait; |
13
|
|
|
|
14
|
|
|
class PhpClientTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
use ClientTestCaseTrait, AssertionTestSupportTrait; |
17
|
|
|
|
18
|
|
|
public function test_php_factory() |
19
|
|
|
{ |
20
|
|
|
$options = $this->getObjectProperty($this->SUT, 'options'); |
|
|
|
|
21
|
|
|
// keys |
22
|
|
|
$this->assertArrayNotHasKey('header', $options, 'Headers are not set up until read()'); |
23
|
|
|
$this->assertArrayHasKey('protocol_version', $options); |
24
|
|
|
$this->assertArrayHasKey('user_agent', $options); |
25
|
|
|
$this->assertArrayHasKey('method', $options); |
26
|
|
|
$this->assertArrayHasKey('timeout', $options); |
27
|
|
|
$this->assertArrayHasKey('max_redirects', $options); |
28
|
|
|
$this->assertArrayHasKey('follow_location', $options); |
29
|
|
|
$this->assertArrayHasKey('ignore_errors', $options); |
30
|
|
|
// "read_buffer_size" is not set at all by default |
31
|
|
|
$this->assertArrayNotHasKey('read_buffer_size', $options); |
32
|
|
|
|
33
|
|
|
// values |
34
|
|
|
$this->assertSame(1.1, $options['protocol_version']); |
35
|
|
|
$this->assertStringContainsString(' stream/' . PHP_VERSION, $options['user_agent']); |
36
|
|
|
$this->assertSame('GET', $options['method']); |
37
|
|
|
$this->assertSame(20, $options['max_redirects']); |
38
|
|
|
$this->assertSame(1, $options['follow_location']); |
39
|
|
|
$this->assertTrue($options['ignore_errors']); |
40
|
|
|
$this->assertFalse($options['ssl']['allow_self_signed']); |
41
|
|
|
$this->assertTrue($options['ssl']['verify_peer']); |
42
|
|
|
$this->assertSame(5.0, $options['timeout']); |
43
|
|
|
$this->assertSame('', (string)$this->SUT->getBody(), 'The body is empty'); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_setting_the_client_with_methods() |
47
|
|
|
{ |
48
|
|
|
$this->SUT |
49
|
|
|
->ignoreErrors(true) |
50
|
|
|
->timeout(5) |
51
|
|
|
->followLocation(false) |
52
|
|
|
->returnTransfer(false) |
53
|
|
|
->maxRedirects(2) |
54
|
|
|
->userAgent('foo') |
55
|
|
|
->verifySslPeer(false) |
56
|
|
|
->verifySslHost(true); |
57
|
|
|
|
58
|
|
|
$options = $this->getObjectProperty($this->SUT, 'options'); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->assertSame('foo', $options['user_agent']); |
61
|
|
|
$this->assertSame(5.0, $options['timeout']); |
62
|
|
|
$this->assertSame(2, $options['max_redirects']); |
63
|
|
|
$this->assertSame(0, $options['follow_location']); |
64
|
|
|
$this->assertSame(0, $options['read_buffer_size']); |
65
|
|
|
$this->assertSame(true, $options['ignore_errors']); |
66
|
|
|
$this->assertSame(true, $options['ssl']['allow_self_signed']); |
67
|
|
|
$this->assertSame(false, $options['ssl']['verify_peer']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function test_when_curl_returns_error() |
71
|
|
|
{ |
72
|
|
|
$SUT = new class(HttpMethod::GET, 'http://example.com') extends PhpClient |
73
|
|
|
{ |
74
|
|
|
protected function hasError($resource): bool |
75
|
|
|
{ |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
}; |
79
|
|
|
$response = $SUT->read(); |
80
|
|
|
|
81
|
|
|
$this->assertInstanceOf(ServerResponse::class, $response); |
82
|
|
|
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json'); |
83
|
|
|
$this->assertSame(HttpStatus::FAILED_DEPENDENCY, $response->getStatusCode(), |
84
|
|
|
(string)$response->getBody()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function test_when_creating_resource_fails() |
88
|
|
|
{ |
89
|
|
|
$SUT = new class(HttpMethod::GET, 'http://example.com') extends PhpClient |
90
|
|
|
{ |
91
|
|
|
protected function createResource($resource) |
92
|
|
|
{ |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
}; |
96
|
|
|
$response = $SUT->read(); |
97
|
|
|
|
98
|
|
|
$this->assertInstanceOf(ServerResponse::class, $response); |
99
|
|
|
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json'); |
100
|
|
|
$this->assertSame(HttpStatus::FAILED_DEPENDENCY, $response->getStatusCode()); |
101
|
|
|
$this->assertStringContainsString('The HTTP client is not created therefore cannot read anything', |
102
|
|
|
(string)$response->getBody()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function test_on_exception() |
106
|
|
|
{ |
107
|
|
|
$SUT = new class(HttpMethod::GET, 'http://example.com') extends PhpClient |
108
|
|
|
{ |
109
|
|
|
protected function createResource($resource) |
110
|
|
|
{ |
111
|
|
|
throw new \Exception('Exception message'); |
112
|
|
|
} |
113
|
|
|
}; |
114
|
|
|
$response = $SUT->read(); |
115
|
|
|
|
116
|
|
|
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json'); |
117
|
|
|
$this->assertSame(HttpStatus::INTERNAL_SERVER_ERROR, $response->getStatusCode()); |
118
|
|
|
$this->assertStringContainsString('Exception message', (string)$response->getBody()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function setUp(): void |
122
|
|
|
{ |
123
|
|
|
$this->SUT = (new ClientFactory(ClientType::PHP)) |
124
|
|
|
->get('http://example.com') |
125
|
|
|
->timeout(5); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|