Skip to content

Commit fc1b5ad

Browse files
author
Serhii Balko
committed
Merge remote-tracking branch 'origin/MC-40920' into 2.4-develop-pr53
2 parents fbd2fc6 + 1035953 commit fc1b5ad

File tree

2 files changed

+114
-15
lines changed

2 files changed

+114
-15
lines changed

app/code/Magento/GraphQl/Controller/HttpRequestValidator/HttpVerbValidator.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@ public function validate(HttpRequestInterface $request) : void
3131
/** @var Http $request */
3232
if (false === $request->isPost()) {
3333
$query = $request->getParam('query', '');
34-
$operationType = null;
35-
$queryAst = \GraphQL\Language\Parser::parse(new \GraphQL\Language\Source($query ?: '', 'GraphQL'));
36-
\GraphQL\Language\Visitor::visit(
37-
$queryAst,
38-
[
39-
'leave' => [
40-
NodeKind::OPERATION_DEFINITION => function (Node $node) use (&$operationType) {
41-
$operationType = $node->operation;
42-
}
34+
if (!empty($query)) {
35+
$operationType = null;
36+
$queryAst = \GraphQL\Language\Parser::parse(new \GraphQL\Language\Source($query ?: '', 'GraphQL'));
37+
\GraphQL\Language\Visitor::visit(
38+
$queryAst,
39+
[
40+
'leave' => [
41+
NodeKind::OPERATION_DEFINITION => function (Node $node) use (&$operationType) {
42+
$operationType = $node->operation;
43+
}
44+
]
4345
]
44-
]
45-
);
46-
47-
if (strtolower($operationType) === 'mutation') {
48-
throw new GraphQlInputException(
49-
new \Magento\Framework\Phrase('Mutation requests allowed only for POST requests')
5046
);
47+
48+
if (strtolower($operationType) === 'mutation') {
49+
throw new GraphQlInputException(
50+
new \Magento\Framework\Phrase('Mutation requests allowed only for POST requests')
51+
);
52+
}
5153
}
5254
}
5355
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Test\Unit\Controller\HttpRequestValidator;
9+
10+
use Magento\Framework\App\HttpRequestInterface;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\GraphQl\Controller\HttpRequestValidator\HttpVerbValidator;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test HttpVerbValidator
18+
*/
19+
class HttpVerbValidatorTest extends TestCase
20+
{
21+
/**
22+
* @var HttpVerbValidator|MockObject
23+
*/
24+
private $httpVerbValidator;
25+
26+
/**
27+
* @var HttpRequestInterface|MockObject
28+
*/
29+
private $requestMock;
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
protected function setup(): void
35+
{
36+
$objectManager = new ObjectManager($this);
37+
$this->requestMock = $this->getMockBuilder(HttpRequestInterface::class)
38+
->disableOriginalConstructor()
39+
->onlyMethods(
40+
[
41+
'isPost',
42+
]
43+
)->addMethods(
44+
[
45+
'getParam',
46+
]
47+
)
48+
->getMockForAbstractClass();
49+
50+
$this->httpVerbValidator = $objectManager->getObject(
51+
HttpVerbValidator::class
52+
);
53+
}
54+
55+
/**
56+
* Test for validate method
57+
*
58+
* @param string $query
59+
* @param bool $needException
60+
* @dataProvider validateDataProvider
61+
*/
62+
public function testValidate(string $query, bool $needException): void
63+
{
64+
$this->requestMock
65+
->expects($this->once())
66+
->method('isPost')
67+
->willReturn(false);
68+
69+
$this->requestMock
70+
->method('getParam')
71+
->with('query', '')
72+
->willReturn($query);
73+
74+
if ($needException) {
75+
$this->expectExceptionMessage('Syntax Error: Unexpected <EOF>');
76+
}
77+
78+
$this->httpVerbValidator->validate($this->requestMock);
79+
}
80+
81+
/**
82+
* @return array
83+
*/
84+
public function validateDataProvider(): array
85+
{
86+
return [
87+
[
88+
'query' => '',
89+
'needException' => false,
90+
],
91+
[
92+
'query' => ' ',
93+
'needException' => true
94+
],
95+
];
96+
}
97+
}

0 commit comments

Comments
 (0)