Skip to content

Commit

Permalink
cors
Browse files Browse the repository at this point in the history
  • Loading branch information
daycry committed Jun 29, 2023
1 parent 04a7e77 commit d6d0619
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 32 deletions.
10 changes: 0 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@
"daycry/cronjob": "^2.0",
"daycry/settings": "^1"
},
"repositories": [
{
"type": "vcs",
"url" : "[email protected]:v3/ImaginaDEVOPS/MEDIAPRO%20-%20Equipo%20Desarrollo/AzureOauthServer"
},
{
"type": "git",
"url": "https://github.com/abraham/phpunit-testlistener-vcr"
}
],
"require-dev":
{
"codeigniter4/devkit": "^1",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Config/RestFul.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class RestFul extends BaseConfig
* @var boolean
*/
public $supportsCredentials = false;

/**
* --------------------------------------------------------------------------
* Enable block Invalid Attempts
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

/** @var \CodeIgniter\Router\RouteCollection $routes */

$routes->options('(:any)', '', ['filter' => 'cors']);
$routes->options('(:any)', '', ['filter' => 'cors']);
5 changes: 2 additions & 3 deletions src/Filters/CorsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -55,4 +54,4 @@ public function after(RequestInterface $request, ResponseInterface $response, $a

return $response;
}
}
}
4 changes: 2 additions & 2 deletions src/Libraries/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(?RestFul $config = null)
}

/**
* @param array|CorsInputOptions $options
* @param RestFul $config
*/
public function setOptions(RestFul $config): void
{
Expand Down Expand Up @@ -257,4 +257,4 @@ public function varyHeader(ResponseInterface $response, string $header): Respons

return $response;
}
}
}
2 changes: 1 addition & 1 deletion src/Validators/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Daycry\RestFul\Validators;

use Config\Services;
use Daycry\RestFul\Config\Services;
use CodeIgniter\HTTP\ResponseInterface;

class Cors
Expand Down
8 changes: 4 additions & 4 deletions tests/Filters/CorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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')
Expand All @@ -64,7 +64,7 @@ public function testCorsError(): void
$result->assertHeaderMissing('Access-Control-Allow-Credentials');
}



protected function tearDown(): void
{
Expand Down
19 changes: 9 additions & 10 deletions tests/Validators/CorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ protected function request()
return new Request(Factories::config('App'));
}

/** @var Response */
protected function response()
{
return new Response(Factories::config('App'));
Expand Down Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -82,22 +81,22 @@ 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()
->withMethod('OPTIONS')
->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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -311,4 +310,4 @@ protected function tearDown(): void
{
parent::tearDown();
}
}
}

0 comments on commit d6d0619

Please sign in to comment.