You are here

public function CspOptimizationTest::testReduceSourceListWithWs in Content-Security-Policy 8

Test reducing the source list when 'ws:' is included.

@covers ::reduceSourceList

File

tests/src/Unit/CspOptimizationTest.php, line 308

Class

CspOptimizationTest
Test optimization of CSP directives.

Namespace

Drupal\Tests\csp\Unit

Code

public function testReduceSourceListWithWs() {
  $policy = new Csp();
  $policy
    ->setDirective('script-src', [
    'https:',
    'ws:',
    // Hosts without protocol should be kept.
    // (e.g. this would allow ftp://example.com)
    'example.com',
    // HTTP hosts should be removed.
    'ws://connect.example.org',
    'wss://connect.example.net',
    // Other network protocols should be kept.
    'ftp:',
    // Non-network protocols should be kept.
    'data:',
    // Additional keywords should be kept.
    Csp::POLICY_UNSAFE_INLINE,
    "'hash-123abc'",
    "'nonce-abc123'",
  ]);
  $this
    ->assertEquals("script-src https: ws: example.com ftp: data: 'unsafe-inline' 'hash-123abc' 'nonce-abc123'", $policy
    ->getHeaderValue());
}