File tree Expand file tree Collapse file tree 2 files changed +32
-3
lines changed
app/code/Magento/GraphQl/Controller
dev/tests/integration/testsuite/Magento/GraphQl/Controller Expand file tree Collapse file tree 2 files changed +32
-3
lines changed Original file line number Diff line number Diff line change 1919use Magento \Framework \App \ResponseInterface ;
2020use Magento \Framework \Controller \Result \JsonFactory ;
2121use Magento \Framework \GraphQl \Exception \ExceptionFormatter ;
22+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
2223use Magento \Framework \GraphQl \Query \Fields as QueryFields ;
2324use Magento \Framework \GraphQl \Query \QueryParser ;
2425use Magento \Framework \GraphQl \Query \QueryProcessor ;
@@ -208,7 +209,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
208209 );
209210 $ statusCode = 200 ;
210211 }
211- } catch (SyntaxError $ error ) {
212+ } catch (SyntaxError | GraphQlInputException $ error ) {
212213 $ result = [
213214 'errors ' => [FormattedError::createFromException ($ error )],
214215 ];
@@ -245,7 +246,7 @@ private function getDataFromRequest(RequestInterface $request): array
245246 {
246247 /** @var Http $request */
247248 if ($ request ->isPost ()) {
248- $ data = $ this ->jsonSerializer ->unserialize ($ request ->getContent ());
249+ $ data = $ request -> getContent () ? $ this ->jsonSerializer ->unserialize ($ request ->getContent ()) : [] ;
249250 } elseif ($ request ->isGet ()) {
250251 $ data = $ request ->getParams ();
251252 $ data ['variables ' ] = isset ($ data ['variables ' ]) ?
Original file line number Diff line number Diff line change @@ -273,7 +273,7 @@ public function testDispatchWithOptions(): void
273273 self ::assertEmpty ($ response ->getContent ());
274274 }
275275
276- public function testDispatchWithoutQuery (): void
276+ public function testDispatchWithGetWithoutQuery (): void
277277 {
278278 $ this ->request ->setPathInfo ('/graphql ' );
279279 $ this ->request ->setMethod ('GET ' );
@@ -282,4 +282,32 @@ public function testDispatchWithoutQuery(): void
282282 $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
283283 self ::assertNotEmpty ($ output ['errors ' ]);
284284 }
285+
286+ public function testDispatchWithPostAndWrongContentType (): void
287+ {
288+ $ query = <<<QUERY
289+ {
290+ products(filter: {sku: {eq: "simple1"}}) {
291+ items {
292+ id
293+ name
294+ sku
295+ }
296+ }
297+ }
298+ QUERY ;
299+ $ postData = [
300+ 'query ' => $ query ,
301+ 'variables ' => null ,
302+ 'operationName ' => null
303+ ];
304+
305+ $ this ->request ->setPathInfo ('/graphql ' );
306+ $ this ->request ->setMethod ('POST ' );
307+ $ this ->request ->setContent (json_encode ($ postData ));
308+ $ response = $ this ->graphql ->dispatch ($ this ->request );
309+ self ::assertEquals (400 , $ response ->getStatusCode ());
310+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
311+ self ::assertNotEmpty ($ output ['errors ' ]);
312+ }
285313}
You can’t perform that action at this time.
0 commit comments