Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #54 from sunrise-php/release/v1.2.3
Browse files Browse the repository at this point in the history
v1.2.3
  • Loading branch information
fenric committed Apr 19, 2022
2 parents 4ffab82 + 19fb9f2 commit c526810
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Uri.php
Expand Up @@ -27,6 +27,8 @@
* Import functions
*/
use function getservbyname;
use function ltrim;
use function strncmp;

/**
* Uniform Resource Identifier
Expand Down Expand Up @@ -351,6 +353,25 @@ public function __toString()

$path = $this->getPath();
if ($path !== '') {
// https://github.com/sunrise-php/uri/issues/31
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
//
// If a URI contains an authority component,
// then the path component must either be empty
// or begin with a slash ("/") character.
if ($authority !== '' && strncmp($path, '/', 1) !== 0) {
$path = '/' . $path;
}

// https://github.com/sunrise-php/uri/issues/31
// https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
//
// If a URI does not contain an authority component,
// then the path cannot begin with two slash characters ("//").
if ($authority === '' && strncmp($path, '//', 2) === 0) {
$path = '/' . ltrim($path, '/');
}

$uri .= $path;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/UriTest.php
Expand Up @@ -567,6 +567,21 @@ public function testToString()
$this->assertSame(self::TEST_URI, (string) $uri);
}

public function testToStringIssue31()
{
$uri = new Uri('//username@hostname');
$uri = $uri->withPath('pathname');
$this->assertSame('//username@hostname/pathname', $uri->__toString());

$uri = new Uri('scheme:');
$uri = $uri->withPath('//pathname');
$this->assertSame('scheme:/pathname', $uri->__toString());

$uri = new Uri('scheme:');
$uri = $uri->withPath('///pathname');
$this->assertSame('scheme:/pathname', $uri->__toString());
}

// Normalizes...

public function testNormalizeScheme()
Expand Down

0 comments on commit c526810

Please sign in to comment.