|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2018 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
|
24 | 24 | */
|
25 | 25 | class GraphQlControllerTest extends \Magento\TestFramework\Indexer\TestCase
|
26 | 26 | {
|
27 |
| - const CONTENT_TYPE = 'application/json'; |
28 |
| - |
29 | 27 | /** @var \Magento\Framework\ObjectManagerInterface */
|
30 | 28 | private $objectManager;
|
31 | 29 |
|
@@ -175,7 +173,7 @@ public function testDispatchGetWithParameterizedVariables() : void
|
175 | 173 | id
|
176 | 174 | name
|
177 | 175 | sku
|
178 |
| - } |
| 176 | + } |
179 | 177 | }
|
180 | 178 | }
|
181 | 179 | QUERY;
|
@@ -223,12 +221,12 @@ public function testError() : void
|
223 | 221 | }
|
224 | 222 | ])
|
225 | 223 | {
|
226 |
| - items{ |
| 224 | + items{ |
227 | 225 | attribute_code
|
228 | 226 | attribute_type
|
229 | 227 | entity_type
|
230 |
| - } |
231 |
| - } |
| 228 | + } |
| 229 | + } |
232 | 230 | }
|
233 | 231 | QUERY;
|
234 | 232 |
|
@@ -265,4 +263,119 @@ public function testError() : void
|
265 | 263 | }
|
266 | 264 | }
|
267 | 265 | }
|
| 266 | + |
| 267 | + public function testDispatchOptions(): void |
| 268 | + { |
| 269 | + $this->request->setPathInfo('/graphql'); |
| 270 | + $this->request->setMethod('OPTIONS'); |
| 271 | + $response = $this->graphql->dispatch($this->request); |
| 272 | + self::assertEquals(200, $response->getStatusCode()); |
| 273 | + self::assertEmpty($response->getContent()); |
| 274 | + } |
| 275 | + |
| 276 | + public function testDispatchGetWithoutQuery(): void |
| 277 | + { |
| 278 | + $this->request->setPathInfo('/graphql'); |
| 279 | + $this->request->setMethod('GET'); |
| 280 | + $response = $this->graphql->dispatch($this->request); |
| 281 | + self::assertEquals(400, $response->getStatusCode()); |
| 282 | + $output = $this->jsonSerializer->unserialize($response->getContent()); |
| 283 | + self::assertArrayHasKey('errors', $output); |
| 284 | + self::assertNotEmpty($output['errors']); |
| 285 | + self::assertArrayHasKey('message', $output['errors'][0]); |
| 286 | + self::assertStringStartsWith('Syntax Error:', $output['errors'][0]['message']); |
| 287 | + } |
| 288 | + |
| 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 |
| 326 | + { |
| 327 | + $query = <<<QUERY |
| 328 | +{ |
| 329 | + products(filter: {sku: {eq: "simple1"}}) { |
| 330 | + items { |
| 331 | + id |
| 332 | + name |
| 333 | + sku |
| 334 | + } |
| 335 | + } |
| 336 | +} |
| 337 | +QUERY; |
| 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]; |
| 369 | + |
| 370 | + $this->request->setPathInfo('/graphql'); |
| 371 | + $this->request->setMethod('POST'); |
| 372 | + $this->request->setContent(json_encode($postData)); |
| 373 | + $response = $this->graphql->dispatch($this->request); |
| 374 | + self::assertEquals(400, $response->getStatusCode()); |
| 375 | + $output = $this->jsonSerializer->unserialize($response->getContent()); |
| 376 | + self::assertArrayHasKey('errors', $output); |
| 377 | + 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']); |
| 380 | + } |
268 | 381 | }
|
0 commit comments