Skip to content

Commit 53fb798

Browse files
committed
Prevented users from using "GET" method
- Throwing exception if client is constructed with request method other than "POST" - Formatted code - Improved code coverage
1 parent d686e0f commit 53fb798

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

src/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GraphQL;
44

55
use GraphQL\Exception\QueryError;
6+
use GraphQL\Exception\MethodNotSupportedException;
67
use GraphQL\QueryBuilder\QueryBuilderInterface;
78
use GraphQL\Util\GuzzleAdapter;
89
use GuzzleHttp\Exception\ClientException;
@@ -70,6 +71,9 @@ public function __construct(
7071
$this->endpointUrl = $endpointUrl;
7172
$this->httpClient = $httpClient ?? new GuzzleAdapter(new \GuzzleHttp\Client($httpOptions));
7273
$this->httpHeaders = $headers;
74+
if ($requestMethod !== 'POST') {
75+
throw new MethodNotSupportedException($requestMethod);
76+
}
7377
$this->requestMethod = $requestMethod;
7478
}
7579

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace GraphQL\Exception;
4+
5+
use RunTimeException;
6+
7+
/**
8+
* Class MethodNotSupportedException
9+
*
10+
* @package GraphQL\Exception
11+
*/
12+
class MethodNotSupportedException extends RunTimeException
13+
{
14+
public function __construct($requestMethod)
15+
{
16+
parent::__construct("Method \"$requestMethod\" is currently unsupported by client.");
17+
}
18+
}

src/FieldTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function constructSelectionSet(): string
4242
{
4343
if (empty($this->selectionSet)) {
4444
return '';
45-
}
45+
}
4646

4747
$attributesString = " {" . PHP_EOL;
4848
$first = true;

tests/ClientTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GraphQL\Client;
66
use GraphQL\Exception\QueryError;
7+
use GraphQL\Exception\MethodNotSupportedException;
78
use GraphQL\QueryBuilder\QueryBuilder;
89
use GraphQL\RawObject;
910
use GuzzleHttp\Exception\ClientException;
@@ -47,6 +48,8 @@ protected function setUp(): void
4748
/**
4849
* @covers \GraphQL\Client::__construct
4950
* @covers \GraphQL\Client::runRawQuery
51+
* @covers \GraphQL\Util\GuzzleAdapter::__construct
52+
* @covers \GraphQL\Util\GuzzleAdapter::sendRequest
5053
*/
5154
public function testConstructClient()
5255
{
@@ -74,9 +77,6 @@ public function testConstructClient()
7477
$client = new Client('', ['Authorization' => 'Basic xyz'], ['handler' => $handler, 'headers' => [ 'Authorization' => 'Basic zyx', 'User-Agent' => 'test' ]]);
7578
$client->runRawQuery('query_string');
7679

77-
$client = new Client('', ['Authorization' => 'Basic xyz'], ['handler' => $handler, 'headers' => [ 'Authorization' => 'Basic zyx', 'User-Agent' => 'test' ]], null, 'GET');
78-
$client->runRawQuery('query_string');
79-
8080
/** @var Request $firstRequest */
8181
$firstRequest = $container[0]['request'];
8282
$this->assertEquals('{"query":"query_string","variables":{}}', $firstRequest->getBody()->getContents());
@@ -100,10 +100,16 @@ public function testConstructClient()
100100
$this->assertNotEmpty($fourthRequest->getHeader('User-Agent'));
101101
$this->assertEquals(['Basic zyx'], $fourthRequest->getHeader('Authorization'));
102102
$this->assertEquals(['test'], $fourthRequest->getHeader('User-Agent'));
103+
}
103104

104-
/** @var Request $fifthRequest */
105-
$fifthRequest = $container[4]['request'];
106-
$this->assertSame('GET', $fifthRequest->getMethod());
105+
/**
106+
* @covers \GraphQL\Client::__construct
107+
* @covers \GraphQL\Exception\MethodNotSupportedException
108+
*/
109+
public function testConstructClientWithGetRequestMethod()
110+
{
111+
$this->expectException(MethodNotSupportedException::class);
112+
$client = new Client('', [], [], null, 'GET');
107113
}
108114

109115
/**

tests/QueryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testEmptyArguments(Query $query)
4848

4949
/**
5050
* @covers \GraphQL\Query::__toString
51+
* @covers FieldTrait::constructSelectionSet
5152
*/
5253
public function testQueryWithoutFieldName()
5354
{

0 commit comments

Comments
 (0)