Skip to content

Commit 1b7f7a6

Browse files
author
Greg Bowler
committed
Fix Uri tests
1 parent a489d1c commit 1b7f7a6

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/Uri.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ public function isSameDocumentReference(Uri $baseUri = null):bool {
679679
}
680680

681681
protected function setDefaults():void {
682-
if($this->host === "") {
682+
if(strlen($this->host) === 0) {
683683
if($this->scheme === "http"
684684
|| $this->scheme === "https") {
685685
$this->host = self::DEFAULT_HOST_HTTP;
@@ -690,6 +690,10 @@ protected function setDefaults():void {
690690
if(strpos($this->path, "//") === 0) {
691691
throw new \InvalidArgumentException("The path of a URI without an authority must not start with two slashes \"//\"");
692692
}
693+
if(strlen($this->scheme) === 0
694+
&& strpos(explode('/', $this->path, 2)[0], ':')) {
695+
throw new \InvalidArgumentException("A relative URI must not have a path beginning with a segment containing a colon");
696+
}
693697
}
694698
else {
695699
if(strlen($this->path) > 0

test/unit/UriTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22
namespace Gt\Http\Test;
33

4-
use Gt\Http\PortOutOfBoundsException;
54
use Gt\Http\Uri;
65
use Gt\Http\UriFactory;
76
use PHPUnit\Framework\TestCase;
8-
use Psr\Http\Message\UriInterface;
97

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

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

568-
/**
569-
* @expectedException \InvalidArgumentException
570-
* @expectedExceptionMessage A relative URI must not have a path beginning with a segment containing a colon
571-
*/
572564
public function testRelativeUriWithPathBeginngWithColonSegmentIsInvalid() {
565+
self::expectException(\InvalidArgumentException::class);
566+
self::expectExceptionMessage("A relative URI must not have a path beginning with a segment containing a colon");
573567
(new Uri)->withPath('mailto:foo');
574568
}
575569

576570
public function testRelativeUriWithPathHavingColonSegment() {
577571
$uri = (new Uri('urn:/mailto:foo'))->withScheme('');
578572
$this->assertSame('/mailto:foo', $uri->getPath());
579-
self::expectException('\InvalidArgumentException');
573+
self::expectException(\InvalidArgumentException::class);
580574
(new Uri('urn:mailto:foo'))->withScheme('');
581575
}
582576

0 commit comments

Comments
 (0)