diff --git a/CHANGELOG.md b/CHANGELOG.md index b97e65b8ae..2f36c9fd0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +[#149](https://github.com/zendframework/zend-http/pull/149) Client::setUri() should keep relative uri status ## 2.8.2 - 2018-08-13 @@ -336,4 +336,4 @@ All notable changes to this project will be documented in this file, in reverse - [#14](https://github.com/zendframework/zend-http/pull/14) fixes `Zend\Http\PhpEnvironment\Request` to ensure that empty `SCRIPT_FILENAME` and `SCRIPT_NAME` values which result in an empty `$baseUrl` will not raise an - `E_WARNING` when used to do a `strpos()` check during base URI detection. + `E_WARNING` when used to do a `strpos()` check during base URI detection. \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index dab67b8f74..9396f591b6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -324,14 +324,18 @@ public function setUri($uri) $this->clearAuth(); } + $uri = $this->getUri(); + $user = $uri->getUser(); + $password = $uri->getPassword(); + // Set auth if username and password has been specified in the uri - if ($this->getUri()->getUser() && $this->getUri()->getPassword()) { - $this->setAuth($this->getUri()->getUser(), $this->getUri()->getPassword()); + if ($user && $password) { + $this->setAuth($user, $password); } // We have no ports, set the defaults - if (! $this->getUri()->getPort()) { - $this->getUri()->setPort(($this->getUri()->getScheme() == 'https' ? 443 : 80)); + if (! $uri->getPort() && $uri->isAbsolute()) { + $uri->setPort($uri->getScheme() === 'https' ? 443 : 80); } } return $this; @@ -901,8 +905,8 @@ public function send(Request $request = null) } } // If we have no ports, set the defaults - if (! $uri->getPort()) { - $uri->setPort($uri->getScheme() == 'https' ? 443 : 80); + if (! $uri->getPort() && $uri->isAbsolute()) { + $uri->setPort($uri->getScheme() === 'https' ? 443 : 80); } // method diff --git a/test/ClientTest.php b/test/ClientTest.php index 0794927f2c..2b018d0e73 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -524,4 +524,59 @@ public function testFormUrlEncodeSeparator() $rawRequest = $client->getLastRawRequest(); $this->assertContains('foo=bar&baz=foo', $rawRequest); } + + public function uriDataProvider() + { + return [ + 'valid-relative' => ['/example', true], + 'invalid-absolute' => ['http://localhost/example', false], + ]; + } + + /** + * @dataProvider uriDataProvider + */ + public function testUriCorrectlyDeterminesWhetherOrNotItIsAValidRelativeUri($uri, $isValidRelativeURI) + { + $client = new Client($uri); + $this->assertSame($isValidRelativeURI, $client->getUri()->isValidRelative()); + + $client->setAdapter(Test::class); + $client->send(); + $this->assertSame($isValidRelativeURI, $client->getUri()->isValidRelative()); + } + + public function portChangeDataProvider() + { + return [ + 'default-https' => ['https://localhost/example', 443], + 'default-http' => ['http://localhost/example', 80] + ]; + } + + /** + * @dataProvider portChangeDataProvider + */ + public function testUriPortIsSetToAppropriateDefaultValueWhenAnUriOmittingThePortIsProvided($absoluteURI, $port) + { + $client = new Client(); + $client->getUri()->setPort(null); + + $client->setUri($absoluteURI); + $this->assertSame($port, $client->getUri()->getPort()); + + $client->setAdapter(Test::class); + $client->send(); + $this->assertSame($port, $client->getUri()->getPort()); + } + + public function testUriPortIsNotSetWhenUriIsRelative() + { + $client = new Client('/example'); + $this->assertNull($client->getUri()->getPort()); + + $client->setAdapter(Test::class); + $client->send(); + $this->assertNull($client->getUri()->getPort()); + } }