1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace ZfrCorsTest\Options; |
20
|
|
|
|
21
|
|
|
use PHPUnit\Framework\TestCase as TestCase; |
22
|
|
|
use ZfrCors\Options\CorsOptions; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Integration tests for {@see \ZfrCors\Service\CorsService} |
26
|
|
|
* |
27
|
|
|
* @author Michaël Gallego <[email protected]> |
28
|
|
|
* |
29
|
|
|
* @covers \ZfrCors\Options\CorsOptions |
30
|
|
|
* @group Coverage |
31
|
|
|
*/ |
32
|
|
|
class CorsOptionsTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
public function testCorsOptionsAreSecuredByDefault() |
35
|
|
|
{ |
36
|
|
|
$options = new CorsOptions(); |
37
|
|
|
|
38
|
|
|
$this->assertEquals([], $options->getAllowedOrigins(), 'No origin are allowed'); |
39
|
|
|
$this->assertEquals([], $options->getAllowedMethods(), 'No methods are allowed'); |
40
|
|
|
$this->assertEquals([], $options->getAllowedHeaders(), 'No headers are allowed'); |
41
|
|
|
$this->assertEquals(0, $options->getMaxAge(), 'Preflight request cannot be cached'); |
42
|
|
|
$this->assertEquals([], $options->getExposedHeaders(), 'No headers are exposed to the browser'); |
43
|
|
|
$this->assertFalse($options->getAllowedCredentials(), 'Cookies are not allowed'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCanModifyOptions() |
47
|
|
|
{ |
48
|
|
|
$options = new CorsOptions(); |
49
|
|
|
|
50
|
|
|
$options->setAllowedOrigins(['http://example1.com', 'http://example2.com']); |
51
|
|
|
$this->assertEquals(['http://example1.com', 'http://example2.com'], $options->getAllowedOrigins()); |
52
|
|
|
|
53
|
|
|
$options->setAllowedMethods(['POST', 'GET']); |
54
|
|
|
$this->assertEquals(['POST', 'GET'], $options->getAllowedMethods()); |
55
|
|
|
|
56
|
|
|
$options->setAllowedHeaders(['Content-Type']); |
57
|
|
|
$this->assertEquals(['Content-Type'], $options->getAllowedHeaders()); |
58
|
|
|
|
59
|
|
|
$options->setMaxAge(30); |
60
|
|
|
$this->assertEquals(30, $options->getMaxAge()); |
61
|
|
|
|
62
|
|
|
$options->setExposedHeaders(['Location', 'X-Custom-Header']); |
63
|
|
|
$this->assertEquals(['Location', 'X-Custom-Header'], $options->getExposedHeaders()); |
64
|
|
|
|
65
|
|
|
$options->setAllowedCredentials(true); |
66
|
|
|
$this->assertTrue($options->getAllowedCredentials()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testNormalizeHttpMethods() |
70
|
|
|
{ |
71
|
|
|
$options = new CorsOptions(); |
72
|
|
|
|
73
|
|
|
$options->setAllowedMethods(['post', 'GeT']); |
74
|
|
|
$this->assertEquals(['POST', 'GET'], $options->getAllowedMethods()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|