Merge pull request #300 from MySchoolManagement/support-multipart-formdata

Adds support for the multipart/form-data content type
This commit is contained in:
Vladimir Razuvaev 2018-07-07 21:45:21 +07:00 committed by GitHub
commit dbafdf849e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) {
$bodyParams = $_POST;
} else if (stripos($contentType, 'multipart/form-data') !== false) {
$bodyParams = $_POST;
} else if (null === $contentType) {
throw new RequestError('Missing "Content-Type" header');
} 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()
{
$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