Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active October 6, 2015 11:28
Show Gist options
  • Save craiga/2986522 to your computer and use it in GitHub Desktop.
Save craiga/2986522 to your computer and use it in GitHub Desktop.
normaliseUrl
<?php
/**
* Normalise a URL.
*
* Normalise a URL. Returns null if the URL couldn't be normalised.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/2986522
*/
function normaliseUrl($url, $defaultComponents = array(), $overrideComponents = array(), $hostTranslationMap = array())
{
$result = null;
// old parameter list was $url, $defaultHost, $defaultScheme, $hostTranlationMap
if (is_string($defaultComponents)) {
$defaultComponents = array("host" => $defaultComponents);
}
if (is_string($overrideComponents)) {
$defaultComponents["scheme"] = $overrideComponents;
$overrideComponents = array();
}
// parse the URL if required
if (is_array($url)) {
$parsedUrl = $url;
} else {
$parsedUrl = @parse_url($url);
}
if (is_array($parsedUrl)) {
// remove empty parts of the parsed URL
foreach ($parsedUrl as $key => $value) {
if ($value == "") {
unset($parsedUrl[$key]);
}
}
// override parts of the URL
foreach ($overrideComponents as $componentKey => $componentValue) {
$parsedUrl[$componentKey] = $componentValue;
}
// attempt to populate missing default components from request URI.
if (isset($_SERVER["REQUEST_URI"])) {
$parsedRequestUrl = @parse_url($_SERVER["REQUEST_URI"]);
if (is_array($parsedRequestUrl)) {
// set default scheme and host
foreach ($parsedRequestUrl as $componentKey => $componentValue) {
if (!isset($defaultComponents[$componentKey]) && in_array($componentKey, array("scheme", "host"))) {
$defaultComponents[$componentKey] = $componentValue;
}
}
// if scheme and host match parsed scheme and host, set default port and user
if (isset($parsedUrl["scheme"]) && isset($parsedRequestUrl["scheme"]) && $parsedUrl["scheme"] == $parsedRequestUrl["scheme"] &&
isset($parsedUrl["host"]) && isset($parsedRequestUrl["host"]) && $parsedUrl["host"] == $parsedRequestUrl["host"]) {
foreach ($parsedRequestUrl as $componentKey => $componentValue) {
if (!isset($defaultComponents[$componentKey]) && in_array($componentKey, array("port", "user"))) {
$defaultComponents[$componentKey] = $componentValue;
}
}
// if user in request URL matches parsed or default user, set default password
if (isset($parsedRequestUrl["pass"]) && !isset($defaultComponents["pass"])) {
$user = null;
if (isset($parsedUrl["user"])) {
$user = $parsedUrl["user"];
} elseif (isset($defaultComponents["user"])) {
$user = $defaultComponents["user"];
}
if ($user == $parsedRequestUrl["user"]) {
$defaultComponents["pass"] = $parsedRequestUrl["pass"];
}
}
}
}
}
// if no default scheme, assume http
if (!isset($defaultComponents["scheme"])) {
$defaultComponents["scheme"] = "http";
}
// inject default components if required
foreach ($defaultComponents as $componentKey => $componentValue) {
if (!isset($parsedUrl[$componentKey])) {
$parsedUrl[$componentKey] = $componentValue;
}
}
// scheme and host are always lowercase
$lowercasedParts = array("scheme", "host");
foreach ($lowercasedParts as $lowercasedPart) {
if (isset($parsedUrl[$lowercasedPart])) {
$parsedUrl[$lowercasedPart] = strtolower($parsedUrl[$lowercasedPart]);
}
}
// determine requirements of scheme
$emptyPathIsSlash = in_array($parsedUrl["scheme"], array("http", "https", "ftp"));
$pathMustStartWithSlash = in_array($parsedUrl["scheme"], array("http", "https", "ftp"));
$schemeRequiresHost = in_array($parsedUrl["scheme"], array("http", "https", "ftp"));
$schemeRequiresDoubleSlash = in_array($parsedUrl["scheme"], array("http", "https", "ftp"));
$stripIndexFileFromPath = in_array($parsedUrl["scheme"], array("http", "https"));
$stripTrailingSlashFromPath = in_array($parsedUrl["scheme"], array("http", "https"));
$removeDoubleSlashesFromPath = in_array($parsedUrl["scheme"], array("http", "https"));
$defaultPortMap = array("http" => 80, "https" => 443);
$portIsDefault = !isset($parsedUrl["port"]) || (isset($defaultPortMap[$parsedUrl["scheme"]]) && $defaultPortMap[$parsedUrl["scheme"]] == $parsedUrl["port"]);
// remove index file from URL
if ($stripIndexFileFromPath && isset($parsedUrl["path"])) {
$parsedUrl["path"] = preg_replace("/(index|default)\.[^\.\/]+$/i", "", $parsedUrl["path"]);
}
// strip trailing slash (note that this should happen before injecting slashes)
if ($stripTrailingSlashFromPath && isset($parsedUrl["path"])) {
$parsedUrl["path"] = preg_replace("/\/$/", "", $parsedUrl["path"]);
}
// replace empty path with slash
if ($emptyPathIsSlash) {
if (!isset($parsedUrl["path"]) || strlen($parsedUrl["path"]) < 1) {
$parsedUrl["path"] = "/";
}
}
// make sure path starts with slash
if ($pathMustStartWithSlash && isset($parsedUrl["path"]) && !preg_match("/^\//", $parsedUrl["path"])) {
$parsedUrl["path"] = "/" . $parsedUrl["path"];
}
// remove double slashes
if ($removeDoubleSlashesFromPath && isset($parsedUrl["path"])) {
$parsedUrl["path"] = preg_replace("/\/\//", "/", $parsedUrl["path"]);
}
// remove default port
if ($portIsDefault) {
unset($parsedUrl["port"]);
}
// apply host translation
if (isset($parsedUrl["host"]) && isset($hostTranslationMap[$parsedUrl["host"]])) {
$parsedUrl["host"] = $hostTranslationMap[$parsedUrl["host"]];
}
// if scheme requires a host and one isn't present, throw an exception
if (!$schemeRequiresHost || isset($parsedUrl["host"])) {
// build result!
$result = $parsedUrl["scheme"] . ":";
if ($schemeRequiresDoubleSlash) {
$result .= "//";
}
if (isset($parsedUrl["user"])) {
$result .= $parsedUrl["user"];
if (isset($parsedUrl["pass"])) {
$result .= ":" . $parsedUrl["pass"];
}
$result .= "@";
}
if (isset($parsedUrl["host"])) {
$result .= $parsedUrl["host"];
}
if (isset($parsedUrl["port"])) {
$result .= ":" . $parsedUrl["port"];
}
if (isset($parsedUrl["path"])) {
$path = $parsedUrl["path"];
$result .= $path;
}
if (isset($parsedUrl["query"])) {
$result .= "?" . $parsedUrl["query"];
}
if (isset($parsedUrl["fragment"])) {
$result .= "#" . $parsedUrl["fragment"];
}
// unescape or capitalise percent-encoded octets
$result = preg_replace_callback("/%[0-f][0-f]/i", function($matches) {
$encoded = strtoupper($matches[0]);
$decoded = rawurldecode($encoded);
if ($encoded != rawurlencode($decoded)) {
$encoded = $decoded;
}
return $encoded;
}, $result);
}
}
return $result;
}
<?php
function exception_error_handler($errno, $errstr, $errfile, $errline )
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
require_once("normaliseUrl.php");
/**
* Assert that normaliseUrl works as expected.
*/
function assertNormaliseUrlResult($input, $expected, $defaultComponents = array(), $overrideComponents = array(), $hostTranslationMap = array())
{
$fail = false;
$actual = normaliseUrl($input, $defaultComponents, $overrideComponents, $hostTranslationMap);
if ($actual != $expected) {
printf("\nFAIL!\n\n");
printf("normaliseUrl(\"%s\", %s, %s, %s) != \"%s\"\n\n", $input, var_export($defaultComponents, true), var_export($overrideComponents, true), var_export($hostTranslationMap, true), $expected);
printf("normaliseUrl(\"%s\", %s, %s, %s) == \"%s\"\n\n", $input, var_export($defaultComponents, true), var_export($overrideComponents, true), var_export($hostTranslationMap, true), $actual);
$fail = true;
}
return $fail;
}
$numFails = 0;
$tests = array(
"http://craiga.id.au/" => "http://craiga.id.au/",
"http://craiga.id.au" => "http://craiga.id.au/",
"http://craiga.id.au/index.htm" => "http://craiga.id.au/",
"http://craiga.id.au/INDEX.HTM" => "http://craiga.id.au/",
"http://craiga.id.au/index.html" => "http://craiga.id.au/",
"http://craiga.id.au/index.HTML" => "http://craiga.id.au/",
"http://craiga.id.au/index.php" => "http://craiga.id.au/",
"http://craiga.id.au/index.asp" => "http://craiga.id.au/",
"http://craiga.id.au/index.aspx" => "http://craiga.id.au/",
"http://craiga.id.au/default.htm" => "http://craiga.id.au/",
"http://craiga.id.au/default.html" => "http://craiga.id.au/",
"http://craiga.id.au/default.php" => "http://craiga.id.au/",
"http://craiga.id.au/default.asp" => "http://craiga.id.au/",
"http://craiga.id.au/DEFAULT.ASP" => "http://craiga.id.au/",
"http://craiga.id.au/default.aspx" => "http://craiga.id.au/",
"http://craiga.id.au/something/index.html" => "http://craiga.id.au/something",
"http://craiga.id.au/indexes.html" => "http://craiga.id.au/indexes.html",
"http://craiga.id.au/index.php/something" => "http://craiga.id.au/index.php/something",
"http://craiga.id.au/dir/" => "http://craiga.id.au/dir",
"http://craiga.id.au/file.txt/" => "http://craiga.id.au/file.txt",
"HTTP://CRAIGA.ID.AU/YELLING" => "http://craiga.id.au/YELLING",
"mailto:[email protected]" => "mailto:[email protected]",
"ftp://craiga:[email protected]/" => "ftp://craiga:[email protected]/",
"ftp://craiga:[email protected]" => "ftp://craiga:[email protected]/",
"http://craiga.id.au:80/xyz" => "http://craiga.id.au/xyz",
"https://craiga.id.au:443/xyz" => "https://craiga.id.au/xyz",
"/search?q=craig+anderson#result" => null,
"search?q=craig+anderson#result" => null,
"/" => null,
"" => null,
null => null,
"http://www.example.com/a%c2%b1b" => "http://www.example.com/a%C2%B1b",
"http://www.example.com/%7Eusername" => "http://www.example.com/~username",
"http://www.example.com/foo//bar.html" => "http://www.example.com/foo/bar.html",
"http://www.example.com/display?" => "http://www.example.com/display"
);
foreach ($tests as $input => $expected) {
printf("Testing \"%s\" => \"%s\"\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$testsDefaultScheme = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"craiga.id.au/xyz" => $scheme . ":craiga.id.au/xyz",
);
foreach ($testsDefaultScheme as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default scheme\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("scheme" => $scheme));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("scheme" => $scheme));
}
$scheme = uniqid("scheme");
$testsOverrideScheme = array(
"http://craiga.id.au/xyz" => $scheme . ":craiga.id.au/xyz",
"craiga.id.au/xyz" => $scheme . ":craiga.id.au/xyz",
);
foreach ($testsOverrideScheme as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden scheme\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("scheme" => $scheme));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("scheme" => $scheme));
}
$host = uniqid("host");
$testsDefaultHost = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"/xyz" => "http://" . $host . "/xyz",
);
foreach ($testsDefaultHost as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default host\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("host" => $host));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("host" => $host));
}
$host = uniqid("host");
$testsOverrideHost = array(
"http://craiga.id.au/xyz" => "http://" . $host . "/xyz",
"/xyz" => "http://" . $host . "/xyz",
);
foreach ($testsOverrideHost as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden host\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("host" => $host));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("host" => $host));
}
$port = rand(100, 9999);
$testsDefaultPort = array(
"http://craiga.id.au:8080/xyz" => "http://craiga.id.au:8080/xyz",
"http://craiga.id.au/xyz" => "http://craiga.id.au:" . $port ."/xyz",
);
foreach ($testsDefaultPort as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default port\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("port" => $port));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("port" => $port));
}
$port = rand(100, 9999);
$testsOverridePort = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au:" . $port ."/xyz",
"http://craiga.id.au:8080/xyz" => "http://craiga.id.au:" . $port ."/xyz",
);
foreach ($testsOverridePort as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden port\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("port" => $port));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("port" => $port));
}
$user = uniqid("user");
$testsDefaultUser = array(
"http://[email protected]/xyz" => "http://[email protected]/xyz",
"http://craiga.id.au/xyz" => "http://" . $user . "@craiga.id.au/xyz",
);
foreach ($testsDefaultUser as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default user\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("user" => $user));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("user" => $user));
}
$user = uniqid("user");
$testsOverrideUser = array(
"http://[email protected]/xyz" => "http://" . $user . "@craiga.id.au/xyz",
"http://craiga.id.au/xyz" => "http://" . $user . "@craiga.id.au/xyz",
);
foreach ($testsOverrideUser as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden user\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("user" => $user));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("user" => $user));
}
$pass = uniqid("pass");
$testsDefaultPass = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"http://[email protected]/xyz" => "http://user:" . $pass . "@craiga.id.au/xyz",
"http://user:[email protected]/xyz" => "http://user:[email protected]/xyz",
);
foreach ($testsDefaultPass as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default pass\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("pass" => $pass));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("pass" => $pass));
}
$pass = uniqid("pass");
$testsOverridePass = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"http://[email protected]/xyz" => "http://user:" . $pass . "@craiga.id.au/xyz",
"http://user:[email protected]/xyz" => "http://user:" . $pass . "@craiga.id.au/xyz",
);
foreach ($testsOverridePass as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden pass\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("pass" => $pass));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("pass" => $pass));
}
$path = uniqid("path");
$testsDefaultPath = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"http://craiga.id.au/" => "http://craiga.id.au/",
"http://craiga.id.au" => "http://craiga.id.au/" . $path,
);
foreach ($testsDefaultPath as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default path\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("path" => $path));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("path" => $path));
}
$path = uniqid("path");
$testsOverridePath = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/" . $path,
"http://craiga.id.au/" => "http://craiga.id.au/" . $path,
"http://craiga.id.au" => "http://craiga.id.au/" . $path,
);
foreach ($testsOverridePath as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden path\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("path" => $path));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("path" => $path));
}
$query = uniqid("query");
$testsDefaultQuery = array(
"http://craiga.id.au/xyz?abc" => "http://craiga.id.au/xyz?abc",
"http://craiga.id.au/xyz?" => "http://craiga.id.au/xyz?" . $query,
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz?" . $query,
);
foreach ($testsDefaultQuery as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default query\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("query" => $query));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("query" => $query));
}
$query = uniqid("query");
$testsOverrideQuery = array(
"http://craiga.id.au/xyz?abc" => "http://craiga.id.au/xyz?" . $query,
"http://craiga.id.au/xyz?" => "http://craiga.id.au/xyz?" . $query,
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz?" . $query,
);
foreach ($testsOverrideQuery as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden query\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("query" => $query));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("query" => $query));
}
$fragment = uniqid("fragment");
$testsDefaultFragment = array(
"http://craiga.id.au/xyz#abc" => "http://craiga.id.au/xyz#abc",
"http://craiga.id.au/xyz#" => "http://craiga.id.au/xyz#" . $fragment,
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz#" . $fragment,
);
foreach ($testsDefaultFragment as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with default fragment\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array("fragment" => $fragment));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array("fragment" => $fragment));
}
$fragment = uniqid("fragment");
$testsOverrideFragment = array(
"http://craiga.id.au/xyz#abc" => "http://craiga.id.au/xyz#" . $fragment,
"http://craiga.id.au/xyz#" => "http://craiga.id.au/xyz#" . $fragment,
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz#" . $fragment,
);
foreach ($testsOverrideFragment as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with overridden fragment\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array("fragment" => $fragment));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array("fragment" => $fragment));
}
$scheme = uniqid("scheme");
$_SERVER = array("REQUEST_URI" => sprintf("%s:%s/%s", $scheme, uniqid("host"), uniqid("path")));
$testsDefaultSchemeFromRequest = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"craiga.id.au/xyz" => $scheme . ":craiga.id.au/xyz",
);
foreach ($testsDefaultSchemeFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with scheme from request URI\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$host = uniqid("host");
$scheme = uniqid("scheme");
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s/%s", $scheme, $host, uniqid("path")));
$testsDefaultHostAndSchemeFromRequest = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"/xyz" => $scheme . ":" . $host . "/xyz",
);
foreach ($testsDefaultHostAndSchemeFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with host and scheme in request URI\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$host = uniqid("host");
$port = rand(100, 9999);
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s:%d/%s", $scheme, $host, $port, uniqid("path")));
$testsDefaultPortFromRequest = array(
"http://craiga.id.au:8080/xyz" => "http://craiga.id.au:8080/xyz",
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"http://" . $host . "/xyz" => "http://" . $host . "/xyz",
$scheme . "://" . $host . "/xyz" => $scheme . ":" . $host . ":" . $port . "/xyz",
);
foreach ($testsDefaultPortFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with port in request\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$host = uniqid("host");
$user = uniqid("user");
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s@%s/%s", $scheme, $user, $host, uniqid("path")));
$testsDefaultUserFromRequest = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"http://" . $host . "/xyz" => "http://" . $host . "/xyz",
$scheme . "://" . $host . "/xyz" => $scheme . ":" . $user . "@" . $host . "/xyz",
$scheme . "://user@" . $host . "/xyz" => $scheme . ":user@" . $host . "/xyz",
);
foreach ($testsDefaultUserFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with user in request\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$host = uniqid("host");
$user = uniqid("user");
$pass = uniqid("pass");
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s:%s@%s/%s", $scheme, $user, $pass, $host, uniqid("path")));
$testsDefaultUserAndPassFromRequest = array(
"http://craiga.id.au/xyz" => "http://craiga.id.au/xyz",
"http://" . $host . "/xyz" => "http://" . $host . "/xyz",
$scheme . "://" . $host . "/xyz" => $scheme . ":" . $user . ":" . $pass . "@" . $host . "/xyz",
$scheme . "://" . $user . "@" . $host . "/xyz" => $scheme . ":" . $user . ":" . $pass . "@" . $host . "/xyz",
$scheme . "://" . $user . ":pass@" . $host . "/xyz" => $scheme . ":" . $user . ":pass@" . $host . "/xyz",
$scheme . "://user@" . $host . "/xyz" => $scheme . ":user@" . $host . "/xyz",
$scheme . "://user:pass@" . $host . "/xyz" => $scheme . ":user:pass@" . $host . "/xyz",
);
foreach ($testsDefaultUserAndPassFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with user and pass in request\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$host = uniqid("host");
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s/%s", $scheme, $host, uniqid("path")));
$testsNoDefaultPathFromRequest = array(
"http://craiga.id.au" => "http://craiga.id.au/",
"http://" . $host => "http://" . $host . "/",
$scheme . "://" . $host => $scheme . ":" . $host
);
foreach ($testsNoDefaultPathFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with path in request\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$host = uniqid("host");
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s/%s?%s", $scheme, $host, uniqid("path"), uniqid("query")));
$testsNoDefaultQueryStringFromRequest = array(
"http://craiga.id.au/abc?xyz" => "http://craiga.id.au/abc?xyz",
"http://" . $host . "/abc" => "http://" . $host . "/abc",
"http://" . $host . "/abc?xyz" => "http://" . $host . "/abc?xyz",
$scheme . "://" . $host . "/abc" => $scheme . ":" . $host . "/abc",
$scheme . "://" . $host . "/abc?xyz" => $scheme . ":" . $host . "/abc?xyz"
);
foreach ($testsNoDefaultPathFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with query string in request\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
$scheme = uniqid("scheme");
$host = uniqid("host");
$_SERVER = array("REQUEST_URI" => sprintf("%s://%s/%s#%s", $scheme, $host, uniqid("path"), uniqid("fragment")));
$testsNoDefaultFragmentFromRequest = array(
"http://craiga.id.au/abc#xyz" => "http://craiga.id.au/abc#xyz",
"http://" . $host . "/abc" => "http://" . $host . "/abc",
"http://" . $host . "/abc#xyz" => "http://" . $host . "/abc#xyz",
$scheme . "://" . $host . "/abc" => $scheme . ":" . $host . "/abc",
$scheme . "://" . $host . "/abc#xyz" => $scheme . ":" . $host . "/abc#xyz"
);
foreach ($testsNoDefaultFragmentFromRequest as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with fragment in request\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected);
$numFails += assertNormaliseUrlResult(parse_url($input), $expected);
}
unset($_SERVER["REQUEST_URI"]);
$testsWithHostMap = array(
"http://www.testhost.net/about/www.testhost.net?from=www.testhost.net" => "http://testhost.net/about/www.testhost.net?from=www.testhost.net"
);
foreach ($testsWithHostMap as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with host map\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, array(), array(), array(
"www.testhost.net" => "testhost.net"
));
$numFails += assertNormaliseUrlResult(parse_url($input), $expected, array(), array(), array(
"www.testhost.net" => "testhost.net"
));
}
$host = uniqid("host");
$testsWithHost = array(
"/search?q=craig+anderson#result" => "http://" . $host . "/search?q=craig+anderson#result",
"search?q=craig+anderson#result" => "http://" . $host . "/search?q=craig+anderson#result",
"/" => "http://" . $host . "/",
"" => "http://" . $host . "/",
null => "http://" . $host . "/"
);
foreach ($testsWithHost as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with old-style host parameter\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, $host);
}
$scheme = uniqid("scheme");
$testsWithScheme = array(
"craiga.id.au/xyz" => $scheme . ":craiga.id.au/xyz"
);
foreach ($testsWithScheme as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with old-style scheme parameter\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, null, $scheme);
}
$host = uniqid("host");
$scheme = uniqid("scheme");
$testsWithHostAndScheme = array(
"/thingo" => $scheme . ":" . $host . "/thingo"
);
foreach ($testsWithHostAndScheme as $input => $expected) {
printf("Testing \"%s\" => \"%s\" with old-style host and scheme parameters\n", $input, $expected);
$numFails += assertNormaliseUrlResult($input, $expected, $host, $scheme);
}
printf ("Testing that an empty query string in an array is ignored");
$numFails += assertNormaliseUrlResult(array("scheme" => "http", "host" => "www.example.com", "path" => "directory", "query" => ""), "http://www.example.com/directory");
printf("\nTests completed. %d test%s failed.\n", $numFails, $numFails != 1 ? "s" : "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment