From c5b78c66e95fbba25dc669b3bc8db3b7952464e0 Mon Sep 17 00:00:00 2001 From: Iain Mckay Date: Thu, 5 Jul 2018 13:52:29 +0700 Subject: [PATCH] Adds support for the multipart/form-data content type (cherry picked from commit 750ce38) --- src/Server/Helper.php | 2 ++ tests/Server/RequestParsingTest.php | 53 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Server/Helper.php b/src/Server/Helper.php index d7e62a8..a767137 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -71,6 +71,8 @@ class Helper } } else if (stripos($contentType, 'application/x-www-form-urlencoded') !== false) { $bodyParams = $_POST; + } else if (stripos($contentType, 'multipart/form-data') !== false) { + $bodyParams = $_POST; } else if (null === $contentType) { throw new RequestError('Missing "Content-Type" header'); } else { diff --git a/tests/Server/RequestParsingTest.php b/tests/Server/RequestParsingTest.php index bff4da3..c32a3ab 100644 --- a/tests/Server/RequestParsingTest.php +++ b/tests/Server/RequestParsingTest.php @@ -69,6 +69,28 @@ class RequestParsingTest extends \PHPUnit_Framework_TestCase } } + public function testParsesMultipartFormdataRequest() + { + $query = '{my query}'; + $variables = ['test' => 1, 'test2' => 2]; + $operation = 'op'; + + $post = [ + 'query' => $query, + 'variables' => $variables, + 'operationName' => $operation + ]; + $parsed = [ + 'raw' => $this->parseRawMultipartFormdataRequest($post), + 'psr' => $this->parsePsrMultipartFormdataRequest($post) + ]; + + foreach ($parsed as $method => $parsedBody) { + $this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method); + $this->assertFalse($parsedBody->isReadOnly(), $method); + } + } + public function testParsesJSONRequest() { $query = '{my query}'; @@ -327,6 +349,37 @@ class RequestParsingTest extends \PHPUnit_Framework_TestCase return $helper->parsePsrRequest($psrRequest); } + /** + * @param array $postValue + * @return OperationParams|OperationParams[] + */ + private function parseRawMultipartFormDataRequest($postValue) + { + $_SERVER['CONTENT_TYPE'] = 'multipart/form-data; boundary=----FormBoundary'; + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST = $postValue; + + $helper = new Helper(); + return $helper->parseHttpRequest(function() { + throw new InvariantViolation("Shouldn't read from php://input for multipart/form-data request"); + }); + } + + /** + * @param $postValue + * @return array|Helper + */ + private function parsePsrMultipartFormDataRequest($postValue) + { + $psrRequest = new PsrRequestStub(); + $psrRequest->headers['content-type'] = ['multipart/form-data; boundary=----FormBoundary']; + $psrRequest->method = 'POST'; + $psrRequest->parsedBody = $postValue; + + $helper = new Helper(); + return $helper->parsePsrRequest($psrRequest); + } + /** * @param $getValue * @return OperationParams