From d6d0619df50b82254fd4bda702f34c5564888e72 Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Thu, 29 Jun 2023 17:40:35 +0200 Subject: [PATCH] cors --- composer.json | 10 ---------- phpstan.neon.dist | 2 ++ src/Config/RestFul.php | 2 +- src/Config/Routes.php | 2 +- src/Filters/CorsFilter.php | 5 ++--- src/Libraries/Cors.php | 4 ++-- src/Validators/Cors.php | 2 +- tests/Filters/CorsTest.php | 8 ++++---- tests/Validators/CorsTest.php | 19 +++++++++---------- 9 files changed, 22 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index babcd96..c332983 100644 --- a/composer.json +++ b/composer.json @@ -21,16 +21,6 @@ "daycry/cronjob": "^2.0", "daycry/settings": "^1" }, - "repositories": [ - { - "type": "vcs", - "url" : "git@ssh.dev.azure.com:v3/ImaginaDEVOPS/MEDIAPRO%20-%20Equipo%20Desarrollo/AzureOauthServer" - }, - { - "type": "git", - "url": "https://github.com/abraham/phpunit-testlistener-vcr" - } - ], "require-dev": { "codeigniter4/devkit": "^1", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 928aa0f..90bb633 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -25,6 +25,8 @@ parameters: - '#Call to deprecated method getMethod\(\) of class CodeIgniter\\HTTP\\Request:#' - '#Call to deprecated method getStatusCode\(\) of class CodeIgniter\\HTTP\\ResponseInterface:#' - '#Negated boolean expression is always true.#' + - '#class CodeIgniter\\HTTP\\Request constructor expects Config\\App#' + - '#class CodeIgniter\\HTTP\\Response constructor expects Config\\App#' universalObjectCratesClasses: - CodeIgniter\Entity - CodeIgniter\Entity\Entity diff --git a/src/Config/RestFul.php b/src/Config/RestFul.php index 2d76cc2..5d4acbf 100644 --- a/src/Config/RestFul.php +++ b/src/Config/RestFul.php @@ -254,7 +254,7 @@ class RestFul extends BaseConfig * @var boolean */ public $supportsCredentials = false; - + /** * -------------------------------------------------------------------------- * Enable block Invalid Attempts diff --git a/src/Config/Routes.php b/src/Config/Routes.php index a4fc38d..1372601 100644 --- a/src/Config/Routes.php +++ b/src/Config/Routes.php @@ -2,4 +2,4 @@ /** @var \CodeIgniter\Router\RouteCollection $routes */ -$routes->options('(:any)', '', ['filter' => 'cors']); \ No newline at end of file +$routes->options('(:any)', '', ['filter' => 'cors']); diff --git a/src/Filters/CorsFilter.php b/src/Filters/CorsFilter.php index a2745cb..7274901 100644 --- a/src/Filters/CorsFilter.php +++ b/src/Filters/CorsFilter.php @@ -10,14 +10,13 @@ class CorsFilter implements FilterInterface { /** - * @var \Fluent\Cors\ServiceCors $cors + * @var \Daycry\RestFul\Libraries\Cors $cors */ protected $cors; /** * Constructor. * - * @param array $options * @return void */ public function __construct() @@ -55,4 +54,4 @@ public function after(RequestInterface $request, ResponseInterface $response, $a return $response; } -} \ No newline at end of file +} diff --git a/src/Libraries/Cors.php b/src/Libraries/Cors.php index 823d8d9..8abcac3 100644 --- a/src/Libraries/Cors.php +++ b/src/Libraries/Cors.php @@ -42,7 +42,7 @@ public function __construct(?RestFul $config = null) } /** - * @param array|CorsInputOptions $options + * @param RestFul $config */ public function setOptions(RestFul $config): void { @@ -257,4 +257,4 @@ public function varyHeader(ResponseInterface $response, string $header): Respons return $response; } -} \ No newline at end of file +} diff --git a/src/Validators/Cors.php b/src/Validators/Cors.php index 3376dc2..1a0ecaf 100644 --- a/src/Validators/Cors.php +++ b/src/Validators/Cors.php @@ -2,7 +2,7 @@ namespace Daycry\RestFul\Validators; -use Config\Services; +use Daycry\RestFul\Config\Services; use CodeIgniter\HTTP\ResponseInterface; class Cors diff --git a/tests/Filters/CorsTest.php b/tests/Filters/CorsTest.php index d4b82d7..1360fcb 100644 --- a/tests/Filters/CorsTest.php +++ b/tests/Filters/CorsTest.php @@ -32,7 +32,7 @@ public function testCorsSuccess(): void $request = service('request'); $request->setHeader('Origin', 'https://test-cors1.local'); - + Services::injectMock('request', $request); $result = $this->call('get', 'example-filter'); @@ -48,13 +48,13 @@ public function testCorsError(): void $request = service('request'); $request->setHeader('Origin', 'https://test-cors1.local'); - + Services::injectMock('request', $request); $result = $this->call('get', 'example-filter'); $response = Services::response(); - + $this->assertNotEquals( 'https://test-cors1.local', $response->getHeaderLine('Access-Control-Allow-Origin') @@ -64,7 +64,7 @@ public function testCorsError(): void $result->assertHeaderMissing('Access-Control-Allow-Credentials'); } - + protected function tearDown(): void { diff --git a/tests/Validators/CorsTest.php b/tests/Validators/CorsTest.php index 10fda2f..efa300f 100644 --- a/tests/Validators/CorsTest.php +++ b/tests/Validators/CorsTest.php @@ -32,7 +32,6 @@ protected function request() return new Request(Factories::config('App')); } - /** @var Response */ protected function response() { return new Response(Factories::config('App')); @@ -62,7 +61,7 @@ public function testIsPreflightRequest() ->withMethod('OPTIONS') ->setHeader('Access-Control-Request-Method', 'GET'); - $cors = new Cors(config('RestFul')); + $cors = new Cors(config('RestFul')); $this->assertTrue($cors->isPreflightRequest($request)); } @@ -72,7 +71,7 @@ public function testIsNotPreflightRequest() $request = $this->request()->withMethod('GET') ->setHeader('Access-Control-Request-Method', 'GET'); - $cors = new Cors(config('RestFul')); + $cors = new Cors(config('RestFul')); $this->assertFalse($cors->isPreflightRequest($request)); } @@ -82,13 +81,13 @@ public function testVaryHeader() $response = $this->response() ->setHeader('Vary', 'Access-Control-Request-Method'); - $cors = new Cors(config('RestFul')); + $cors = new Cors(config('RestFul')); $vary = $cors->varyHeader($response, 'Access-Control-Request-Method'); $this->assertEquals($response->getHeaderLine('Vary'), $vary->getHeaderLine('Vary')); } - + public function testHandlePreflightRequest() { $request = $this->request() @@ -96,8 +95,8 @@ public function testHandlePreflightRequest() ->setHeader('Origin', 'http://foobar.com') ->setHeader('Access-Control-Request-Method', 'GET') ->setHeader('Access-Control-Request-Headers', 'X-CSRF-TOKEN'); - - $cors = new Cors(config('RestFul')); + + $cors = new Cors(config('RestFul')); $expected = $cors->handlePreflightRequest($request); @@ -123,7 +122,7 @@ public function testHandleRequest() $response = $this->response() ->setHeader('Access-Control-Allow-Origin', $request->getHeaderLine('Origin')); - $cors = new Cors(config('RestFul')); + $cors = new Cors(config('RestFul')); $expected = $cors->addPreflightRequestHeaders($response, $request); @@ -242,7 +241,7 @@ public function testHandlePreflightRequestWithExposeHeadersNotSet() ->setHeader('Origin', 'http://foo.com') ->setHeader('Access-Control-Request-Headers', 'X-CSRF-TOKEN'); - $cors = new Cors(config('RestFul')); + $cors = new Cors(config('RestFul')); $expeted = $cors->addPreflightRequestHeaders($this->response(), $request); @@ -311,4 +310,4 @@ protected function tearDown(): void { parent::tearDown(); } -} \ No newline at end of file +}