8
8
namespace ZendTest \Http \Header ;
9
9
10
10
use PHPUnit \Framework \TestCase ;
11
+ use Zend \Http \Exception \RuntimeException ;
11
12
use Zend \Http \Header \ContentSecurityPolicy ;
12
13
use Zend \Http \Header \Exception \InvalidArgumentException ;
14
+ use Zend \Http \Header \GenericHeader ;
13
15
use Zend \Http \Header \HeaderInterface ;
16
+ use Zend \Http \Header \MultipleHeaderInterface ;
17
+ use Zend \Http \Headers ;
14
18
15
19
class ContentSecurityPolicyTest extends TestCase
16
20
{
@@ -25,6 +29,7 @@ public function testContentSecurityPolicyFromStringParsesDirectivesCorrectly()
25
29
$ csp = ContentSecurityPolicy::fromString (
26
30
"Content-Security-Policy: default-src 'none'; script-src 'self'; img-src 'self'; style-src 'self'; "
27
31
);
32
+ $ this ->assertInstanceOf (MultipleHeaderInterface::class, $ csp );
28
33
$ this ->assertInstanceOf (HeaderInterface::class, $ csp );
29
34
$ this ->assertInstanceOf (ContentSecurityPolicy::class, $ csp );
30
35
$ directives = [
@@ -139,4 +144,47 @@ public function testContentSecurityPolicySetDirectiveWithEmptyReportUriRemovesEx
139
144
$ csp ->toString ()
140
145
);
141
146
}
147
+
148
+ public function testToStringMultipleHeaders ()
149
+ {
150
+ $ csp = new ContentSecurityPolicy ();
151
+ $ csp ->setDirective ('default-src ' , ["'self' " ]);
152
+
153
+ $ additional = new ContentSecurityPolicy ();
154
+ $ additional ->setDirective ('img-src ' , ['https://*.github.com ' ]);
155
+
156
+ self ::assertSame (
157
+ "Content-Security-Policy: default-src 'self'; \r\n"
158
+ . "Content-Security-Policy: img-src https://*.github.com; \r\n" ,
159
+ $ csp ->toStringMultipleHeaders ([$ additional ])
160
+ );
161
+ }
162
+
163
+ public function testToStringMultipleHeadersExceptionIfDifferent ()
164
+ {
165
+ $ csp = new ContentSecurityPolicy ();
166
+ $ csp ->setDirective ('default-src ' , ["'self' " ]);
167
+
168
+ $ additional = new GenericHeader ();
169
+
170
+ $ this ->expectException (RuntimeException::class);
171
+ $ this ->expectExceptionMessage (
172
+ 'The ContentSecurityPolicy multiple header implementation '
173
+ . ' can only accept an array of ContentSecurityPolicy headers '
174
+ );
175
+ $ csp ->toStringMultipleHeaders ([$ additional ]);
176
+ }
177
+
178
+ public function testMultiple ()
179
+ {
180
+ $ headers = new Headers ();
181
+ $ headers ->addHeader ((new ContentSecurityPolicy ())->setDirective ('default-src ' , ["'self' " ]));
182
+ $ headers ->addHeader ((new ContentSecurityPolicy ())->setDirective ('img-src ' , ['https://*.github.com ' ]));
183
+
184
+ self ::assertSame (
185
+ "Content-Security-Policy: default-src 'self'; \r\n"
186
+ . "Content-Security-Policy: img-src https://*.github.com; \r\n" ,
187
+ $ headers ->toString ()
188
+ );
189
+ }
142
190
}
0 commit comments