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

Commit

Permalink
Merge branch 'hotfix/80'
Browse files Browse the repository at this point in the history
Close #85
  • Loading branch information
weierophinney committed Aug 10, 2015
2 parents 20cc20f + d0f90f0 commit ee8dfc4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ All notable changes to this project will be documented in this file, in reverse
- [#73](https://github.com/zendframework/zend-diactoros/pull/73) changes the
behavior in `Request` such that if it marshals a stream during instantiation,
the stream is marked as writeable (specifically, mode `wb+`).
- [#85](https://github.com/zendframework/zend-diactoros/pull/85) updates the
behavior of `Zend\Diactoros\Uri`'s various `with*()` methods that are
documented as accepting strings to raise exceptions on non-string input.
Previously, several simply passed non-string input on verbatim, others
normalized the input, and a few correctly raised the exceptions. Behavior is
now consistent across each.

## 1.1.2 - 2015-07-12

Expand Down
43 changes: 39 additions & 4 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ public function getFragment()
*/
public function withScheme($scheme)
{
if (! is_string($scheme)) {
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
(is_object($scheme) ? get_class($scheme) : gettype($scheme))
));
}

$scheme = $this->filterScheme($scheme);

if ($scheme === $this->scheme) {
Expand All @@ -238,6 +246,21 @@ public function withScheme($scheme)
*/
public function withUserInfo($user, $password = null)
{
if (! is_string($user)) {
throw new InvalidArgumentException(sprintf(
'%s expects a string user argument; received %s',
__METHOD__,
(is_object($user) ? get_class($user) : gettype($user))
));
}
if (null !== $password && ! is_string($password)) {
throw new InvalidArgumentException(sprintf(
'%s expects a string password argument; received %s',
__METHOD__,
(is_object($password) ? get_class($password) : gettype($password))
));
}

$info = $user;
if ($password) {
$info .= ':' . $password;
Expand All @@ -259,6 +282,14 @@ public function withUserInfo($user, $password = null)
*/
public function withHost($host)
{
if (! is_string($host)) {
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
(is_object($host) ? get_class($host) : gettype($host))
));
}

if ($host === $this->host) {
// Do nothing if no change was made.
return clone $this;
Expand Down Expand Up @@ -373,6 +404,14 @@ public function withQuery($query)
*/
public function withFragment($fragment)
{
if (! is_string($fragment)) {
throw new InvalidArgumentException(sprintf(
'%s expects a string argument; received %s',
__METHOD__,
(is_object($fragment) ? get_class($fragment) : gettype($fragment))
));
}

$fragment = $this->filterFragment($fragment);

if ($fragment === $this->fragment) {
Expand Down Expand Up @@ -585,10 +624,6 @@ private function splitQueryValue($value)
*/
private function filterFragment($fragment)
{
if (null === $fragment) {
$fragment = '';
}

if (! empty($fragment) && strpos($fragment, '#') === 0) {
$fragment = substr($fragment, 1);
}
Expand Down
45 changes: 45 additions & 0 deletions test/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,49 @@ public function testProperlyTrimsLeadingSlashesToPreventXSS()
$uri = new Uri($url);
$this->assertEquals('http://example.org/zend.com', (string) $uri);
}

public function invalidStringComponentValues()
{
$methods = [
'withScheme',
'withUserInfo',
'withHost',
'withPath',
'withQuery',
'withFragment',
];

$values = [
'null' => null,
'true' => true,
'false' => false,
'zero' => 0,
'int' => 1,
'zero-float' => 0.0,
'float' => 1.1,
'array' => ['value'],
'object' => (object)['value' => 'value'],
];

$combinations = [];
foreach ($methods as $method) {
foreach ($values as $type => $value) {
$key = sprintf('%s-%s', $method, $type);
$combinations[$key] = [$method, $value];
}
}

return $combinations;
}

/**
* @group 80
* @dataProvider invalidStringComponentValues
*/
public function testPassingInvalidValueToWithMethodRaisesException($method, $value)
{
$uri = new Uri('https://example.com/');
$this->setExpectedException('InvalidArgumentException');
$uri->$method($value);
}
}

0 comments on commit ee8dfc4

Please sign in to comment.