Skip to content

Commit 412596e

Browse files
authored
Merge pull request #558 from flightphp/request-improvements
little changes to requests and integration testing JSON POST requests
2 parents 018ca42 + 9786820 commit 412596e

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

flight/net/Request.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,24 @@ public function __construct(array $config = [])
146146
// Default properties
147147
if (empty($config)) {
148148
$config = [
149-
'url' => str_replace('@', '%40', self::getVar('REQUEST_URI', '/')),
150-
'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))),
151-
'method' => self::getMethod(),
152-
'referrer' => self::getVar('HTTP_REFERER'),
153-
'ip' => self::getVar('REMOTE_ADDR'),
154-
'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'),
155-
'scheme' => self::getScheme(),
149+
'url' => str_replace('@', '%40', self::getVar('REQUEST_URI', '/')),
150+
'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))),
151+
'method' => self::getMethod(),
152+
'referrer' => self::getVar('HTTP_REFERER'),
153+
'ip' => self::getVar('REMOTE_ADDR'),
154+
'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'),
155+
'scheme' => self::getScheme(),
156156
'user_agent' => self::getVar('HTTP_USER_AGENT'),
157-
'type' => self::getVar('CONTENT_TYPE'),
158-
'length' => intval(self::getVar('CONTENT_LENGTH', 0)),
159-
'query' => new Collection($_GET),
160-
'data' => new Collection($_POST),
161-
'cookies' => new Collection($_COOKIE),
162-
'files' => new Collection($_FILES),
163-
'secure' => 'https' === self::getScheme(),
164-
'accept' => self::getVar('HTTP_ACCEPT'),
165-
'proxy_ip' => self::getProxyIpAddress(),
166-
'host' => self::getVar('HTTP_HOST'),
157+
'type' => self::getVar('CONTENT_TYPE'),
158+
'length' => intval(self::getVar('CONTENT_LENGTH', 0)),
159+
'query' => new Collection($_GET),
160+
'data' => new Collection($_POST),
161+
'cookies' => new Collection($_COOKIE),
162+
'files' => new Collection($_FILES),
163+
'secure' => 'https' === self::getScheme(),
164+
'accept' => self::getVar('HTTP_ACCEPT'),
165+
'proxy_ip' => self::getProxyIpAddress(),
166+
'host' => self::getVar('HTTP_HOST'),
167167
];
168168
}
169169

@@ -181,7 +181,7 @@ public function init(array $properties = []): self
181181
{
182182
// Set all the defined properties
183183
foreach ($properties as $name => $value) {
184-
$this->$name = $value;
184+
$this->{$name} = $value;
185185
}
186186

187187
// Get the requested URL without the base directory
@@ -229,7 +229,7 @@ public function getBody(): string
229229
return $body;
230230
}
231231

232-
$method = self::getMethod();
232+
$method = $this->method ?? self::getMethod();
233233

234234
if ('POST' === $method || 'PUT' === $method || 'DELETE' === $method || 'PATCH' === $method) {
235235
$body = file_get_contents($this->stream_path);

tests/EngineTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Exception;
88
use Flight;
99
use flight\Engine;
10+
use flight\net\Request;
1011
use flight\net\Response;
12+
use flight\util\Collection;
1113
use PHPUnit\Framework\TestCase;
1214

1315
// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore
@@ -318,6 +320,33 @@ public function testRedirectWithBaseUrl()
318320
$this->assertEquals(301, $engine->response()->status());
319321
}
320322

323+
public function testJsonRequestBody()
324+
{
325+
$engine = new Engine();
326+
$tmpfile = tmpfile();
327+
$stream_path = stream_get_meta_data($tmpfile)['uri'];
328+
file_put_contents($stream_path, '{"key1":"value1","key2":"value2"}');
329+
330+
$engine->register('request', Request::class, [
331+
[
332+
'method' => 'POST',
333+
'url' => '/something/fancy',
334+
'base' => '/vagrant/public',
335+
'type' => 'application/json',
336+
'length' => 13,
337+
'data' => new Collection(),
338+
'query' => new Collection(),
339+
'stream_path' => $stream_path
340+
]
341+
]);
342+
$engine->post('/something/fancy', function () use ($engine) {
343+
echo $engine->request()->data->key1;
344+
echo $engine->request()->data->key2;
345+
});
346+
$engine->start();
347+
$this->expectOutputString('value1value2');
348+
}
349+
321350
public function testJson()
322351
{
323352
$engine = new Engine();

0 commit comments

Comments
 (0)