Adds support for the multipart/form-data content type

This commit is contained in:
Iain Mckay 2018-07-05 08:52:29 +02:00
parent ddc3a01f09
commit 750ce383ec
2 changed files with 55 additions and 0 deletions

View File

@ -71,6 +71,8 @@ class Helper
} }
} else if (stripos($contentType, 'application/x-www-form-urlencoded') !== false) { } else if (stripos($contentType, 'application/x-www-form-urlencoded') !== false) {
$bodyParams = $_POST; $bodyParams = $_POST;
} else if (stripos($contentType, 'multipart/form-data') !== false) {
$bodyParams = $_POST;
} else if (null === $contentType) { } else if (null === $contentType) {
throw new RequestError('Missing "Content-Type" header'); throw new RequestError('Missing "Content-Type" header');
} else { } else {

View File

@ -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() public function testParsesJSONRequest()
{ {
$query = '{my query}'; $query = '{my query}';
@ -327,6 +349,37 @@ class RequestParsingTest extends \PHPUnit_Framework_TestCase
return $helper->parsePsrRequest($psrRequest); 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 * @param $getValue
* @return OperationParams * @return OperationParams