Skip to content

Commit

Permalink
[HttpFoundation] [Tests] Refactor tests for prepare request uri
Browse files Browse the repository at this point in the history
  • Loading branch information
alquerci committed Dec 9, 2018
1 parent 2cdbd9f commit 95e9fe0
Showing 1 changed file with 39 additions and 49 deletions.
88 changes: 39 additions & 49 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -238,63 +238,53 @@ public function testCreate()
$this->assertEquals('http://test.com/foo', $request->getUri());
}

public function testCreateWithRequestUri()
/**
* @dataProvider getRequestUriData
*/
public function testGetRequestUri($serverRequestUri, $expected, $message)
{
$request = Request::create('http://test.com:80/foo');
$request->server->set('REQUEST_URI', 'http://test.com:80/foo');
$this->assertEquals('http://test.com/foo', $request->getUri());
$this->assertEquals('/foo', $request->getPathInfo());
$this->assertEquals('test.com', $request->getHost());
$this->assertEquals('test.com', $request->getHttpHost());
$this->assertEquals(80, $request->getPort());
$this->assertFalse($request->isSecure());
$request = new Request();
$request->server->add(array(
'REQUEST_URI' => $serverRequestUri,

$request = Request::create('http://test.com:8080/foo');
$request->server->set('REQUEST_URI', 'http://test.com:8080/foo');
$this->assertEquals('http://test.com:8080/foo', $request->getUri());
$this->assertEquals('/foo', $request->getPathInfo());
$this->assertEquals('test.com', $request->getHost());
$this->assertEquals('test.com:8080', $request->getHttpHost());
$this->assertEquals(8080, $request->getPort());
$this->assertFalse($request->isSecure());
// For having http://test.com
'SERVER_NAME' => 'test.com',
'SERVER_PORT' => 80,
));

$request = Request::create('http://test.com/foo?bar=foo', 'GET', array('bar' => 'baz'));
$request->server->set('REQUEST_URI', 'http://test.com/foo?bar=foo');
$this->assertEquals('http://test.com/foo?bar=baz', $request->getUri());
$this->assertEquals('/foo', $request->getPathInfo());
$this->assertEquals('bar=baz', $request->getQueryString());
$this->assertEquals('test.com', $request->getHost());
$this->assertEquals('test.com', $request->getHttpHost());
$this->assertEquals(80, $request->getPort());
$this->assertFalse($request->isSecure());
$this->assertSame($expected, $request->getRequestUri(), $message);
$this->assertSame($expected, $request->server->get('REQUEST_URI'), 'Normalize the request URI.');
}

$request = Request::create('https://test.com:443/foo');
$request->server->set('REQUEST_URI', 'https://test.com:443/foo');
$this->assertEquals('https://test.com/foo', $request->getUri());
$this->assertEquals('/foo', $request->getPathInfo());
$this->assertEquals('test.com', $request->getHost());
$this->assertEquals('test.com', $request->getHttpHost());
$this->assertEquals(443, $request->getPort());
$this->assertTrue($request->isSecure());
public function getRequestUriData()
{
$message = 'Do not modify the path.';
yield array('/foo', '/foo', $message);
yield array('//bar/foo', '//bar/foo', $message);
yield array('///bar/foo', '///bar/foo', $message);

// Fragment should not be included in the URI
$request = Request::create('http://test.com/foo#bar');
$request->server->set('REQUEST_URI', 'http://test.com/foo#bar');
$this->assertEquals('http://test.com/foo', $request->getUri());
$message = 'Handle when the scheme, host are on REQUEST_URI.';
yield array('http://test.com/foo?bar=baz', '/foo?bar=baz', $message);

$request = Request::create('http://test.com/foo#bar');
$request->server->set('REQUEST_URI', '/foo#bar');
$this->assertEquals('http://test.com/foo', $request->getUri());
$message = 'Handle when the scheme, host and port are on REQUEST_URI.';
yield array('http://test.com:80/foo', '/foo', $message);
yield array('https://test.com:8080/foo', '/foo', $message);
yield array('https://test.com:443/foo', '/foo', $message);

// When the path starts with 2 slashes
$request = Request::create('http://foo//bar/foo');
$request->server->set('REQUEST_URI', '//bar/foo');
$this->assertEquals('//bar/foo', $request->getPathInfo(), 'When the REQUEST_URI starts with double slashes.');
$message = 'Fragment should not be included in the URI';
yield array('http://test.com/foo#bar', '/foo', $message);
yield array('/foo#bar', '/foo', $message);
}

public function testGetRequestUriWithoutRequiredHeader()
{
$expected = '';

$request = new Request();

// When the path starts with more than 2 slashes
$request = Request::create('http://foo///bar/foo');
$request->server->set('REQUEST_URI', '///bar/foo');
$this->assertEquals('///bar/foo', $request->getPathInfo(), 'When the REQUEST_URI starts with more than 2 slashes.');
$message = 'Fallback to empty URI when headers are missing.';
$this->assertSame($expected, $request->getRequestUri(), $message);
$this->assertSame($expected, $request->server->get('REQUEST_URI'), 'Normalize the request URI.');
}

public function testCreateCheckPrecedence()
Expand Down

0 comments on commit 95e9fe0

Please sign in to comment.