Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'fix-http-request-1' of https://github.com/cogitatio/zf2
Browse files Browse the repository at this point in the history
…into hotfix/uri-host-validation
  • Loading branch information
weierophinney committed May 4, 2012
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,16 @@ public function getMethod()
public function setUri($uri)
{
if (is_string($uri)) {
if (!\Zend\Uri\Uri::validateHost($uri)) {
throw new Exception\InvalidArgumentException('Invalid URI passed as string');
try {
$uri = new HttpUri($uri);
} catch (Exception\InvalidUriPartException $e) {
throw new Exception\InvalidArgumentException(
sprintf('Invalid URI passed as string (%s)', (string) $uri),
$e->getCode(),
$e
);
}
} elseif (!($uri instanceof \Zend\Uri\Http)) {
} elseif (!($uri instanceof HttpUri)) {
throw new Exception\InvalidArgumentException('URI must be an instance of Zend\Uri\Http or a string');
}
$this->uri = $uri;
Expand Down
22 changes: 17 additions & 5 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,26 @@ public function testRequestCanAlwaysForcesUppecaseMethodName()
$this->assertEquals('GET', $request->getMethod());
}

public function testRequestCanSetAndRetrieveUri()
/**
* @dataProvider uriDataProvider
*/
public function testRequestCanSetAndRetrieveUri($uri)
{
$request = new Request();
$request->setUri('/foo');
$this->assertEquals('/foo', $request->getUri());
$request->setUri($uri);
$this->assertEquals($uri, $request->getUri());
$this->assertInstanceOf('Zend\Uri\Uri', $request->uri());
$this->assertEquals('/foo', $request->uri()->toString());
$this->assertEquals('/foo', $request->getUri());
$this->assertEquals($uri, $request->uri()->toString());
$this->assertEquals($uri, $request->getUri());
}

public function uriDataProvider()
{
return array(
array('/foo'),
array('/foo#test'),
array('/hello?what=true#noway')
);
}

public function testRequestSetUriWillThrowExceptionOnInvalidArgument()
Expand Down

0 comments on commit b98761f

Please sign in to comment.