diff --git a/CHANGELOG.md b/CHANGELOG.md index 9617970..c33fbe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## 1.0 +## 1.1-dev (Unreleased) + +### Added + - setOptions() method to overwrite or set the options later. + +## 1.0.0 (2020-02-19) ### Changed since split from asm89/stack-cors diff --git a/composer.json b/composer.json index 264e834..650bf0c 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ }, "extra": { "branch-alias": { - "dev-main": "1.0-dev" + "dev-main": "1.1-dev" } } } diff --git a/tests/CorsServiceTest.php b/tests/CorsServiceTest.php index c3ee5a8..fb2a14c 100644 --- a/tests/CorsServiceTest.php +++ b/tests/CorsServiceTest.php @@ -62,6 +62,75 @@ public function itCanHaveOptions(): void $this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']); } + /** + * @test + */ + public function itCanSetOptions(): void + { + $service = new CorsService(); + $normalized = $this->getOptionsFromService($service); + $this->assertEquals([], $normalized['allowedOrigins']); + + $this->assertInstanceOf(CorsService::class, $service); + + $options = [ + 'allowedOrigins' => ['localhost'], + 'allowedOriginsPatterns' => ['/something/'], + 'allowedHeaders' => ['x-custom'], + 'allowedMethods' => ['PUT'], + 'maxAge' => 684, + 'supportsCredentials' => true, + 'exposedHeaders' => ['x-custom-2'], + ]; + + $service->setOptions($options); + + $normalized = $this->getOptionsFromService($service); + + $this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']); + $this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']); + $this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']); + $this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']); + $this->assertEquals($options['maxAge'], $normalized['maxAge']); + $this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']); + $this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']); + } + + /** + * @test + */ + public function itCanOverwriteSetOptions(): void + { + $service = new CorsService(['allowedOrigins' => ['example.com']]); + $normalized = $this->getOptionsFromService($service); + + $this->assertEquals(['example.com'], $normalized['allowedOrigins']); + + $this->assertInstanceOf(CorsService::class, $service); + + $options = [ + 'allowedOrigins' => ['localhost'], + 'allowedOriginsPatterns' => ['/something/'], + 'allowedHeaders' => ['x-custom'], + 'allowedMethods' => ['PUT'], + 'maxAge' => 684, + 'supportsCredentials' => true, + 'exposedHeaders' => ['x-custom-2'], + ]; + + $service->setOptions($options); + + $normalized = $this->getOptionsFromService($service); + + $this->assertEquals($options['allowedOrigins'], $normalized['allowedOrigins']); + $this->assertEquals($options['allowedOriginsPatterns'], $normalized['allowedOriginsPatterns']); + $this->assertEquals($options['allowedHeaders'], $normalized['allowedHeaders']); + $this->assertEquals($options['allowedMethods'], $normalized['allowedMethods']); + $this->assertEquals($options['maxAge'], $normalized['maxAge']); + $this->assertEquals($options['supportsCredentials'], $normalized['supportsCredentials']); + $this->assertEquals($options['exposedHeaders'], $normalized['exposedHeaders']); + } + /** * @test */