@@ -434,12 +434,42 @@ public function testPortCanBeRemoved()
434
434
$ this ->assertSame ('http://example.com ' , (string ) $ uri );
435
435
}
436
436
437
- public function testAuthorityWithUserInfoButWithoutHost ()
437
+ /**
438
+ * In RFC 8986 the host is optional and the authority can only
439
+ * consist of the user info and port.
440
+ */
441
+ public function testAuthorityWithUserInfoOrPortButWithoutHost ()
438
442
{
439
443
$ uri = (new Uri ())->withUserInfo ('user ' , 'pass ' );
440
444
441
445
$ this ->assertSame ('user:pass ' , $ uri ->getUserInfo ());
442
- $ this ->assertSame ('' , $ uri ->getAuthority ());
446
+ $ this ->assertSame ('user:pass@ ' , $ uri ->getAuthority ());
447
+
448
+ $ uri = $ uri ->withPort (8080 );
449
+ $ this ->assertSame (8080 , $ uri ->getPort ());
450
+ $ this ->assertSame ('user:pass@:8080 ' , $ uri ->getAuthority ());
451
+ $ this ->assertSame ('//user:pass@:8080 ' , (string ) $ uri );
452
+
453
+ $ uri = $ uri ->withUserInfo ('' );
454
+ $ this ->assertSame (':8080 ' , $ uri ->getAuthority ());
455
+ }
456
+
457
+ public function testHostInHttpUriDefaultsToLocalhost ()
458
+ {
459
+ $ uri = (new Uri ())->withScheme ('http ' );
460
+
461
+ $ this ->assertSame ('localhost ' , $ uri ->getHost ());
462
+ $ this ->assertSame ('localhost ' , $ uri ->getAuthority ());
463
+ $ this ->assertSame ('http://localhost ' , (string ) $ uri );
464
+ }
465
+
466
+ public function testHostInHttpsUriDefaultsToLocalhost ()
467
+ {
468
+ $ uri = (new Uri ())->withScheme ('https ' );
469
+
470
+ $ this ->assertSame ('localhost ' , $ uri ->getHost ());
471
+ $ this ->assertSame ('localhost ' , $ uri ->getAuthority ());
472
+ $ this ->assertSame ('https://localhost ' , (string ) $ uri );
443
473
}
444
474
445
475
public function uriComponentsEncodingProvider ()
@@ -509,24 +539,53 @@ public function testAllowsForRelativeUri()
509
539
$ this ->assertSame ('foo ' , (string ) $ uri );
510
540
}
511
541
512
- public function testAddsSlashForRelativeUriStringWithHost ()
542
+ /**
543
+ * @expectedException \InvalidArgumentException
544
+ * @expectedExceptionMessage The path of a URI with an authority must start with a slash "/" or be empty
545
+ */
546
+ public function testRelativePathAndAuhorityIsInvalid ()
513
547
{
514
- // If the path is rootless and an authority is present, the path MUST
515
- // be prefixed by "/".
516
- $ uri = (new Uri )->withPath ('foo ' )->withHost ('example.com ' );
517
- $ this ->assertSame ('foo ' , $ uri ->getPath ());
518
548
// concatenating a relative path with a host doesn't work: "//example.comfoo" would be wrong
519
- $ this -> assertSame ( ' // example.com/foo ' , ( string ) $ uri );
549
+ ( new Uri )-> withPath ( ' foo ' )-> withHost ( ' example.com ' );
520
550
}
521
551
522
- public function testRemoveExtraSlashesWihoutHost ()
552
+ /**
553
+ * @expectedException \InvalidArgumentException
554
+ * @expectedExceptionMessage The path of a URI without an authority must not start with two slashes "//"
555
+ */
556
+ public function testPathStartingWithTwoSlashesAndNoAuthorityIsInvalid ()
523
557
{
524
- // If the path is starting with more than one "/" and no authority is
525
- // present, the starting slashes MUST be reduced to one.
526
- $ uri = (new Uri )->withPath ('//foo ' );
527
- $ this ->assertSame ('//foo ' , $ uri ->getPath ());
528
558
// URI "//foo" would be interpreted as network reference and thus change the original path to the host
529
- $ this ->assertSame ('/foo ' , (string ) $ uri );
559
+ (new Uri )->withPath ('//foo ' );
560
+ }
561
+
562
+ public function testPathStartingWithTwoSlashes ()
563
+ {
564
+ $ uri = new Uri ('http://example.org//path-not-host.com ' );
565
+ $ this ->assertSame ('//path-not-host.com ' , $ uri ->getPath ());
566
+
567
+ $ uri = $ uri ->withScheme ('' );
568
+ $ this ->assertSame ('//example.org//path-not-host.com ' , (string ) $ uri ); // This is still valid
569
+ $ this ->setExpectedException ('\InvalidArgumentException ' );
570
+ $ uri ->withHost ('' ); // Now it becomes invalid
571
+ }
572
+
573
+ /**
574
+ * @expectedException \InvalidArgumentException
575
+ * @expectedExceptionMessage A relative URI must not have a path beginning with a segment containing a colon
576
+ */
577
+ public function testRelativeUriWithPathBeginngWithColonSegmentIsInvalid ()
578
+ {
579
+ (new Uri )->withPath ('mailto:foo ' );
580
+ }
581
+
582
+ public function testRelativeUriWithPathHavingColonSegment ()
583
+ {
584
+ $ uri = (new Uri ('urn:/mailto:foo ' ))->withScheme ('' );
585
+ $ this ->assertSame ('/mailto:foo ' , $ uri ->getPath ());
586
+
587
+ $ this ->setExpectedException ('\InvalidArgumentException ' );
588
+ (new Uri ('urn:mailto:foo ' ))->withScheme ('' );
530
589
}
531
590
532
591
public function testDefaultReturnValuesOfGetters ()
@@ -559,15 +618,15 @@ public function testImmutability()
559
618
public function testExtendingClassesInstantiates ()
560
619
{
561
620
// The non-standard port triggers a cascade of private methods which
562
- // should not use late static binding to access private static members.
621
+ // should not use late static binding to access private static members.
563
622
// If they do, this will fatal.
564
623
$ this ->assertInstanceOf (
565
- '\ GuzzleHttp\Tests\Psr7\ExtendingClassTest ' ,
566
- new ExtendingClassTest ('http://h:9/ ' )
624
+ 'GuzzleHttp\Tests\Psr7\ExtendedUriTest ' ,
625
+ new ExtendedUriTest ('http://h:9/ ' )
567
626
);
568
627
}
569
628
}
570
629
571
- class ExtendingClassTest extends \ GuzzleHttp \ Psr7 \ Uri
630
+ class ExtendedUriTest extends Uri
572
631
{
573
632
}
0 commit comments