-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mnapoli/fix-1
Fix #1 Support for POST body
- Loading branch information
Showing
5 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/tests/Bridge/Symfony/cache | ||
/tests/Bridge/Symfony/logs | ||
/.bref/ | ||
/.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
language: php | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- nightly | ||
|
||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
- php: nightly | ||
include: | ||
- php: 7.1 | ||
env: dependencies=lowest | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
before_script: | ||
- composer install -n | ||
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; | ||
|
||
script: | ||
- vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bref\Test\Bridge\Psr7; | ||
|
||
use Bref\Bridge\Psr7\RequestFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RequestFactoryTest extends TestCase | ||
{ | ||
public function test create basic request() | ||
{ | ||
$currentTimestamp = time(); | ||
|
||
$request = RequestFactory::fromLambdaEvent([ | ||
'httpMethod' => 'GET', | ||
'queryStringParameters' => [ | ||
'foo' => 'bar', | ||
'bim' => 'baz', | ||
], | ||
'requestContext' => [ | ||
'protocol' => '1.1', | ||
'path' => '/test', | ||
'requestTimeEpoch' => $currentTimestamp, | ||
], | ||
'headers' => [ | ||
], | ||
]); | ||
|
||
self::assertEquals('GET', $request->getMethod()); | ||
self::assertEquals(['foo' => 'bar', 'bim' => 'baz'], $request->getQueryParams()); | ||
self::assertEquals('1.1', $request->getProtocolVersion()); | ||
self::assertEquals('/test', $request->getUri()->__toString()); | ||
self::assertEquals('', $request->getBody()->getContents()); | ||
self::assertEquals([], $request->getAttributes()); | ||
$serverParams = $request->getServerParams(); | ||
unset($serverParams['DOCUMENT_ROOT']); | ||
self::assertEquals([ | ||
'SERVER_PROTOCOL' => '1.1', | ||
'REQUEST_METHOD' => 'GET', | ||
'REQUEST_TIME' => $currentTimestamp, | ||
'QUERY_STRING' => 'foo=bar&bim=baz', | ||
'REQUEST_URI' => '/test', | ||
], $serverParams); | ||
self::assertEquals('/test', $request->getRequestTarget()); | ||
self::assertEquals([], $request->getHeaders()); | ||
} | ||
|
||
public function test non empty body() | ||
{ | ||
$request = RequestFactory::fromLambdaEvent([ | ||
'httpMethod' => 'GET', | ||
'body' => 'test test test', | ||
]); | ||
|
||
self::assertEquals('test test test', $request->getBody()->getContents()); | ||
} | ||
|
||
public function test POST body is parsed() | ||
{ | ||
$request = RequestFactory::fromLambdaEvent([ | ||
'httpMethod' => 'POST', | ||
'headers' => [ | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
], | ||
'body' => 'foo=bar&bim=baz', | ||
]); | ||
self::assertEquals('POST', $request->getMethod()); | ||
self::assertEquals(['foo' => 'bar', 'bim' => 'baz'], $request->getParsedBody()); | ||
} | ||
|
||
public function test POST JSON body is not parsed() | ||
{ | ||
$request = RequestFactory::fromLambdaEvent([ | ||
'httpMethod' => 'POST', | ||
'headers' => [ | ||
'Content-Type' => 'application/json', | ||
], | ||
'body' => json_encode(['foo' => 'bar']), | ||
]); | ||
self::assertEquals('POST', $request->getMethod()); | ||
self::assertEquals(null, $request->getParsedBody()); | ||
self::assertEquals(['foo' => 'bar'], json_decode($request->getBody()->getContents(), true)); | ||
} | ||
|
||
public function test multipart form data is not supported() | ||
{ | ||
$request = RequestFactory::fromLambdaEvent([ | ||
'httpMethod' => 'POST', | ||
'headers' => [ | ||
'Content-Type' => 'multipart/form-data', | ||
], | ||
'body' => 'abcd', | ||
]); | ||
self::assertEquals('POST', $request->getMethod()); | ||
self::assertNull(null, $request->getParsedBody()); | ||
} | ||
|
||
public function test cookies are not supported() | ||
{ | ||
$request = RequestFactory::fromLambdaEvent([ | ||
'httpMethod' => 'GET', | ||
'headers' => [ | ||
'Cookie' => 'theme=light', | ||
], | ||
]); | ||
self::assertEquals([], $request->getCookieParams()); | ||
} | ||
} |