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

Fixes #36 Client::setUri() should keep relative uri status #149

Merged
merged 21 commits into from Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down Expand Up @@ -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.
16 changes: 10 additions & 6 deletions src/Client.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions test/ClientTest.php
Expand Up @@ -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());
}
}