class ReverseProxyMiddlewareTest
Unit test the reverse proxy stack middleware.
Attributes
#[Group('StackMiddleware')]
  Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase- class \Drupal\Tests\Core\StackMiddleware\ReverseProxyMiddlewareTest extends \Drupal\Tests\UnitTestCase
 
Expanded class hierarchy of ReverseProxyMiddlewareTest
File
- 
              core/tests/ Drupal/ Tests/ Core/ StackMiddleware/ ReverseProxyMiddlewareTest.php, line 19 
Namespace
Drupal\Tests\Core\StackMiddlewareView source
class ReverseProxyMiddlewareTest extends UnitTestCase {
  
  /**
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $mockHttpKernel;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $responseMock = $this->createMock(Response::class);
    $this->mockHttpKernel = $this->createMock(HttpKernelInterface::class);
    $this->mockHttpKernel
      ->method('handle')
      ->willReturn($responseMock);
  }
  
  /**
   * Tests that subscriber does not act when reverse proxy is not set.
   */
  public function testNoProxy() : void {
    $settings = new Settings([]);
    $this->assertEquals(0, $settings->get('reverse_proxy'));
    $middleware = new ReverseProxyMiddleware($this->mockHttpKernel, $settings);
    // Mock a request object.
    $request = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Request')
      ->onlyMethods([
      'setTrustedProxies',
    ])
      ->getMock();
    // setTrustedProxies() should never fire.
    $request->expects($this->never())
      ->method('setTrustedProxies');
    // Actually call the check method.
    $middleware->handle($request);
  }
  
  /**
   * Tests that subscriber sets trusted headers when reverse proxy is set.
   */
  public function testReverseProxyEnabled($provided_settings, $expected_trusted_header_set) : void {
    // Enable reverse proxy and add test values.
    $settings = new Settings([
      'reverse_proxy' => 1,
    ] + $provided_settings);
    $this->trustedHeadersAreSet($settings, $expected_trusted_header_set);
  }
  
  /**
   * Data provider for testReverseProxyEnabled.
   */
  public static function reverseProxyEnabledProvider() {
    return [
      'Proxy with default trusted headers' => [
        [
          'reverse_proxy_addresses' => [
            '127.0.0.2',
            '127.0.0.3',
          ],
        ],
        Request::HEADER_FORWARDED | Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO,
      ],
      'Proxy with AWS trusted headers' => [
        [
          'reverse_proxy_addresses' => [
            '127.0.0.2',
            '127.0.0.3',
          ],
          'reverse_proxy_trusted_headers' => Request::HEADER_X_FORWARDED_AWS_ELB,
        ],
        Request::HEADER_X_FORWARDED_AWS_ELB,
      ],
      'Proxy with custom trusted headers' => [
        [
          'reverse_proxy_addresses' => [
            '127.0.0.2',
            '127.0.0.3',
          ],
          'reverse_proxy_trusted_headers' => Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST,
        ],
        Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST,
      ],
    ];
  }
  
  /**
   * Tests that trusted headers are set correctly.
   *
   * \Symfony\Component\HttpFoundation\Request::setTrustedProxies() should
   * always be called when reverse proxy settings are enabled.
   *
   * @param \Drupal\Core\Site\Settings $settings
   *   The settings object that holds reverse proxy configuration.
   * @param int $expected_trusted_header_set
   *   The expected bit value returned by
   *   \Symfony\Component\HttpFoundation\Request::getTrustedHeaderSet()
   */
  protected function trustedHeadersAreSet(Settings $settings, $expected_trusted_header_set) : void {
    $middleware = new ReverseProxyMiddleware($this->mockHttpKernel, $settings);
    $request = new Request();
    $middleware->handle($request);
    $this->assertSame($settings->get('reverse_proxy_addresses'), $request->getTrustedProxies());
    $this->assertSame($expected_trusted_header_set, $request->getTrustedHeaderSet());
  }
}Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| ReverseProxyMiddlewareTest::$mockHttpKernel | protected | property | ||
| ReverseProxyMiddlewareTest::reverseProxyEnabledProvider | public static | function | Data provider for testReverseProxyEnabled. | |
| ReverseProxyMiddlewareTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
| ReverseProxyMiddlewareTest::testNoProxy | public | function | Tests that subscriber does not act when reverse proxy is not set. | |
| ReverseProxyMiddlewareTest::testReverseProxyEnabled | public | function | Tests that subscriber sets trusted headers when reverse proxy is set. | |
| ReverseProxyMiddlewareTest::trustedHeadersAreSet | protected | function | Tests that trusted headers are set correctly. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
