@@ -187,4 +187,65 @@ public function testMultiple()
187
187
$ headers ->toString ()
188
188
);
189
189
}
190
+
191
+ public static function validDirectives ()
192
+ {
193
+ return [
194
+ ['child-src ' , ["'self' " ],"Content-Security-Policy: child-src 'self'; " ],
195
+ ['manifest-src ' , ["'self' " ], "Content-Security-Policy: manifest-src 'self'; " ],
196
+ ['worker-src ' , ["'self' " ], "Content-Security-Policy: worker-src 'self'; " ],
197
+ ['prefetch-src ' , ["'self' " ], "Content-Security-Policy: prefetch-src 'self'; " ],
198
+ ['script-src-elem ' , ["'self' " ], "Content-Security-Policy: script-src-elem 'self'; " ],
199
+ ['script-src-attr ' , ["'self' " ], "Content-Security-Policy: script-src-attr 'self'; " ],
200
+ ['style-src-elem ' , ["'self' " ], "Content-Security-Policy: style-src-elem 'self'; " ],
201
+ ['style-src-attr ' , ["'self' " ], "Content-Security-Policy: style-src-attr 'self'; " ],
202
+ ['base-uri ' , ["'self' " , "'unsafe-inline' " ], "Content-Security-Policy: base-uri 'self' 'unsafe-inline'; " ],
203
+ ['plugin-types ' , ['text/csv ' ], 'Content-Security-Policy: plugin-types text/csv; ' ],
204
+ [
205
+ 'form-action ' ,
206
+ ['http://*.example.com ' , "'self' " ],
207
+ "Content-Security-Policy: form-action http://*.example.com 'self'; "
208
+ ],
209
+ [
210
+ 'frame-ancestors ' ,
211
+ ['http://*.example.com ' , "'self' " ],
212
+ "Content-Security-Policy: frame-ancestors http://*.example.com 'self'; "
213
+ ],
214
+ ['navigate-to ' , ['example.com ' ], 'Content-Security-Policy: navigate-to example.com; ' ],
215
+ ['sandbox ' , ['allow-forms ' ], 'Content-Security-Policy: sandbox allow-forms; ' ],
216
+ ];
217
+ }
218
+
219
+ /**
220
+ * @dataProvider validDirectives
221
+ *
222
+ * @param string $directive
223
+ * @param string[] $values
224
+ * @param string $expected
225
+ */
226
+ public function testContentSecurityPolicySetDirectiveThrowsExceptionIfMissingDirectiveNameGiven (
227
+ $ directive ,
228
+ array $ values ,
229
+ $ expected
230
+ ) {
231
+ $ csp = new ContentSecurityPolicy ();
232
+ $ csp ->setDirective ($ directive , $ values );
233
+
234
+ self ::assertSame ($ expected , $ csp ->toString ());
235
+ }
236
+
237
+ /**
238
+ * @dataProvider validDirectives
239
+ *
240
+ * @param string $directive
241
+ * @param string[] $values
242
+ * @param string $header
243
+ */
244
+ public function testFromString ($ directive , array $ values , $ header )
245
+ {
246
+ $ contentSecurityPolicy = ContentSecurityPolicy::fromString ($ header );
247
+
248
+ self ::assertArrayHasKey ($ directive , $ contentSecurityPolicy ->getDirectives ());
249
+ self ::assertSame (implode (' ' , $ values ), $ contentSecurityPolicy ->getDirectives ()[$ directive ]);
250
+ }
190
251
}
0 commit comments