Skip to content
Merged
2 changes: 0 additions & 2 deletions ext/uri/php_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -1425,8 +1425,6 @@ PHP_METHOD(Uri_WhatWg_UrlBuilder, build)

lxb_url_t *base_url = NULL;
if (base_url_zv != NULL) {
zend_argument_error(NULL, 1, "is not supported yet, and therefore, null must be passed");
RETURN_THROWS();
base_url = Z_URI_OBJECT_P(base_url_zv)->uri;
}

Expand Down
21 changes: 21 additions & 0 deletions ext/uri/tests/whatwg/builder/authority_success_with_base.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - replaces the base authority with encoded credentials
--FILE--
<?php

$base = new Uri\WhatWg\Url("https://old:secret@example.com:81/a?old#old");
$builder = new Uri\WhatWg\UrlBuilder();
$builder->setHost("example.net");
$builder->setUsername("a@b");
$builder->setPassword("c:d");
$url = $builder->build($base);

var_dump($url->toAsciiString());
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
var_dump($base->toAsciiString());

?>
--EXPECT--
string(32) "https://a%40b:c%3Ad@example.net/"
bool(true)
string(43) "https://old:secret@example.com:81/a?old#old"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - empty reference with an opaque base
--FILE--
<?php

$base = new Uri\WhatWg\Url("foo:opaque?old#old");
$builder = new Uri\WhatWg\UrlBuilder();

try {
$builder->build($base);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors[0]->type);
}

?>
--EXPECT--
Uri\WhatWg\InvalidUrlException: The specified path is malformed (MissingSchemeNonRelativeUrl)
enum(Uri\WhatWg\UrlValidationErrorType::MissingSchemeNonRelativeUrl)
17 changes: 17 additions & 0 deletions ext/uri/tests/whatwg/builder/basic_error_with_opaque_base.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test Uri\WhatWg\UrlBuilder basic - error - with base URL containing opaque path
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setPath("/foo/bar/baz");

try {
$builder->build(new Uri\WhatWg\Url("scheme:opaque-path"));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

?>
--EXPECT--
Uri\WhatWg\InvalidUrlException: The specified path is malformed (MissingSchemeNonRelativeUrl)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - inherits the path and query but not the fragment
--FILE--
<?php

$base = new Uri\WhatWg\Url("https://example.com/a?old#old");
$builder = new Uri\WhatWg\UrlBuilder();

$url = $builder->build($base);

var_dump($url->toAsciiString());
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECT--
string(25) "https://example.com/a?old"
bool(true)
2 changes: 0 additions & 2 deletions ext/uri/tests/whatwg/builder/basic_success_with_base.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
--TEST--
Test Uri\WhatWg\UrlBuilder basic - success - with base URL
--XFAIL--
Support for passing $baseUrl to Uri\WhatWg\UrlBuilder::build() is not implemented yet.
--FILE--
<?php

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Test Uri\WhatWg\UrlBuilder basic - success - with scheme relative URL
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setHost("example.net");
$builder->setPath("/foo/bar/baz");
$builder->setPort(124);
$url = $builder->build(new Uri\WhatWg\Url("https://user:pass@example.com:123/foo/bar?query#hash"));

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECTF--
string(35) "https://example.net:124/foo/bar/baz"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
string(11) "example.net"
["port"]=>
int(124)
["path"]=>
string(12) "/foo/bar/baz"
["query"]=>
NULL
["fragment"]=>
NULL
}
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - preserves soft errors output with an opaque base URL
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setPath("/a\tb");
$softErrors = ["previous error"];

try {
$builder->build(new Uri\WhatWg\Url("foo:opaque"), $softErrors);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
foreach ($e->errors as $error) {
var_dump($error->type);
}
}

var_dump($softErrors);

?>
--EXPECT--
Uri\WhatWg\InvalidUrlException: The specified path is malformed (MissingSchemeNonRelativeUrl)
enum(Uri\WhatWg\UrlValidationErrorType::MissingSchemeNonRelativeUrl)
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
array(0) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - soft errors assigned to a typed property with a base URL
--FILE--
<?php

class Foo {
public string $errors = "unchanged";
}

