-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathSocketHttpClientTest.php
More file actions
171 lines (142 loc) · 5.2 KB
/
SocketHttpClientTest.php
File metadata and controls
171 lines (142 loc) · 5.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Http\Client\Socket\Tests;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Socket\Client as SocketHttpClient;
use Http\Client\Socket\Exception\NetworkException;
use Http\Client\Socket\Exception\TimeoutException;
use Nyholm\Psr7\Factory\Psr17Factory;
class SocketHttpClientTest extends BaseTestCase
{
public function createClient($options = []): HttpMethodsClient
{
return new HttpMethodsClient(new SocketHttpClient($options), new Psr17Factory());
}
public function testTcpSocketDomain(): void
{
$this->startServer('tcp-server');
$client = $this->createClient(['remote_socket' => '127.0.0.1:19999']);
$response = $client->get('/', []);
$this->assertSame(200, $response->getStatusCode());
}
public function testNoRemote(): void
{
$client = $this->createClient();
$this->expectException(NetworkException::class);
$client->get('/', []);
}
public function testRemoteInUri(): void
{
$this->startServer('tcp-server');
$client = $this->createClient();
$response = $client->get('http://127.0.0.1:19999/', []);
$this->assertSame(200, $response->getStatusCode());
}
public function testRemoteInHostHeader(): void
{
$this->startServer('tcp-server');
$client = $this->createClient();
$response = $client->get('/', ['Host' => '127.0.0.1:19999']);
$this->assertSame(200, $response->getStatusCode());
}
public function testBrokenSocket(): void
{
$this->startServer('tcp-bugous-server');
$client = $this->createClient(['remote_socket' => '127.0.0.1:19999']);
$this->expectException(NetworkException::class);
$client->get('/', []);
}
public function testSslRemoteInUri(): void
{
$this->startServer('tcp-ssl-server');
$client = $this->createClient([
'remote_socket' => 'tcp://127.0.0.1:19999',
'ssl' => true,
'stream_context_options' => [
'ssl' => [
'peer_name' => 'socket-adapter',
'cafile' => __DIR__.'/server/ssl/ca.pem',
],
],
]);
$response = $client->get('/');
$this->assertSame(200, $response->getStatusCode());
}
public function testUnixSocketDomain(): void
{
$this->startServer('unix-domain-server');
$client = $this->createClient([
'remote_socket' => 'unix://'.__DIR__.'/server/server.sock',
]);
$response = $client->get('/', []);
$this->assertSame(200, $response->getStatusCode());
}
public function testNetworkExceptionOnConnectError(): void
{
$client = $this->createClient(['remote_socket' => '127.0.0.1:19999']);
$this->expectException(NetworkException::class);
$client->get('/', []);
}
public function testSslConnection()
{
$this->startServer('tcp-ssl-server');
$client = $this->createClient([
'remote_socket' => '127.0.0.1:19999',
'ssl' => true,
'stream_context_options' => [
'ssl' => [
'peer_name' => 'socket-adapter',
'cafile' => __DIR__.'/server/ssl/ca.pem',
],
],
]);
$response = $client->get('/', []);
$this->assertSame(200, $response->getStatusCode());
}
public function testSslConnectionWithClientCertificate(): void
{
$this->startServer('tcp-ssl-server-client');
$client = $this->createClient([
'remote_socket' => '127.0.0.1:19999',
'ssl' => true,
'stream_context_options' => [
'ssl' => [
'peer_name' => 'socket-adapter',
'cafile' => __DIR__.'/server/ssl/ca.pem',
'local_cert' => __DIR__.'/server/ssl/client-and-key.pem',
],
],
]);
$response = $client->get('/', []);
$this->assertSame(200, $response->getStatusCode());
}
public function testInvalidSslConnectionWithClientCertificate(): void
{
$this->startServer('tcp-ssl-server-client');
$client = $this->createClient([
'remote_socket' => '127.0.0.1:19999',
'ssl' => true,
'stream_context_options' => [
'ssl' => [
'peer_name' => 'socket-adapter',
'cafile' => __DIR__.'/server/ssl/ca.pem',
],
],
]);
$response = $client->get('/', []);
$this->assertEquals(403, $response->getStatusCode());
}
public function testNetworkExceptionOnSslError(): void
{
$this->startServer('tcp-server');
$client = $this->createClient(['remote_socket' => '127.0.0.1:19999', 'ssl' => true]);
$this->expectException(NetworkException::class);
$client->get('/', []);
}
public function testNetworkExceptionOnTimeout(): void
{
$client = $this->createClient(['timeout' => 1]);
$this->expectException(TimeoutException::class);
$response = $client->get('https://php.net', []);
$response->getBody()->getContents();
}
}