@@ -264,26 +264,65 @@ public function testError() : void
264264 }
265265 }
266266
267- public function testDispatchWithOptions (): void
267+ public function testDispatchOptions (): void
268268 {
269269 $ this ->request ->setPathInfo ('/graphql ' );
270270 $ this ->request ->setMethod ('OPTIONS ' );
271271 $ response = $ this ->graphql ->dispatch ($ this ->request );
272- self ::assertEquals (204 , $ response ->getStatusCode ());
272+ self ::assertEquals (200 , $ response ->getStatusCode ());
273273 self ::assertEmpty ($ response ->getContent ());
274274 }
275275
276- public function testDispatchWithGetWithoutQuery (): void
276+ public function testDispatchGetWithoutQuery (): void
277277 {
278278 $ this ->request ->setPathInfo ('/graphql ' );
279279 $ this ->request ->setMethod ('GET ' );
280280 $ response = $ this ->graphql ->dispatch ($ this ->request );
281281 self ::assertEquals (400 , $ response ->getStatusCode ());
282282 $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
283+ self ::assertArrayHasKey ('errors ' , $ output );
283284 self ::assertNotEmpty ($ output ['errors ' ]);
285+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
286+ self ::assertStringStartsWith ('Syntax Error: ' , $ output ['errors ' ][0 ]['message ' ]);
284287 }
285288
286- public function testDispatchWithPostAndWrongContentType (): void
289+ public function testDispatchGetWithInvalidQuery (): void
290+ {
291+ $ query = <<<QUERY
292+ {
293+ products(filter: {sku: {eq: "simple1"}
294+ }
295+ QUERY ;
296+
297+ $ this ->request ->setPathInfo ('/graphql ' );
298+ $ this ->request ->setMethod ('GET ' );
299+ $ this ->request ->setQueryValue ('query ' , $ query );
300+ $ response = $ this ->graphql ->dispatch ($ this ->request );
301+ self ::assertEquals (400 , $ response ->getStatusCode ());
302+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
303+ self ::assertArrayHasKey ('errors ' , $ output );
304+ self ::assertNotEmpty ($ output ['errors ' ]);
305+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
306+ self ::assertStringStartsWith ('Syntax Error: ' , $ output ['errors ' ][0 ]['message ' ]);
307+ }
308+
309+ public function testDispatchPostWithoutQuery (): void
310+ {
311+ $ this ->request ->setPathInfo ('/graphql ' );
312+ $ this ->request ->setMethod ('POST ' );
313+ $ headers = $ this ->objectManager ->create (\Laminas \Http \Headers::class)
314+ ->addHeaders (['Content-Type ' => 'application/json ' ]);
315+ $ this ->request ->setHeaders ($ headers );
316+ $ response = $ this ->graphql ->dispatch ($ this ->request );
317+ self ::assertEquals (400 , $ response ->getStatusCode ());
318+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
319+ self ::assertArrayHasKey ('errors ' , $ output );
320+ self ::assertNotEmpty ($ output ['errors ' ]);
321+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
322+ self ::assertStringStartsWith ('Syntax Error: ' , $ output ['errors ' ][0 ]['message ' ]);
323+ }
324+
325+ public function testDispatchPostWithInvalidJson (): void
287326 {
288327 $ query = <<<QUERY
289328{
@@ -296,18 +335,47 @@ public function testDispatchWithPostAndWrongContentType(): void
296335 }
297336}
298337QUERY ;
299- $ postData = [
300- 'query ' => $ query ,
301- 'variables ' => null ,
302- 'operationName ' => null
303- ];
338+ $ postData = ['query ' => $ query ];
339+
340+ $ this ->request ->setPathInfo ('/graphql ' );
341+ $ this ->request ->setMethod ('POST ' );
342+ $ this ->request ->setContent (http_build_query ($ postData ));
343+ $ headers = $ this ->objectManager ->create (\Laminas \Http \Headers::class)
344+ ->addHeaders (['Content-Type ' => 'application/json ' ]);
345+ $ this ->request ->setHeaders ($ headers );
346+ $ response = $ this ->graphql ->dispatch ($ this ->request );
347+ self ::assertEquals (400 , $ response ->getStatusCode ());
348+ $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
349+ self ::assertArrayHasKey ('errors ' , $ output );
350+ self ::assertNotEmpty ($ output ['errors ' ]);
351+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
352+ self ::assertEquals ('Unable to parse the request. ' , $ output ['errors ' ][0 ]['message ' ]);
353+ }
354+
355+ public function testDispatchPostWithWrongContentType (): void
356+ {
357+ $ query = <<<QUERY
358+ {
359+ products(filter: {sku: {eq: "simple1"}}) {
360+ items {
361+ id
362+ name
363+ sku
364+ }
365+ }
366+ }
367+ QUERY ;
368+ $ postData = ['query ' => $ query ];
304369
305370 $ this ->request ->setPathInfo ('/graphql ' );
306371 $ this ->request ->setMethod ('POST ' );
307372 $ this ->request ->setContent (json_encode ($ postData ));
308373 $ response = $ this ->graphql ->dispatch ($ this ->request );
309374 self ::assertEquals (400 , $ response ->getStatusCode ());
310375 $ output = $ this ->jsonSerializer ->unserialize ($ response ->getContent ());
376+ self ::assertArrayHasKey ('errors ' , $ output );
311377 self ::assertNotEmpty ($ output ['errors ' ]);
378+ self ::assertArrayHasKey ('message ' , $ output ['errors ' ][0 ]);
379+ self ::assertEquals ('Request content type must be application/json ' , $ output ['errors ' ][0 ]['message ' ]);
312380 }
313381}
0 commit comments