-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHttpBaseTest.php
More file actions
282 lines (242 loc) · 7.32 KB
/
HttpBaseTest.php
File metadata and controls
282 lines (242 loc) · 7.32 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
namespace Http\Client\Tests;
use Http\Message\MessageFactory;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Nerd\CartesianProduct\CartesianProduct;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
abstract class HttpBaseTest extends TestCase
{
/**
* @var string
*/
private static $logPath;
/**
* @var MessageFactory
*/
protected static $messageFactory;
/**
* @var array
*/
protected $defaultOptions = [
'protocolVersion' => '1.1',
'statusCode' => 200,
'reasonPhrase' => 'OK',
'headers' => ['Content-Type' => 'text/html'],
'body' => 'Ok',
];
/**
* @var array
*/
protected $defaultHeaders = [
'Connection' => 'close',
'User-Agent' => 'PHP HTTP Adapter',
'Content-Length' => '0',
];
/**
* {@inheritdoc}
*/
public static function setUpBeforeClass(): void
{
self::$logPath = PHPUnitUtility::getFile(true, 'php-http-adapter.log');
self::$messageFactory = new GuzzleMessageFactory();
}
/**
* {@inheritdoc}
*/
public static function tearDownAfterClass(): void
{
if (file_exists(self::$logPath)) {
unlink(self::$logPath);
}
}
public function requestProvider(): array
{
$sets = [
'methods' => $this->getMethods(),
'uris' => [$this->getUri()],
'headers' => $this->getHeaders(),
'body' => $this->getBodies(),
];
$cartesianProduct = new CartesianProduct($sets);
$cases = $cartesianProduct->compute();
// Filter all TRACE requests with a body, as they're not HTTP spec compliant
return array_filter($cases, function ($case) {
if ('TRACE' === $case[0] && null !== $case[3]) {
return false;
}
return true;
});
}
public function requestWithOutcomeProvider(): array
{
$sets = [
'urisAndOutcomes' => $this->getUrisAndOutcomes(),
'protocolVersions' => $this->getProtocolVersions(),
'headers' => $this->getHeaders(),
'body' => $this->getBodies(),
];
$cartesianProduct = new CartesianProduct($sets);
return $cartesianProduct->compute();
}
private function getMethods(): array
{
return [
'GET',
'HEAD',
'TRACE',
'POST',
'PUT',
'DELETE',
'OPTIONS',
];
}
/**
* @param string[] $query
*
* @return string|null
*/
protected function getUri(array $query = [])
{
return !empty($query)
? PHPUnitUtility::getUri().'?'.http_build_query($query, '', '&')
: PHPUnitUtility::getUri();
}
/**
* @return string
*/
protected function getInvalidUri()
{
return 'http://invalid.php-http.org';
}
/**
* @return array
*/
private function getUrisAndOutcomes()
{
return [
[
$this->getUri(['client_error' => true]),
[
'statusCode' => 400,
'reasonPhrase' => 'Bad Request',
],
],
[
$this->getUri(['server_error' => true]),
[
'statusCode' => 500,
'reasonPhrase' => 'Internal Server Error',
],
],
[
$this->getUri(['redirect' => true]),
[
'statusCode' => 302,
'reasonPhrase' => 'Found',
'body' => 'Redirect',
],
],
];
}
/**
* @return array
*/
private function getProtocolVersions()
{
return ['1.1', '1.0'];
}
/**
* @return string[]
*/
private function getHeaders()
{
$headers = $this->defaultHeaders;
$headers['Accept-Charset'] = 'utf-8';
$headers['Accept-Language'] = 'en';
return [
$this->defaultHeaders,
$headers,
];
}
/**
* @return array
*/
private function getBodies()
{
return [
null,
http_build_query($this->getData(), '', '&'),
];
}
/**
* @return array
*/
private function getData()
{
return ['param1' => 'foo', 'param2' => ['bar', ['baz']]];
}
protected function assertResponse(ResponseInterface $response, array $options = [])
{
$options = array_merge($this->defaultOptions, $options);
// The response version may be greater or equal to the request version. See https://tools.ietf.org/html/rfc2145#section-2.3
$this->assertTrue(substr($options['protocolVersion'], 0, 1) === substr($response->getProtocolVersion(), 0, 1) && 1 !== version_compare($options['protocolVersion'], $response->getProtocolVersion()));
$this->assertSame($options['statusCode'], $response->getStatusCode());
$this->assertSame($options['reasonPhrase'], $response->getReasonPhrase());
$this->assertNotEmpty($response->getHeaders());
foreach ($options['headers'] as $name => $value) {
$this->assertTrue($response->hasHeader($name));
$this->assertStringStartsWith($value, $response->getHeaderLine($name));
}
if (null === $options['body']) {
$this->assertEmpty($response->getBody()->__toString());
} else {
self::assertStringContainsString($options['body'], $response->getBody()->__toString());
}
}
/**
* @param string $method
* @param string[] $headers
* @param string $body
* @param string $protocolVersion
*/
protected function assertRequest(
$method,
array $headers = [],
$body = null,
$protocolVersion = '1.1'
) {
$request = $this->getRequest();
$actualProtocolVersion = substr($request['SERVER']['SERVER_PROTOCOL'], 5);
$this->assertTrue(substr($protocolVersion, 0, 1) === substr($actualProtocolVersion, 0, 1) && 1 !== version_compare($protocolVersion, $actualProtocolVersion));
$this->assertSame($method, $request['SERVER']['REQUEST_METHOD']);
$defaultHeaders = [
'Connection' => 'close',
'User-Agent' => 'PHP HTTP Adapter',
];
$headers = array_merge($defaultHeaders, $headers);
foreach ($headers as $name => $value) {
if (is_int($name)) {
list($name, $value) = explode(':', $value);
}
$name = strtoupper(str_replace('-', '_', 'http-'.$name));
if ('TRACE' === $method && 'HTTP_CONTENT_LENGTH' === $name && !isset($request['SERVER'][$name])) {
continue;
}
$this->assertArrayHasKey($name, $request['SERVER']);
$this->assertSame($value, $request['SERVER'][$name], "Failed asserting value for {$name}.");
}
}
/**
* @return array
*/
protected function getRequest()
{
$file = fopen(self::$logPath, 'r');
flock($file, LOCK_EX);
$request = json_decode(stream_get_contents($file), true);
flock($file, LOCK_UN);
fclose($file);
return $request;
}
}