From 4fab4c3a6ff481d4aecca2e15a4ead82da85fe81 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Thu, 11 Jan 2024 11:57:01 +0100 Subject: [PATCH] Support for CORS --- src/Controller/OptionsController.php | 28 ++++++++++++++++ tests/Controller/DataControllerTest.php | 22 +++++++++++++ tests/Controller/GraphControllerTest.php | 22 +++++++++++++ .../Controller/HealthCheckControllerTest.php | 11 +++++++ tests/Controller/ReportControllerTest.php | 33 +++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 src/Controller/OptionsController.php diff --git a/src/Controller/OptionsController.php b/src/Controller/OptionsController.php new file mode 100644 index 00000000..712c6acd --- /dev/null +++ b/src/Controller/OptionsController.php @@ -0,0 +1,28 @@ +headers->set('Access-Control-Allow-Origin', '*'); + $response->headers->set('Access-Control-Allow-Methods', 'GET'); + $response->headers->set('Access-Control-Max-Age', '3600'); + + return $response; + } +} diff --git a/tests/Controller/DataControllerTest.php b/tests/Controller/DataControllerTest.php index 53b85234..da7dec2d 100644 --- a/tests/Controller/DataControllerTest.php +++ b/tests/Controller/DataControllerTest.php @@ -6,6 +6,17 @@ class DataControllerTest extends WebTestCase { + public function testCorsBadgeJson(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/data/badge'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testBadgeJson(): void { $client = static::createClient(); @@ -44,6 +55,17 @@ public function testBadgeJsonNotFound(): void $this->assertEquals('Execution not found', $content['message']); } + public function testCorsBadgeSvg(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/data/badge/svg'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testBadgeSvg(): void { $client = static::createClient(); diff --git a/tests/Controller/GraphControllerTest.php b/tests/Controller/GraphControllerTest.php index 5d7a73e3..16e71104 100644 --- a/tests/Controller/GraphControllerTest.php +++ b/tests/Controller/GraphControllerTest.php @@ -6,6 +6,17 @@ class GraphControllerTest extends WebTestCase { + public function testCorsGraph(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/graph'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testGraph(): void { $client = static::createClient(); @@ -40,6 +51,17 @@ public function testGraph(): void } } + public function testCorsGraphParameters(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/graph/parameters'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testParameters(): void { $client = static::createClient(); diff --git a/tests/Controller/HealthCheckControllerTest.php b/tests/Controller/HealthCheckControllerTest.php index c3b2a433..a41776cb 100644 --- a/tests/Controller/HealthCheckControllerTest.php +++ b/tests/Controller/HealthCheckControllerTest.php @@ -6,6 +6,17 @@ class HealthCheckControllerTest extends WebTestCase { + public function testCorsHealthcheck(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/healthcheck'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testHealthcheck(): void { $client = static::createClient(); diff --git a/tests/Controller/ReportControllerTest.php b/tests/Controller/ReportControllerTest.php index 233cf2b3..9268af8d 100644 --- a/tests/Controller/ReportControllerTest.php +++ b/tests/Controller/ReportControllerTest.php @@ -21,6 +21,17 @@ public static function setUpBeforeClass(): void self::$suiteId = array_key_first($data['suites_data']); } + public function testCorsReports(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/reports'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testReports(): void { $client = static::createClient(); @@ -86,6 +97,17 @@ public function testReportNotFound(): void $this->assertEquals('Execution not found', $content['message']); } + public function testCorsReportID(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/reports/2'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testReportID(): void { $client = static::createClient(); @@ -212,6 +234,17 @@ public function testCompareReportFilterState(): void } } + public function testCorsReportSuite(): void + { + $client = static::createClient(); + $client->request('OPTIONS', '/reports/2/suites/4'); + $response = $client->getResponse(); + $this->assertEquals($response->getStatusCode(), 200); + $this->assertEquals($response->headers->get('access-control-allow-methods'), 'GET'); + $this->assertEquals($response->headers->get('access-control-max-age'), 3600); + $this->assertEquals($response->headers->get('access-control-allow-origin'), '*'); + } + public function testCompareSuite(): void { $client = static::createClient();