From 945f5cd7bf3375fd4362e0da3b279969d6284d4c Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 30 Apr 2017 08:24:22 -0700 Subject: [PATCH] add tests for `url.php` --- src/url.php | 49 +++++++++++--------- src/utils.php | 7 +++ tests/URLTest.php | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 20 deletions(-) create mode 100644 tests/URLTest.php diff --git a/src/url.php b/src/url.php index 91618e7..fd43abb 100644 --- a/src/url.php +++ b/src/url.php @@ -26,8 +26,28 @@ function add_query_params_to_url($url, $add_params) { return build_url($parts); } +function strip_tracking_params($url) { + $parts = parse_url($url); + + if(!array_key_exists('query', $parts)) + return $url; + + parse_str($parts['query'], $params); + + $new_params = []; + + foreach($params as $key=>$val) { + if(substr($key, 0, 4) != 'utm_') + $new_params[$key] = $val; + } + + $parts['query'] = http_build_query($new_params); + + return build_url($parts); +} + // Input: Any URL or string like "aaronparecki.com" -// Output: Normlized URL (default to http if no scheme, force "/" path) +// Output: Normalized URL (default to http if no scheme, force "/" path) // or return false if not a valid URL function normalize($url) { $parts = parse_url($url); @@ -57,22 +77,18 @@ function normalize($url) { // Inverse of parse_url() // http://php.net/parse_url function build_url($parsed_url) { - $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; - $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; - $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; - $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; - $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; + $scheme = !empty($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; + $host = !empty($parsed_url['host']) ? $parsed_url['host'] : ''; + $port = !empty($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; + $user = !empty($parsed_url['user']) ? $parsed_url['user'] : ''; + $pass = !empty($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; $pass = ($user || $pass) ? "$pass@" : ''; - $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; - $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; - $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; + $path = !empty($parsed_url['path']) ? $parsed_url['path'] : ''; + $query = !empty($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; + $fragment = !empty($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; return "$scheme$user$pass$host$port$path$query$fragment"; } -function parse($url) { - return parse_url($url); -} - function host_matches($a, $b) { return parse_url($a, PHP_URL_HOST) == parse_url($b, PHP_URL_HOST); } @@ -81,13 +97,6 @@ function is_url($url) { return is_string($url) && preg_match('/^https?:\/\/[a-z0-9\.\-]\/?/', $url); } -function http_header_case($str) { - $str = str_replace('-', ' ', $str); - $str = ucwords($str); - $str = str_replace(' ', '-', $str); - return $str; -} - function is_public_ip($ip) { // http://stackoverflow.com/a/30143143 diff --git a/src/utils.php b/src/utils.php index 7eebee2..8b52147 100644 --- a/src/utils.php +++ b/src/utils.php @@ -84,6 +84,13 @@ function flash($key) { } } +function http_header_case($str) { + $str = str_replace('-', ' ', $str); + $str = ucwords($str); + $str = str_replace(' ', '-', $str); + return $str; +} + function html_to_dom_document($html) { // Parse the source body as HTML $doc = new DOMDocument(); diff --git a/tests/URLTest.php b/tests/URLTest.php new file mode 100644 index 0000000..f650d5b --- /dev/null +++ b/tests/URLTest.php @@ -0,0 +1,112 @@ +assertEquals('example.com', $url); + $url = p3k\url\display_url('http://example.com/'); + $this->assertEquals('example.com', $url); + $url = p3k\url\display_url('example.com'); + $this->assertEquals('example.com', $url); + $url = p3k\url\display_url('http://example.com/foo'); + $this->assertEquals('example.com/foo', $url); + $url = p3k\url\display_url('http://example.com/foo/'); + $this->assertEquals('example.com/foo/', $url); + } + + public function testAddQueryParamsToURLNoExistingParams() { + $url = p3k\url\add_query_params_to_url('http://example.com', ['q'=>1]); + $this->assertEquals('http://example.com?q=1', $url); + $url = p3k\url\add_query_params_to_url('http://example.com/', ['q'=>1]); + $this->assertEquals('http://example.com/?q=1', $url); + $url = p3k\url\add_query_params_to_url('http://example.com/foo', ['q'=>1]); + $this->assertEquals('http://example.com/foo?q=1', $url); + $url = p3k\url\add_query_params_to_url('http://example.com/foo#fragment', ['q'=>1]); + $this->assertEquals('http://example.com/foo?q=1#fragment', $url); + } + + public function testAddQueryParamsToURLWithExistingParams() { + $url = p3k\url\add_query_params_to_url('http://example.com?a=b', ['q'=>1]); + $this->assertEquals('http://example.com?a=b&q=1', $url); + $url = p3k\url\add_query_params_to_url('http://example.com/?a=b', ['q'=>1]); + $this->assertEquals('http://example.com/?a=b&q=1', $url); + $url = p3k\url\add_query_params_to_url('http://example.com/foo?a=b', ['q'=>1]); + $this->assertEquals('http://example.com/foo?a=b&q=1', $url); + $url = p3k\url\add_query_params_to_url('http://example.com/foo?a=b#fragment', ['q'=>1]); + $this->assertEquals('http://example.com/foo?a=b&q=1#fragment', $url); + } + + public function testStripTrackingParams() { + $url = p3k\url\strip_tracking_params('http://example.com/'); + $this->assertEquals('http://example.com/', $url); + $url = p3k\url\strip_tracking_params('http://example.com/?utm_source=foo'); + $this->assertEquals('http://example.com/', $url); + $url = p3k\url\strip_tracking_params('http://example.com/?foo=bar'); + $this->assertEquals('http://example.com/?foo=bar', $url); + $url = p3k\url\strip_tracking_params('http://example.com/?foo=bar&utm_source=froogle'); + $this->assertEquals('http://example.com/?foo=bar', $url); + } + + public function testNormalizeURL() { + $url = p3k\url\normalize('http://example.com/'); + $this->assertEquals('http://example.com/', $url); + $url = p3k\url\normalize('http://example.com'); + $this->assertEquals('http://example.com/', $url); + $url = p3k\url\normalize('example.com'); + $this->assertEquals('http://example.com/', $url); + $url = p3k\url\normalize('mailto:user@example.com'); + $this->assertEquals(false, $url); + } + + public function testBuildURL() { + $parts = p3k\url\build_url(parse_url('http://example.com')); + $this->assertEquals('http://example.com', $parts); + $parts = p3k\url\build_url(parse_url('http://example.com/')); + $this->assertEquals('http://example.com/', $parts); + $parts = p3k\url\build_url(parse_url('https://example.com/?')); + $this->assertEquals('https://example.com/', $parts); + $parts = p3k\url\build_url(parse_url('https://example.com/?foo=bar')); + $this->assertEquals('https://example.com/?foo=bar', $parts); + $parts = p3k\url\build_url(parse_url('https://example.com?foo=bar')); + $this->assertEquals('https://example.com?foo=bar', $parts); + $parts = p3k\url\build_url(parse_url('https://user:pass@example.com/?foo=bar')); + $this->assertEquals('https://user:pass@example.com/?foo=bar', $parts); + $parts = p3k\url\build_url(parse_url('https://user:pass@example.com:3000/?foo=bar#f')); + $this->assertEquals('https://user:pass@example.com:3000/?foo=bar#f', $parts); + $parts = p3k\url\build_url(parse_url('https://user@example.com/?foo=bar')); + $this->assertEquals('https://user@example.com/?foo=bar', $parts); + $parts = p3k\url\build_url(parse_url('https://user:@example.com/?foo=bar')); + $this->assertEquals('https://user@example.com/?foo=bar', $parts); + } + + public function testHostMatches() { + $this->assertTrue(p3k\url\host_matches('http://example.com/', 'https://example.com/foo')); + $this->assertFalse(p3k\url\host_matches('http://example.com/', 'https://subdomain.example.com/foo')); + } + + public function testIsURL() { + $this->assertTrue(p3k\url\is_url('http://example.com/')); + $this->assertTrue(p3k\url\is_url('http://example')); + $this->assertTrue(p3k\url\is_url('https://example.com/foo?a=b#f')); + $this->assertFalse(p3k\url\is_url('mailto:user@example.com')); + $this->assertFalse(p3k\url\is_url('geo:45.5,-122.6')); + } + + public function testIsPublicIP() { + $this->assertTrue(p3k\url\is_public_ip('45.1.200.42')); + $this->assertFalse(p3k\url\is_public_ip('192.168.200.1')); + $this->assertFalse(p3k\url\is_public_ip('127.0.0.1')); + $this->assertFalse(p3k\url\is_public_ip('0.10.0.0')); + $this->assertFalse(p3k\url\is_public_ip('10.10.0.0')); + } + + public function testGeoToLatLng() { + $coords = p3k\url\geo_to_latlng('geo:45.521296,-122.626412'); + $this->assertEquals(['latitude'=>45.521296, 'longitude'=>-122.626412], $coords); + $coords = p3k\url\geo_to_latlng('geo:45.521296,-122.626412;u=35'); + $this->assertEquals(['latitude'=>45.521296, 'longitude'=>-122.626412], $coords); + $coords = p3k\url\geo_to_latlng('http://example.com/'); + $this->assertEquals(false, $coords); + } + +}