Skip to content

Commit

Permalink
Fix Uri tests
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Nov 5, 2018
1 parent a489d1c commit 1b7f7a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public function isSameDocumentReference(Uri $baseUri = null):bool {
}

protected function setDefaults():void {
if($this->host === "") {
if(strlen($this->host) === 0) {
if($this->scheme === "http"
|| $this->scheme === "https") {
$this->host = self::DEFAULT_HOST_HTTP;
Expand All @@ -690,6 +690,10 @@ protected function setDefaults():void {
if(strpos($this->path, "//") === 0) {
throw new \InvalidArgumentException("The path of a URI without an authority must not start with two slashes \"//\"");
}
if(strlen($this->scheme) === 0
&& strpos(explode('/', $this->path, 2)[0], ':')) {
throw new \InvalidArgumentException("A relative URI must not have a path beginning with a segment containing a colon");
}
}
else {
if(strlen($this->path) > 0
Expand Down
18 changes: 6 additions & 12 deletions test/unit/UriTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?php
namespace Gt\Http\Test;

use Gt\Http\PortOutOfBoundsException;
use Gt\Http\Uri;
use Gt\Http\UriFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface;

class UriTest extends TestCase {
public function testParsesProvidedUri() {
Expand Down Expand Up @@ -547,11 +545,9 @@ public function testRelativePathAndAuhorityIsAutomagicallyFixed() {
$this->assertSame('//example.com/foo', (string)$uri);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The path of a URI without an authority must not start with two slashes "//"
*/
public function testPathStartingWithTwoSlashesAndNoAuthorityIsInvalid() {
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage("The path of a URI without an authority must not start with two slashes \"//\"");
// URI "//foo" would be interpreted as network reference and thus change the original path to the host
(new Uri)->withPath('//foo');
}
Expand All @@ -561,22 +557,20 @@ public function testPathStartingWithTwoSlashes() {
$this->assertSame('//path-not-host.com', $uri->getPath());
$uri = $uri->withScheme('');
$this->assertSame('//example.org//path-not-host.com', (string)$uri); // This is still valid
self::expectException('\InvalidArgumentException');
self::expectException(\InvalidArgumentException::class);
$uri->withHost(''); // Now it becomes invalid
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage A relative URI must not have a path beginning with a segment containing a colon
*/
public function testRelativeUriWithPathBeginngWithColonSegmentIsInvalid() {
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage("A relative URI must not have a path beginning with a segment containing a colon");
(new Uri)->withPath('mailto:foo');
}

public function testRelativeUriWithPathHavingColonSegment() {
$uri = (new Uri('urn:/mailto:foo'))->withScheme('');
$this->assertSame('/mailto:foo', $uri->getPath());
self::expectException('\InvalidArgumentException');
self::expectException(\InvalidArgumentException::class);
(new Uri('urn:mailto:foo'))->withScheme('');
}

Expand Down

0 comments on commit 1b7f7a6

Please sign in to comment.