Browse Source

add tests for `url.php`

pull/1/head
Aaron Parecki 6 years ago
parent
commit
945f5cd7bf
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 148 additions and 20 deletions
  1. +29
    -20
      src/url.php
  2. +7
    -0
      src/utils.php
  3. +112
    -0
      tests/URLTest.php

+ 29
- 20
src/url.php View File

@ -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

+ 7
- 0
src/utils.php View File

@ -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();

+ 112
- 0
tests/URLTest.php View File

@ -0,0 +1,112 @@
<?php
class URLTest extends PHPUnit_Framework_TestCase {
public function testDisplayURL() {
$url = p3k\url\display_url('http://example.com');
$this->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);
}
}

Loading…
Cancel
Save