$foo = new Foo();
$builder = new Uri\WhatWg\UrlBuilder();
$builder->setFragment("a\tb");
try {
$builder->build(new Uri\WhatWg\Url("https://example.com/"), $foo->errors);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
var_dump($foo->errors);

?>
--EXPECT--
TypeError: Cannot assign array to reference held by property Foo::$errors of type string
string(9) "unchanged"
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - collects and resets soft errors with a base URL
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setPath("/a\tb");
$builder->setFragment("x\ny");
$base = new Uri\WhatWg\Url("https://example.com/");
$softErrors = ["previous error"];
$url = $builder->build($base, $softErrors);

var_dump($url->toAsciiString());
var_dump($softErrors);

$builder->setPath("/ab");
$builder->setFragment("xy");
$builder->build($base, $softErrors);
var_dump($softErrors);

?>
--EXPECTF--
string(25) "https://example.com/ab#xy"
array(2) {
[0]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(2) " b"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
[1]=>
object(Uri\WhatWg\UrlValidationError)#%d (%d) {
["context"]=>
string(2) "
y"
["type"]=>
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
["failure"]=>
bool(false)
}
}
array(0) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - replaces the base fragment with an empty fragment
--FILE--
<?php

$base = new Uri\WhatWg\Url("https://example.com/a?old#old");
$builder = new Uri\WhatWg\UrlBuilder();
$builder->setFragment("");
$url = $builder->build($base);

var_dump($url->toAsciiString());
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECT--
string(26) "https://example.com/a?old#"
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - success - resolves an empty fragment against an opaque base
--FILE--
<?php

$base = new Uri\WhatWg\Url("foo:opaque?old#old");
$builder = new Uri\WhatWg\UrlBuilder();
$builder->setPath("\t\n");
$builder->setFragment("");
$url = $builder->build($base, $softErrors);
var_dump($url->toAsciiString());
var_dump($softErrors[0]->type);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));
var_dump($base->toAsciiString());

?>
--EXPECT--
string(15) "foo:opaque?old#"
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
bool(true)
string(18) "foo:opaque?old#old"
35 changes: 35 additions & 0 deletions ext/uri/tests/whatwg/builder/fragment_success_with_base.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setFragment() - success - with base URL
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setFragment("foo");
$url = $builder->build(new Uri\WhatWg\Url("https://example.com/#bar"));

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECTF--
string(24) "https://example.com/#foo"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(5) "https"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
string(11) "example.com"
["port"]=>
NULL
["path"]=>
string(1) "/"
["query"]=>
NULL
["fragment"]=>
string(3) "foo"
}
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::setFragment() - success - with base URL with opaque path
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setFragment("foo");
$url = $builder->build(new Uri\WhatWg\Url("scheme:opaque-path"));

var_dump($url->toAsciiString());
var_dump($url);
var_dump($url->equals(new Uri\WhatWg\Url($url->toAsciiString())));

?>
--EXPECTF--
string(22) "scheme:opaque-path#foo"
object(Uri\WhatWg\Url)#%d (%d) {
["scheme"]=>
string(6) "scheme"
["username"]=>
NULL
["password"]=>
NULL
["host"]=>
NULL
["port"]=>
NULL
["path"]=>
string(11) "opaque-path"
["query"]=>
NULL
["fragment"]=>
string(3) "foo"
}
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - rejects credentials after host normalization with a base URL
--FILE--
<?php

$builder = new Uri\WhatWg\UrlBuilder();
$builder->setHost("\t");
$builder->setUsername("user");

try {
$builder->build(new Uri\WhatWg\Url("foo://example.com/"));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
foreach ($e->errors as $error) {
var_dump($error->type);
}
}

?>
--EXPECT--
Uri\WhatWg\InvalidUrlException: The specified URL cannot have username
enum(Uri\WhatWg\UrlValidationErrorType::InvalidUrlUnit)
20 changes: 20 additions & 0 deletions ext/uri/tests/whatwg/builder/host_error_with_opaque_base.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Test Uri\WhatWg\UrlBuilder::build() - error - new authority with an opaque base
--FILE--
<?php

$base = new Uri\WhatWg\Url("foo:opaque?old#old");
$builder = new Uri\WhatWg\UrlBuilder();
$builder->setHost("example.com");

try {
$builder->build($base);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
var_dump($e->errors[0]->type);
}

?>
--EXPECT--
Uri\WhatWg\InvalidUrlException: The specified host is malformed (MissingSchemeNonRelativeUrl)
enum(Uri\WhatWg\UrlValidationErrorType::MissingSchemeNonRelativeUrl)
Loading
Loading