Skip to content

Commit

Permalink
Merge pull request #23 from gregorip02/2.x
Browse files Browse the repository at this point in the history
2.x Remove support for base64 queries
  • Loading branch information
gregorip02 authored Apr 18, 2021
2 parents eea6743 + e6239e6 commit f186a43
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/docs export-ignore
/CHANGELOG.md export-ignore
/CONTRIBUTING.md export-ignore
/.php_cs export-ignore
41 changes: 12 additions & 29 deletions src/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Restql;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Restql\Traits\HasConfigService;
Expand Down Expand Up @@ -45,21 +46,9 @@ public static function filter(Request $request): Collection
*/
protected function decode(): Collection
{
if (strlen($this->getQueryParamName()) && $this->hasParam()) {
/// If the user decides to give a value to the attribute in the
/// "query_param" setting, then it will be assumed that
/// the application accepts base64 encoded queries
/// sent as parameters in the HTTP request.

/// Similarly, when this happens, the value of the
/// query must be compulsorily encoded in
/// base64.
return $this->decodeParam();
}
$resources = $this->hasParam() ? $this->getQueryParamValue() : $this->request->all();

/// By default, restql accepts queries that come in the body
/// of the HTTP request and are not base64 encoded.
return $this->getAllowedResources($this->request->all());
return $this->getAllowedResources($resources);
}

/**
Expand All @@ -77,13 +66,19 @@ protected function hasParam(): bool
/**
* Get the request param value.
*
* @return string
* @return array
*/
protected function getQueryParamValue(): string
protected function getQueryParamValue(): array
{
$paramName = $this->getQueryParamName();

return $this->request->get($paramName);
$paramValue = $this->request->input($paramName);

if (! is_array($paramValue)) {
throw new Exception(sprintf('The value of param %s must be array', $paramName));
}

return $paramValue;
}

/**
Expand All @@ -96,18 +91,6 @@ protected function getQueryParamName(): string
return $this->getConfigService()->get('query_param', '');
}

/**
* Decode the parameter.
*
* @return \Illuminate\Support\Collection
*/
protected function decodeParam(): Collection
{
$query = (array) json_decode(base64_decode($this->getQueryParamValue()));

return $this->getAllowedResources($query);
}

/**
* The accepted resources key names (models/resolvers).
*
Expand Down
28 changes: 5 additions & 23 deletions tests/Unit/RequestParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Testing\Unit;

use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase as FrameworkTestCase;
use Restql\RequestParser;
use Testing\TestCase;

class RequestParserTest extends FrameworkTestCase
class RequestParserTest extends TestCase
{
/**
* Fake request body.
Expand All @@ -19,43 +19,25 @@ class RequestParserTest extends FrameworkTestCase
'unknow_schema_definition' => true
];

/**
* Create generic encoded request.
*
* @return \Illuminate\Http\Request
*/
protected static function genericEncodedRequest(): Request
{
$body = base64_encode(json_encode(self::$body));

return new Request([
'query' => $body
]);
}

/**
* Create generic request.
*
* @return \Illuminate\Http\Request
*/
protected static function genericRequest(): Request
{
return new Request(self::$body);
return new Request([
'query' => self::$body
]);
}

/**
* @test Exclude unknow resources.
*/
public function theParserExcludeUnknowResources(): void
{
$encoded = RequestParser::filter(self::genericEncodedRequest());

$decoded = RequestParser::filter(self::genericRequest());

$this->assertFalse($encoded->has('unknow_schema_definition'));
$this->assertFalse($decoded->has('unknow_schema_definition'));

$this->assertTrue($encoded->has('authors'));
$this->assertTrue($decoded->has('authors'));
}
}

0 comments on commit f186a43

Please sign in to comment.