You are here

public function RequestTest::testGetPort in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/Tests/RequestTest.php \Symfony\Component\HttpFoundation\Tests\RequestTest::testGetPort()

File

vendor/symfony/http-foundation/Tests/RequestTest.php, line 731

Class

RequestTest

Namespace

Symfony\Component\HttpFoundation\Tests

Code

public function testGetPort() {
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => 'https',
    'HTTP_X_FORWARDED_PORT' => '443',
  ));
  $port = $request
    ->getPort();
  $this
    ->assertEquals(80, $port, 'Without trusted proxies FORWARDED_PROTO and FORWARDED_PORT are ignored.');
  Request::setTrustedProxies(array(
    '1.1.1.1',
  ));
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => 'https',
    'HTTP_X_FORWARDED_PORT' => '8443',
  ));
  $this
    ->assertEquals(80, $request
    ->getPort(), 'With PROTO and PORT on untrusted connection server value takes precedence.');
  $request->server
    ->set('REMOTE_ADDR', '1.1.1.1');
  $this
    ->assertEquals(8443, $request
    ->getPort(), 'With PROTO and PORT set PORT takes precedence.');
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => 'https',
  ));
  $this
    ->assertEquals(80, $request
    ->getPort(), 'With only PROTO set getPort() ignores trusted headers on untrusted connection.');
  $request->server
    ->set('REMOTE_ADDR', '1.1.1.1');
  $this
    ->assertEquals(443, $request
    ->getPort(), 'With only PROTO set getPort() defaults to 443.');
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => 'http',
  ));
  $this
    ->assertEquals(80, $request
    ->getPort(), 'If X_FORWARDED_PROTO is set to HTTP getPort() ignores trusted headers on untrusted connection.');
  $request->server
    ->set('REMOTE_ADDR', '1.1.1.1');
  $this
    ->assertEquals(80, $request
    ->getPort(), 'If X_FORWARDED_PROTO is set to HTTP getPort() returns port of the original request.');
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => 'On',
  ));
  $this
    ->assertEquals(80, $request
    ->getPort(), 'With only PROTO set and value is On, getPort() ignores trusted headers on untrusted connection.');
  $request->server
    ->set('REMOTE_ADDR', '1.1.1.1');
  $this
    ->assertEquals(443, $request
    ->getPort(), 'With only PROTO set and value is On, getPort() defaults to 443.');
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => '1',
  ));
  $this
    ->assertEquals(80, $request
    ->getPort(), 'With only PROTO set and value is 1, getPort() ignores trusted headers on untrusted connection.');
  $request->server
    ->set('REMOTE_ADDR', '1.1.1.1');
  $this
    ->assertEquals(443, $request
    ->getPort(), 'With only PROTO set and value is 1, getPort() defaults to 443.');
  $request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
    'HTTP_X_FORWARDED_PROTO' => 'something-else',
  ));
  $port = $request
    ->getPort();
  $this
    ->assertEquals(80, $port, 'With only PROTO set and value is not recognized, getPort() defaults to 80.');
  Request::setTrustedProxies(array());
}