-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathServerRequestTest.php
More file actions
250 lines (231 loc) · 9.29 KB
/
Copy pathServerRequestTest.php
File metadata and controls
250 lines (231 loc) · 9.29 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
<?php
use FastD\Http\PhpInputStream;
use FastD\Http\ServerRequest;
use PHPUnit\Framework\TestCase;
/**
*
* @author jan huang <bboyjanhuang@gmail.com>
* @copyright 2018
*
* @link https://www.github.com/janhuang
* @link http://www.fast-d.cn/
*/
class ServerRequestTest extends TestCase
{
public function dataInputFromGlobals()
{
fwrite(STDOUT, http_build_query($this->dataBodyFromGlobals()));
}
public function dataFilesFromGlobals()
{
return [
'file' => [
'name' => 'MyFile.txt',
'type' => 'text/plain',
'tmp_name' => '/tmp/php/php1h4j1o',
'error' => UPLOAD_ERR_OK,
'size' => 123,
],
'files' => [
'name' => [
'MyFile.txt',
'MyFile2.txt',
],
'type' => [
'text/plain',
'text/plain',
],
'tmp_name' => [
'/tmp/php/php1h4j1o',
'/tmp/php/php1h4j1o',
],
'error' => [
UPLOAD_ERR_OK,
UPLOAD_ERR_OK
],
'size' => [
123,
123
],
]
];
}
public function dataQueryFromGlobals()
{
return [
'id' => 10,
'user' => 'foo',
];
}
public function dataBodyFromGlobals()
{
return [
'name' => 'Pesho',
'email' => 'pesho@example.com',
];
}
public function dataCookiesFromGlobals()
{
return [
'logged-in' => 'yes!'
];
}
public function dataServerFromGlobals()
{
return [
'PHP_SELF' => '/blog/article.php',
'GATEWAY_INTERFACE' => 'CGI/1.1',
'SERVER_ADDR' => 'Server IP: 217.112.82.20',
'SERVER_NAME' => 'www.blakesimpson.co.uk',
'SERVER_SOFTWARE' => 'Apache/2.2.15 (Win32) JRun/4.0 PHP/5.2.13',
'SERVER_PROTOCOL' => 'HTTP/1.0',
'REQUEST_METHOD' => 'POST',
'REQUEST_TIME' => 'Request start time: 1280149029',
'QUERY_STRING' => 'id=10&user=foo',
'DOCUMENT_ROOT' => '/path/to/your/server/root/',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
'HTTP_ACCEPT_LANGUAGE' => 'en-gb,en;q=0.5',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_HOST' => 'www.blakesimpson.co.uk',
'HTTP_REFERER' => 'http://previous.url.com',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)',
'HTTPS' => '1',
'REMOTE_ADDR' => '193.60.168.69',
'REMOTE_HOST' => 'Client server\'s host name',
'REMOTE_PORT' => '5390',
'SCRIPT_FILENAME' => '/path/to/this/script.php',
'SERVER_ADMIN' => 'webmaster@blakesimpson.co.uk',
'SERVER_PORT' => '80',
'SERVER_SIGNATURE' => 'Version signature: 5.123',
'SCRIPT_NAME' => '/blog/article.php',
'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
];
}
public function dataHeadersFromGlobals()
{
return [
'accept' => [
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
],
'accept_charset' => [
'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
],
'accept_encoding' => [
'gzip,deflate',
],
'accept_language' => [
'en-gb,en;q=0.5',
],
'connection' => [
'keep-alive',
],
'host' => [
'www.blakesimpson.co.uk',
],
'referer' => [
'http://previous.url.com',
],
'user_agent' => [
'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)',
],
];
}
public function dataPUTServerFromGlobals()
{
return [
'PHP_SELF' => '/blog/article.php',
'GATEWAY_INTERFACE' => 'CGI/1.1',
'SERVER_ADDR' => 'Server IP: 217.112.82.20',
'SERVER_NAME' => 'www.blakesimpson.co.uk',
'SERVER_SOFTWARE' => 'Apache/2.2.15 (Win32) JRun/4.0 PHP/5.2.13',
'SERVER_PROTOCOL' => 'HTTP/1.0',
'REQUEST_METHOD' => 'PUT',
'REQUEST_TIME' => 'Request start time: 1280149029',
'QUERY_STRING' => 'id=10&user=foo',
'DOCUMENT_ROOT' => '/path/to/your/server/root/',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
'HTTP_ACCEPT_LANGUAGE' => 'en-gb,en;q=0.5',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_HOST' => 'www.blakesimpson.co.uk',
'HTTP_REFERER' => 'http://previous.url.com',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)',
'HTTPS' => '1',
'REMOTE_ADDR' => '193.60.168.69',
'REMOTE_HOST' => 'Client server\'s host name',
'REMOTE_PORT' => '5390',
'SCRIPT_FILENAME' => '/path/to/this/script.php',
'SERVER_ADMIN' => 'webmaster@blakesimpson.co.uk',
'SERVER_PORT' => '80',
'SERVER_SIGNATURE' => 'Version signature: 5.123',
'SCRIPT_NAME' => '/blog/article.php',
'REQUEST_URI' => '/blog/article.php?id=100&user=foo',
];
}
public function testDefaultInstanceServerRequest()
{
$serverRequest = new ServerRequest('GET', 'http://example.com');
$this->assertEquals('GET', $serverRequest->getMethod());
$this->assertEquals('http', $serverRequest->getUri()->getScheme());
$this->assertEquals('/', $serverRequest->getUri()->getPath());
}
public function testServerRequestQueryParams()
{
$serverRequest = new ServerRequest('GET', 'http://example.com');
$serverRequest->withQueryParams($this->dataQueryFromGlobals());
$this->assertEquals($this->dataQueryFromGlobals(), $serverRequest->getQueryParams());
$serverRequest = new ServerRequest('GET', 'http://example.com?foo=bar');
$this->assertEquals($serverRequest->getQueryParams(), [
'foo' => 'bar'
]);
}
public function testServerRequestCookieParams()
{
$serverRequest = new ServerRequest('GET', 'http://example.com');
$serverRequest->withCookieParams($this->dataCookiesFromGlobals());
$this->assertEquals($this->dataCookiesFromGlobals(), $serverRequest->getCookieParams());
}
public function testServerRequestFromGlobals()
{
$_SERVER = $this->dataServerFromGlobals();
$_COOKIE = $this->dataCookiesFromGlobals();
$_GET = $this->dataQueryFromGlobals();
$_POST = $this->dataBodyFromGlobals();
$serverRequest = ServerRequest::createServerRequestFromGlobals();
$this->assertEquals($serverRequest->getUri()->getPath(), '/blog/article.php');
$this->assertEquals('POST', $serverRequest->getMethod());
$this->assertEquals($this->dataQueryFromGlobals() , $serverRequest->getQueryParams());
$this->assertEquals($this->dataCookiesFromGlobals(), $serverRequest->getCookieParams());
$this->assertEquals($this->dataCookiesFromGlobals(), $serverRequest->getCookieParams());
$this->assertEquals($this->dataBodyFromGlobals(), $serverRequest->getParsedBody());
$this->assertEquals($this->dataHeadersFromGlobals(), $serverRequest->getHeaders());
}
public function testSerRequestFromGlobalsIsMethodPUT()
{
$_SERVER = $this->dataPUTServerFromGlobals();
$_COOKIE = $this->dataCookiesFromGlobals();
$_GET = $this->dataQueryFromGlobals();
$_FILES = $this->dataFilesFromGlobals();
$body = new PhpInputStream('php://temp', 'wr');
$body->write(http_build_query($this->dataBodyFromGlobals()));
$serverRequest = new ServerRequest('DELETE', 'http://example.com/blog/articles.php', [], $body, $_SERVER);
$serverRequest->withCookieParams($_COOKIE);
$serverRequest->withQueryParams($_GET);
$serverRequest->withUploadedFiles($_FILES);
$this->assertEquals('10', $serverRequest->getParam('id'));
}
public function testServerRequestFilesNormalizer()
{
$serverRequest = new ServerRequest('PUT', 'http://example.com/blog/articles.php');
$serverRequest->withUploadedFiles($this->dataFilesFromGlobals());
$files = $serverRequest->getUploadedFiles();
$this->assertNotEmpty($files);
}
public function testCreateUriFromGlobal()
{
$this->assertEquals('http://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo', ServerRequest::createUriFromGlobal($this->dataPUTServerFromGlobals()));
}
}