Skip to content

Commit

Permalink
Update tests for handling a default port in the Host header
Browse files Browse the repository at this point in the history
Also remove test where SERVER_PORT is used when Host header exists as
this was an incorrect assumption that is wrong.
  • Loading branch information
akrabat committed Sep 11, 2018
1 parent 8b7a445 commit 77bd8c9
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions tests/Http/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,27 @@ public function testCreateEnvironment()
$this->assertEquals('', $uri->getFragment());
}

public function testCreateFromEnvironmentSetsDefaultPortWhenHostHeaderDoesntHaveAPort()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com',
'HTTPS' => 'on',
]);

$uri = Uri::createFromEnvironment($environment);

$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals(null, $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());

$this->assertEquals('https://example.com/foo/bar?abc=123', (string)$uri);
}

public function testCreateEnvironmentWithIPv6HostNoPort()
{
$environment = Environment::mock([
Expand All @@ -556,14 +577,13 @@ public function testCreateEnvironmentWithIPv6HostNoPort()
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);

$uri = Uri::createFromEnvironment($environment);

$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertNull($uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
Expand All @@ -579,7 +599,6 @@ public function testCreateEnvironmentWithIPv6HostWithPort()
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]:8080',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);

$uri = Uri::createFromEnvironment($environment);
Expand All @@ -592,6 +611,33 @@ public function testCreateEnvironmentWithIPv6HostWithPort()
$this->assertEquals('', $uri->getFragment());
}

/**
* @group one
*/
public function testCreateEnvironmentWithNoHostHeader()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_NAME' => '[2001:db8::1]',
'SERVER_PORT' => '8080',
]);
$environment->remove('HTTP_HOST');

$uri = Uri::createFromEnvironment($environment);

$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}

/**
* @covers Slim\Http\Uri::createFromEnvironment
* @ticket 1375
Expand Down

0 comments on commit 77bd8c9

Please sign in to comment.