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

Commit

Permalink
Merge pull request #149 from samsonasik/fix-36
Browse files Browse the repository at this point in the history
Fixes #36 Client::setUri() should keep relative uri status

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Jan 8, 2019
2 parents 834c806 + aa48298 commit 75969b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#149](https://github.com/zendframework/zend-http/pull/149) provides fixes to `Client::setUri()` to ensure its status as a relative
or absolute URI is correctly memoized.

- [#162](https://github.com/zendframework/zend-http/pull/162) fixes a typo in an exception message raised within `Cookies::fromString()`.

- [#121](https://github.com/zendframework/zend-http/pull/121) adds detection for non-numeric connection timeout values as well as
Expand Down
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());
}
}

0 comments on commit 75969b6

Please sign in to comment.