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

Commit b98e979

Browse files
committed
Merge branch 'hotfix/149'
Close #149 Fixes #36
2 parents 834c806 + 75969b6 commit b98e979

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#149](https://github.com/zendframework/zend-http/pull/149) provides fixes to `Client::setUri()` to ensure its status as a relative
26+
or absolute URI is correctly memoized.
27+
2528
- [#162](https://github.com/zendframework/zend-http/pull/162) fixes a typo in an exception message raised within `Cookies::fromString()`.
2629

2730
- [#121](https://github.com/zendframework/zend-http/pull/121) adds detection for non-numeric connection timeout values as well as

src/Client.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,18 @@ public function setUri($uri)
324324
$this->clearAuth();
325325
}
326326

327+
$uri = $this->getUri();
328+
$user = $uri->getUser();
329+
$password = $uri->getPassword();
330+
327331
// Set auth if username and password has been specified in the uri
328-
if ($this->getUri()->getUser() && $this->getUri()->getPassword()) {
329-
$this->setAuth($this->getUri()->getUser(), $this->getUri()->getPassword());
332+
if ($user && $password) {
333+
$this->setAuth($user, $password);
330334
}
331335

332336
// We have no ports, set the defaults
333-
if (! $this->getUri()->getPort()) {
334-
$this->getUri()->setPort(($this->getUri()->getScheme() == 'https' ? 443 : 80));
337+
if (! $uri->getPort() && $uri->isAbsolute()) {
338+
$uri->setPort($uri->getScheme() === 'https' ? 443 : 80);
335339
}
336340
}
337341
return $this;
@@ -901,8 +905,8 @@ public function send(Request $request = null)
901905
}
902906
}
903907
// If we have no ports, set the defaults
904-
if (! $uri->getPort()) {
905-
$uri->setPort($uri->getScheme() == 'https' ? 443 : 80);
908+
if (! $uri->getPort() && $uri->isAbsolute()) {
909+
$uri->setPort($uri->getScheme() === 'https' ? 443 : 80);
906910
}
907911

908912
// method

test/ClientTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,4 +524,59 @@ public function testFormUrlEncodeSeparator()
524524
$rawRequest = $client->getLastRawRequest();
525525
$this->assertContains('foo=bar&baz=foo', $rawRequest);
526526
}
527+
528+
public function uriDataProvider()
529+
{
530+
return [
531+
'valid-relative' => ['/example', true],
532+
'invalid-absolute' => ['http://localhost/example', false],
533+
];
534+
}
535+
536+
/**
537+
* @dataProvider uriDataProvider
538+
*/
539+
public function testUriCorrectlyDeterminesWhetherOrNotItIsAValidRelativeUri($uri, $isValidRelativeURI)
540+
{
541+
$client = new Client($uri);
542+
$this->assertSame($isValidRelativeURI, $client->getUri()->isValidRelative());
543+
544+
$client->setAdapter(Test::class);
545+
$client->send();
546+
$this->assertSame($isValidRelativeURI, $client->getUri()->isValidRelative());
547+
}
548+
549+
public function portChangeDataProvider()
550+
{
551+
return [
552+
'default-https' => ['https://localhost/example', 443],
553+
'default-http' => ['http://localhost/example', 80]
554+
];
555+
}
556+
557+
/**
558+
* @dataProvider portChangeDataProvider
559+
*/
560+
public function testUriPortIsSetToAppropriateDefaultValueWhenAnUriOmittingThePortIsProvided($absoluteURI, $port)
561+
{
562+
$client = new Client();
563+
$client->getUri()->setPort(null);
564+
565+
$client->setUri($absoluteURI);
566+
$this->assertSame($port, $client->getUri()->getPort());
567+
568+
$client->setAdapter(Test::class);
569+
$client->send();
570+
$this->assertSame($port, $client->getUri()->getPort());
571+
}
572+
573+
public function testUriPortIsNotSetWhenUriIsRelative()
574+
{
575+
$client = new Client('/example');
576+
$this->assertNull($client->getUri()->getPort());
577+
578+
$client->setAdapter(Test::class);
579+
$client->send();
580+
$this->assertNull($client->getUri()->getPort());
581+
}
527582
}

0 commit comments

Comments
 (0)