|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @link http://github.com/zendframework/zf2 for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ZendTest\Http\Header; |
| 11 | + |
| 12 | +use Zend\Http\Header\ContentSecurityPolicy; |
| 13 | + |
| 14 | +class ContentSecurityPolicyTest extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + public function testContentSecurityPolicyFromStringThrowsExceptionIfImproperHeaderNameUsed() |
| 17 | + { |
| 18 | + $this->setExpectedException('Zend\Http\Header\Exception\InvalidArgumentException'); |
| 19 | + ContentSecurityPolicy::fromString('X-Content-Security-Policy: default-src *;'); |
| 20 | + } |
| 21 | + |
| 22 | + public function testContentSecurityPolicyFromStringParsesDirectivesCorrectly() |
| 23 | + { |
| 24 | + $csp = ContentSecurityPolicy::fromString( |
| 25 | + "Content-Security-Policy: default-src 'none'; script-src 'self'; img-src 'self'; style-src 'self';" |
| 26 | + ); |
| 27 | + $this->assertInstanceOf('Zend\Http\Header\HeaderInterface', $csp); |
| 28 | + $this->assertInstanceOf('Zend\Http\Header\ContentSecurityPolicy', $csp); |
| 29 | + $directives = array('default-src' => "'none'", |
| 30 | + 'script-src' => "'self'", |
| 31 | + 'img-src' => "'self'", |
| 32 | + 'style-src' => "'self'"); |
| 33 | + $this->assertEquals($directives, $csp->getDirectives()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testContentSecurityPolicyGetFieldNameReturnsHeaderName() |
| 37 | + { |
| 38 | + $csp = new ContentSecurityPolicy(); |
| 39 | + $this->assertEquals('Content-Security-Policy', $csp->getFieldName()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testContentSecurityPolicyToStringReturnsHeaderFormattedString() |
| 43 | + { |
| 44 | + $csp = ContentSecurityPolicy::fromString( |
| 45 | + "Content-Security-Policy: default-src 'none'; img-src 'self' https://*.gravatar.com;" |
| 46 | + ); |
| 47 | + $this->assertInstanceOf('Zend\Http\Header\HeaderInterface', $csp); |
| 48 | + $this->assertInstanceOf('Zend\Http\Header\ContentSecurityPolicy', $csp); |
| 49 | + $this->assertEquals( |
| 50 | + "Content-Security-Policy: default-src 'none'; img-src 'self' https://*.gravatar.com;", |
| 51 | + $csp->toString() |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testContentSecurityPolicySetDirective() |
| 56 | + { |
| 57 | + $csp = new ContentSecurityPolicy(); |
| 58 | + $csp->setDirective('default-src', array('https://*.google.com', 'http://foo.com')) |
| 59 | + ->setDirective('img-src', array("'self'")) |
| 60 | + ->setDirective('script-src', array('https://*.googleapis.com', 'https://*.bar.com')); |
| 61 | + $header = "Content-Security-Policy: default-src https://*.google.com http://foo.com; " |
| 62 | + . "img-src 'self'; script-src https://*.googleapis.com https://*.bar.com;"; |
| 63 | + $this->assertEquals($header, $csp->toString()); |
| 64 | + } |
| 65 | + |
| 66 | + public function testContentSecurityPolicySetDirectiveWithEmptySourcesDefaultsToNone() |
| 67 | + { |
| 68 | + $csp = new ContentSecurityPolicy(); |
| 69 | + $csp->setDirective('default-src', array("'self'")) |
| 70 | + ->setDirective('img-src', array('*')) |
| 71 | + ->setDirective('script-src', array()); |
| 72 | + $this->assertEquals( |
| 73 | + "Content-Security-Policy: default-src 'self'; img-src *; script-src 'none';", |
| 74 | + $csp->toString() |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function testContentSecurityPolicySetDirectiveThrowsExceptionIfInvalidDirectiveNameGiven() |
| 79 | + { |
| 80 | + $this->setExpectedException('Zend\Http\Header\Exception\InvalidArgumentException'); |
| 81 | + $csp = new ContentSecurityPolicy(); |
| 82 | + $csp->setDirective('foo', array()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testContentSecurityPolicyGetFieldValueReturnsProperValue() |
| 86 | + { |
| 87 | + $csp = new ContentSecurityPolicy(); |
| 88 | + $csp->setDirective('default-src', array("'self'")) |
| 89 | + ->setDirective('img-src', array('https://*.github.com')); |
| 90 | + $this->assertEquals("default-src 'self'; img-src https://*.github.com;", $csp->getFieldValue()); |
| 91 | + } |
| 92 | +} |
0 commit comments