Skip to content

Commit

Permalink
Merge pull request #13 from boesing/bugfix/issue-12
Browse files Browse the repository at this point in the history
  • Loading branch information
boesing committed Oct 31, 2020
2 parents e442678 + 10e16e7 commit b8d601d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#13](https://github.com/mezzio/mezzio-cors/pull/13) Added missing default value for `allowed_max_age` which fixes [#12](https://github.com/mezzio/mezzio-cors/issues/12)

## 1.0.1 - 2020-09-02

Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/AbstractConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractConfiguration implements ConfigurationInterface
protected $allowedHeaders = [];

/** @var string */
protected $allowedMaxAge = '';
protected $allowedMaxAge = ConfigurationInterface::PREFLIGHT_CACHE_DISABLED;

/** @var bool */
protected $credentialsAllowed = false;
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface ConfigurationInterface
*/
public const CONFIGURATION_IDENTIFIER = 'expressive.cors';
public const ANY_ORIGIN = '*';
public const PREFLIGHT_CACHE_DISABLED = '-1';

/**
* Should return all allowed methods, the requested path can handle.
Expand All @@ -27,7 +28,7 @@ public function allowedMethods(): array;
public function allowedHeaders(): array;

/**
* Should return the maximum age, the response may be cached by a client.
* Should return the maximum age, the preflight response may be cached by a client.
*/
public function allowedMaxAge(): string;

Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/RouteConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function mergeWithConfiguration(ConfigurationInterface $configuration): R
$instance->setCredentialsAllowed($configuration->credentialsAllowed());
}

if (! $instance->allowedMaxAge()) {
if ($instance->allowedMaxAge() === ConfigurationInterface::PREFLIGHT_CACHE_DISABLED) {
$instance->setAllowedMaxAge($configuration->allowedMaxAge());
}

Expand Down
18 changes: 18 additions & 0 deletions test/Configuration/ProjectConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mezzio\CorsTest\Configuration;

use Mezzio\Cors\Configuration\ConfigurationInterface;
use Mezzio\Cors\Configuration\Exception\InvalidConfigurationException;
use Mezzio\Cors\Configuration\ProjectConfiguration;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -54,4 +55,21 @@ public function testWillThrowExceptionOnUnknownParameter(): void
$this->expectException(InvalidConfigurationException::class);
new ProjectConfiguration(['foo' => 'bar']);
}

public function testWillDisablePreflightCacheWhenAllowedMaxAgeIsNotConfigured(): void
{
$config = new ProjectConfiguration([]);
$this->assertSame(ConfigurationInterface::PREFLIGHT_CACHE_DISABLED, $config->allowedMaxAge());
}

public function testWillInstantiateProjectConfiguration(): void
{
$instance = new ProjectConfiguration([]);
self::assertEquals(ConfigurationInterface::PREFLIGHT_CACHE_DISABLED, $instance->allowedMaxAge());
self::assertEmpty($instance->allowedMethods());
self::assertEmpty($instance->allowedOrigins());
self::assertEmpty($instance->allowedHeaders());
self::assertEmpty($instance->exposedHeaders());
self::assertFalse($instance->credentialsAllowed());
}
}
3 changes: 2 additions & 1 deletion test/Configuration/RouteConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mezzio\CorsTest\Configuration;

use Mezzio\Cors\Configuration\ConfigurationInterface;
use Mezzio\Cors\Configuration\RouteConfigurationFactory;
use PHPUnit\Framework\TestCase;

Expand All @@ -24,7 +25,7 @@ public function testWillInstantiateRouteConfiguration(): void
{
$factory = $this->factory;
$instance = $factory([]);
self::assertEmpty($instance->allowedMaxAge());
self::assertEquals(ConfigurationInterface::PREFLIGHT_CACHE_DISABLED, $instance->allowedMaxAge());
self::assertEmpty($instance->allowedMethods());
self::assertEmpty($instance->allowedOrigins());
self::assertEmpty($instance->allowedHeaders());
Expand Down
6 changes: 6 additions & 0 deletions test/Configuration/RouteConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,10 @@ public function testWontMergeItself(): void

$this->assertSame($routeConfiguration, $routeConfiguration);
}

public function testWillDisablePreflightCacheWhenAllowedMaxAgeIsNotConfigured(): void
{
$config = new ProjectConfiguration([]);
$this->assertSame(ConfigurationInterface::PREFLIGHT_CACHE_DISABLED, $config->allowedMaxAge());
}
}

0 comments on commit b8d601d

Please sign in to comment.