Repository navigation
Expand file tree
/
Copy pathServerRequestTest.php
More file actions
243 lines (202 loc) · 9.32 KB
/
Copy pathServerRequestTest.php
File metadata and controls
243 lines (202 loc) · 9.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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\HttpMessage;
use Hyperf\Codec\Json;
use Hyperf\Codec\Xml;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpMessage\Exception\BadRequestHttpException;
use Hyperf\HttpMessage\Server\Request;
use Hyperf\HttpMessage\Server\Request\JsonParser;
use Hyperf\HttpMessage\Server\Request\Parser;
use Hyperf\HttpMessage\Server\Request\XmlParser;
use Hyperf\HttpMessage\Server\RequestParserInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use HyperfTest\HttpMessage\Stub\ParserStub;
use HyperfTest\HttpMessage\Stub\Server\RequestStub;
use InvalidArgumentException;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use ReflectionClass;
use Swoole\Http\Request as SwooleRequest;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class ServerRequestTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
RequestStub::setParser(null);
}
public function testNormalizeParsedBody()
{
$this->getContainer();
$data = ['id' => 1];
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('');
$this->assertSame($data, RequestStub::normalizeParsedBody($data));
$this->assertSame($data, RequestStub::normalizeParsedBody($data, $request));
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/xml; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(Xml::toXml($json)));
$this->assertSame($json, RequestStub::normalizeParsedBody($json, $request));
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(Json::encode($json)));
$this->assertSame($json, RequestStub::normalizeParsedBody($data, $request));
}
public function testNormalizeParsedBodyException()
{
$this->expectException(BadRequestHttpException::class);
$this->getContainer();
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream('xxxx'));
$this->assertSame([], RequestStub::normalizeParsedBody($json, $request));
}
public function testXmlNormalizeParsedBodyException()
{
$this->expectException(BadRequestHttpException::class);
$this->getContainer();
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/xml; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream('xxxx'));
$this->assertSame([], RequestStub::normalizeParsedBody($json, $request));
}
public function testNormalizeEmptyBody()
{
$this->getContainer();
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(''));
$this->assertSame($json, RequestStub::normalizeParsedBody($json, $request));
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/json; charset=utf-8');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(''));
$this->assertSame([], RequestStub::normalizeParsedBody([], $request));
}
public function testNormalizeParsedBodyInvalidContentType()
{
$this->getContainer();
$data = ['id' => 1];
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/JSON');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(json_encode($json)));
$this->assertSame($json, RequestStub::normalizeParsedBody($data, $request));
}
public function testOverrideRequestParser()
{
$this->getContainer();
$this->assertSame(Parser::class, get_class(RequestStub::getParser()));
RequestStub::setParser(new ParserStub());
$json = ['name' => 'Hyperf'];
$request = Mockery::mock(ServerRequestInterface::class);
$request->shouldReceive('getHeaderLine')->with('content-type')->andReturn('application/JSON');
$request->shouldReceive('getBody')->andReturn(new SwooleStream(json_encode($json)));
$this->assertSame(['mock' => true], RequestStub::normalizeParsedBody([], $request));
$this->assertSame(ParserStub::class, get_class(RequestStub::getParser()));
}
public function testGetUriFromGlobals()
{
$swooleRequest = Mockery::mock(SwooleRequest::class);
$data = ['name' => 'Hyperf'];
$swooleRequest->shouldReceive('rawContent')->andReturn(Json::encode($data));
$swooleRequest->server = ['server_port' => 9501];
$request = Request::loadFromSwooleRequest($swooleRequest);
$uri = $request->getUri();
$this->assertSame(9501, $uri->getPort());
$swooleRequest = Mockery::mock(SwooleRequest::class);
$data = ['name' => 'Hyperf'];
$swooleRequest->shouldReceive('rawContent')->andReturn(Json::encode($data));
$swooleRequest->header = ['host' => '127.0.0.1'];
$swooleRequest->server = ['server_port' => 9501];
$request = Request::loadFromSwooleRequest($swooleRequest);
$uri = $request->getUri();
$this->assertSame(null, $uri->getPort());
}
#[Group('ParseHost')]
public function testParseHost()
{
$hostStrIPv4 = '192.168.119.100:9501';
$hostStrIPv6 = '[fe80::a464:1aff:fe88:7b5a]:9502';
$objReflectClass = new ReflectionClass('Hyperf\HttpMessage\Server\Request');
$method = $objReflectClass->getMethod('parseHost');
$resIPv4 = $method->invokeArgs(null, [$hostStrIPv4]);
$this->assertSame('192.168.119.100', $resIPv4[0]);
$this->assertSame(9501, $resIPv4[1]);
$resIPv6 = $method->invokeArgs(null, [$hostStrIPv6]);
$this->assertSame('[fe80::a464:1aff:fe88:7b5a]', $resIPv6[0]);
$this->assertSame(9502, $resIPv6[1]);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid host: ');
$method->invokeArgs(null, ['']);
}
/**
* @param mixed $originHost
* @param mixed $host
* @param mixed $port
*/
#[DataProvider('getIPv6Examples')]
public function testGetUriFromGlobalsForIPv6Host($originHost, $host, $port)
{
$swooleRequest = Mockery::mock(SwooleRequest::class);
$data = ['name' => 'Hyperf'];
$swooleRequest->shouldReceive('rawContent')->andReturn(Json::encode($data));
$swooleRequest->server = [
'http_host' => $originHost,
];
$request = Request::loadFromSwooleRequest($swooleRequest);
$uri = $request->getUri();
$this->assertSame($port, $uri->getPort());
$this->assertSame($host, $uri->getHost());
$swooleRequest->server = [];
$swooleRequest->header = [
'host' => $originHost,
];
$request = Request::loadFromSwooleRequest($swooleRequest);
$uri = $request->getUri();
$this->assertSame($port, $uri->getPort());
$this->assertSame($host, $uri->getHost());
}
public static function getIPv6Examples(): array
{
return [
['localhost:9501', 'localhost', 9501],
['localhost:', 'localhost', null],
['localhost', 'localhost', null],
['[2a00:f48:1008::212:183:10]', '[2a00:f48:1008::212:183:10]', null],
['[2a00:f48:1008::212:183:10]:9501', '[2a00:f48:1008::212:183:10]', 9501],
['[2a00:f48:1008::212:183:10]:', '[2a00:f48:1008::212:183:10]', null],
];
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
$container->shouldReceive('has')->andReturn(true);
$container->shouldReceive('make')->with(JsonParser::class, Mockery::any())->andReturn(new JsonParser());
$container->shouldReceive('make')->with(XmlParser::class, Mockery::any())->andReturn(new XmlParser());
ApplicationContext::setContainer($container);
$container->shouldReceive('get')->with(RequestParserInterface::class)->andReturn(new Parser());
return $container;
}
}