Skip to content

Commit 8b9a8c1

Browse files
committed
Test option normalisation
1 parent 720a90c commit 8b9a8c1

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

tests/CorsServiceTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Fruitcake\Cors\Tests;
1414

1515
use Fruitcake\Cors\CorsService;
16+
use Fruitcake\Cors\Exceptions\InvalidOptionException;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class CorsServiceTest extends TestCase
@@ -36,7 +37,6 @@ public function it_can_have_no_options()
3637
{
3738
$service = new CorsService();
3839
$this->assertInstanceOf(CorsService::class, $service);
39-
4040
}
4141

4242
/**
@@ -46,6 +46,73 @@ public function it_can_have_empty_options()
4646
{
4747
$service = new CorsService([]);
4848
$this->assertInstanceOf(CorsService::class, $service);
49+
}
50+
51+
/**
52+
* @test
53+
*/
54+
public function it_throws_exception_on_invalid_exposed_headers()
55+
{
56+
$this->expectException(InvalidOptionException::class);
57+
58+
$service = new CorsService(['exposedHeaders' => true]);
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function it_throws_exception_on_invalid_origins_array()
65+
{
66+
$this->expectException(InvalidOptionException::class);
67+
68+
$service = new CorsService(['allowedOrigins' => 'string']);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function it_normalizes_wildcard_options()
75+
{
76+
$origins = ['*'];
77+
78+
$service = new CorsService(['allowedOrigins' => $origins]);
79+
$this->assertInstanceOf(CorsService::class, $service);
80+
81+
$this->assertEquals(true, $this->getOptionsFromService($service)['allowedOrigins']);
82+
}
83+
84+
/**
85+
* @test
86+
*/
87+
public function it_converts_origin_patterns()
88+
{
89+
$service = new CorsService(['allowedOrigins' => ['*.mydomain.com']]);
90+
$this->assertInstanceOf(CorsService::class, $service);
91+
92+
$patterns = $this->getOptionsFromService($service)['allowedOriginsPatterns'];
93+
$this->assertEquals(['#^.*\.mydomain\.com\z#u'], $patterns);
94+
}
95+
96+
/**
97+
* @test
98+
*/
99+
public function it_normalizes_underscore_options()
100+
{
101+
$origins = ['localhost'];
102+
103+
$service = new CorsService(['allowed_origins' => $origins]);
104+
$this->assertInstanceOf(CorsService::class, $service);
105+
106+
$this->assertEquals($origins, $this->getOptionsFromService($service)['allowedOrigins']);
107+
}
108+
109+
private function getOptionsFromService(CorsService $service)
110+
{
111+
$reflected = new \ReflectionClass($service);
112+
113+
$property = $reflected->getProperty('options');
114+
$property->setAccessible(true);
49115

116+
return $property->getValue($service);
50117
}
51118
}

0 commit comments

Comments
 (0)