Skip to content

Commit

Permalink
feature #117 Leverage Request::getPayload() to populate the parsed …
Browse files Browse the repository at this point in the history
…body of PSR-7 requests (AurelienPillevesse)

This PR was squashed before being merged into the 2.2-dev branch.

Discussion
----------

Leverage `Request::getPayload()` to populate the parsed body of PSR-7 requests

I'm trying to send a ```POST``` request with the fields below and ```application/x-www-form-urlencoded``` header :
- "city": "Paris"
- "country": "France"

If I send it like that, the fields are understood and I have these fields when I display the Request :
- [ "city": "Paris", "country": "France" ]

Now if I send the same ```POST``` request with ```application/json``` header (with the same fields but in ```JSON``` format) :
- {"city":"Paris","country":"France"}

They are not understand and I have an empty Request :
- []

Commits
-------

3a8caad Leverage `Request::getPayload()` to populate the parsed body of PSR-7 requests
  • Loading branch information
derrabus committed Jul 25, 2023
2 parents 18c9e82 + 3a8caad commit 6b2f5df
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Factory/PsrHttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Psr\Http\Message\UploadedFileInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -29,6 +30,7 @@
* Builds Psr\HttpMessage instances using a PSR-17 implementation.
*
* @author Antonio J. García Lagar <[email protected]>
* @author Aurélien Pillevesse <[email protected]>
*/
class PsrHttpFactory implements HttpMessageFactoryInterface
{
Expand Down Expand Up @@ -71,12 +73,28 @@ public function createRequest(Request $symfonyRequest)

$body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));

if (method_exists(Request::class, 'getContentTypeFormat')) {
$format = $symfonyRequest->getContentTypeFormat();
} else {
$format = $symfonyRequest->getContentType();
}

if (method_exists(Request::class, 'getPayload') && 'json' === $format) {
try {
$parsedBody = $symfonyRequest->getPayload()->all();
} catch (JsonException $e) {
$parsedBody = [];
}
} else {
$parsedBody = $symfonyRequest->request->all();
}

$request = $request
->withBody($body)
->withUploadedFiles($this->getFiles($symfonyRequest->files->all()))
->withCookieParams($symfonyRequest->cookies->all())
->withQueryParams($symfonyRequest->query->all())
->withParsedBody($symfonyRequest->request->all())
->withParsedBody($parsedBody)
;

foreach ($symfonyRequest->attributes->all() as $key => $value) {
Expand Down
49 changes: 49 additions & 0 deletions Tests/Factory/PsrHttpFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/**
* @author Kévin Dunglas <[email protected]>
* @author Antonio J. García Lagar <[email protected]>
* @author Aurélien Pillevesse <[email protected]>
*/
class PsrHttpFactoryTest extends TestCase
{
Expand Down Expand Up @@ -246,4 +247,52 @@ public function testUploadErrNoFile()
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
}

public function testJsonContent()
{
if (!method_exists(Request::class, 'getPayload')) {
$this->markTestSkipped();
}

$headers = [
'HTTP_HOST' => 'http_host.fr',
'CONTENT_TYPE' => 'application/json',
];
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris","country":"France"}');
$psrRequest = $this->factory->createRequest($request);

$this->assertSame(['city' => 'Paris', 'country' => 'France'], $psrRequest->getParsedBody());
}

public function testEmptyJsonContent()
{
if (!method_exists(Request::class, 'getPayload')) {
$this->markTestSkipped();
}

$headers = [
'HTTP_HOST' => 'http_host.fr',
'CONTENT_TYPE' => 'application/json',
];
$request = new Request([], [], [], [], [], $headers, '{}');
$psrRequest = $this->factory->createRequest($request);

$this->assertSame([], $psrRequest->getParsedBody());
}

public function testWrongJsonContent()
{
if (!method_exists(Request::class, 'getPayload')) {
$this->markTestSkipped();
}

$headers = [
'HTTP_HOST' => 'http_host.fr',
'CONTENT_TYPE' => 'application/json',
];
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris"');
$psrRequest = $this->factory->createRequest($request);

$this->assertSame([], $psrRequest->getParsedBody());
}
}

0 comments on commit 6b2f5df

Please sign in to comment.