From 51753e403fa69c0ce124ede27d300477e3e799ca Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 00:34:53 +0100 Subject: namespacing: move HTTP utilities along \Shaarli\Http\ classes Signed-off-by: VirtualTam --- tests/HttpUtils/ClientIpIdTest.php | 52 ------- tests/HttpUtils/GetHttpUrlTest.php | 65 -------- tests/HttpUtils/GetIpAdressFromProxyTest.php | 59 ------- tests/HttpUtils/IndexUrlTest.php | 72 --------- tests/HttpUtils/IsHttpsTest.php | 36 ----- tests/HttpUtils/PageUrlTest.php | 76 --------- tests/HttpUtils/ServerUrlTest.php | 221 --------------------------- 7 files changed, 581 deletions(-) delete mode 100644 tests/HttpUtils/ClientIpIdTest.php delete mode 100644 tests/HttpUtils/GetHttpUrlTest.php delete mode 100644 tests/HttpUtils/GetIpAdressFromProxyTest.php delete mode 100644 tests/HttpUtils/IndexUrlTest.php delete mode 100644 tests/HttpUtils/IsHttpsTest.php delete mode 100644 tests/HttpUtils/PageUrlTest.php delete mode 100644 tests/HttpUtils/ServerUrlTest.php (limited to 'tests/HttpUtils') diff --git a/tests/HttpUtils/ClientIpIdTest.php b/tests/HttpUtils/ClientIpIdTest.php deleted file mode 100644 index c15ac5cc..00000000 --- a/tests/HttpUtils/ClientIpIdTest.php +++ /dev/null @@ -1,52 +0,0 @@ -assertEquals( - '10.1.167.42', - client_ip_id(['REMOTE_ADDR' => '10.1.167.42']) - ); - } - - /** - * Get a remote client ID based on its IP and proxy information (1) - */ - public function testClientIpIdRemoteForwarded() - { - $this->assertEquals( - '10.1.167.42_127.0.1.47', - client_ip_id([ - 'REMOTE_ADDR' => '10.1.167.42', - 'HTTP_X_FORWARDED_FOR' => '127.0.1.47' - ]) - ); - } - - /** - * Get a remote client ID based on its IP and proxy information (2) - */ - public function testClientIpIdRemoteForwardedClient() - { - $this->assertEquals( - '10.1.167.42_10.1.167.56_127.0.1.47', - client_ip_id([ - 'REMOTE_ADDR' => '10.1.167.42', - 'HTTP_X_FORWARDED_FOR' => '10.1.167.56', - 'HTTP_CLIENT_IP' => '127.0.1.47' - ]) - ); - } -} diff --git a/tests/HttpUtils/GetHttpUrlTest.php b/tests/HttpUtils/GetHttpUrlTest.php deleted file mode 100644 index ea53de5f..00000000 --- a/tests/HttpUtils/GetHttpUrlTest.php +++ /dev/null @@ -1,65 +0,0 @@ -assertEquals('Invalid HTTP Url', $headers[0]); - $this->assertFalse($content); - - // Non HTTP - list($headers, $content) = get_http_response('ftp://save.tld/mysave', 1); - $this->assertEquals('Invalid HTTP Url', $headers[0]); - $this->assertFalse($content); - } - - /** - * Get an invalid remote URL - */ - public function testGetInvalidRemoteUrl() - { - list($headers, $content) = @get_http_response('http://non.existent', 1); - $this->assertFalse($headers); - $this->assertFalse($content); - } - - /** - * Test getAbsoluteUrl with relative target URL. - */ - public function testGetAbsoluteUrlWithRelative() - { - $origin = 'http://non.existent/blabla/?test'; - $target = '/stuff.php'; - - $expected = 'http://non.existent/stuff.php'; - $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); - - $target = 'stuff.php'; - $expected = 'http://non.existent/blabla/stuff.php'; - $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); - } - - /** - * Test getAbsoluteUrl with absolute target URL. - */ - public function testGetAbsoluteUrlWithAbsolute() - { - $origin = 'http://non.existent/blabla/?test'; - $target = 'http://other.url/stuff.php'; - - $this->assertEquals($target, getAbsoluteUrl($origin, $target)); - } -} diff --git a/tests/HttpUtils/GetIpAdressFromProxyTest.php b/tests/HttpUtils/GetIpAdressFromProxyTest.php deleted file mode 100644 index 7af5bd9d..00000000 --- a/tests/HttpUtils/GetIpAdressFromProxyTest.php +++ /dev/null @@ -1,59 +0,0 @@ -assertFalse(getIpAddressFromProxy(array(), array())); - } - - /** - * Test with a single IP in proxy header. - */ - public function testWithOneForwardedIp() - { - $ip = '1.1.1.1'; - $server = array('HTTP_X_FORWARDED_FOR' => $ip); - $this->assertEquals($ip, getIpAddressFromProxy($server, array())); - } - - /** - * Test with a multiple IPs in proxy header. - */ - public function testWithMultipleForwardedIp() - { - $ip = '1.1.1.1'; - $ip2 = '2.2.2.2'; - - $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); - $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); - - $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2); - $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); - } - - /** - * Test with a trusted IP address. - */ - public function testWithTrustedIp() - { - $ip = '1.1.1.1'; - $ip2 = '2.2.2.2'; - - $server = array('HTTP_X_FORWARDED_FOR' => $ip); - $this->assertFalse(getIpAddressFromProxy($server, array($ip))); - - $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); - $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip))); - $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2))); - } -} diff --git a/tests/HttpUtils/IndexUrlTest.php b/tests/HttpUtils/IndexUrlTest.php deleted file mode 100644 index 337dcab0..00000000 --- a/tests/HttpUtils/IndexUrlTest.php +++ /dev/null @@ -1,72 +0,0 @@ -assertEquals( - 'http://host.tld/', - index_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/index.php' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/', - index_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/index.php' - ) - ) - ); - } - - /** - * The resource is != "index.php" - */ - public function testOtherResource() - { - $this->assertEquals( - 'http://host.tld/page.php', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/page.php' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/page.php', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/page.php' - ) - ) - ); - } -} diff --git a/tests/HttpUtils/IsHttpsTest.php b/tests/HttpUtils/IsHttpsTest.php deleted file mode 100644 index 097f2bcf..00000000 --- a/tests/HttpUtils/IsHttpsTest.php +++ /dev/null @@ -1,36 +0,0 @@ -assertTrue(is_https(['HTTPS' => true])); - $this->assertTrue(is_https(['HTTPS' => '1'])); - $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 443])); - $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443'])); - $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443,123,456,'])); - } - - /** - * Test is_https with HTTP values. - */ - public function testIsHttpsFalse() - { - $this->assertFalse(is_https([])); - $this->assertFalse(is_https(['HTTPS' => false])); - $this->assertFalse(is_https(['HTTPS' => '0'])); - $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 123])); - $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '123'])); - $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => ',123,456,'])); - } -} diff --git a/tests/HttpUtils/PageUrlTest.php b/tests/HttpUtils/PageUrlTest.php deleted file mode 100644 index 4dbbe9cf..00000000 --- a/tests/HttpUtils/PageUrlTest.php +++ /dev/null @@ -1,76 +0,0 @@ -assertEquals( - 'http://host.tld/?p1=v1&p2=v2', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/index.php', - 'QUERY_STRING' => 'p1=v1&p2=v2' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/?action=edit_tag', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/index.php', - 'QUERY_STRING' => 'action=edit_tag' - ) - ) - ); - } - - /** - * The resource is != "index.php" - */ - public function testOtherResource() - { - $this->assertEquals( - 'http://host.tld/page.php?p1=v1&p2=v2', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/page.php', - 'QUERY_STRING' => 'p1=v1&p2=v2' - ) - ) - ); - - $this->assertEquals( - 'http://host.tld/admin/page.php?action=edit_tag', - page_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/admin/page.php', - 'QUERY_STRING' => 'action=edit_tag' - ) - ) - ); - } -} diff --git a/tests/HttpUtils/ServerUrlTest.php b/tests/HttpUtils/ServerUrlTest.php deleted file mode 100644 index 324b827a..00000000 --- a/tests/HttpUtils/ServerUrlTest.php +++ /dev/null @@ -1,221 +0,0 @@ -assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '443' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080' - ) - ) - ); - } - - /** - * Detect a Proxy that sets Forwarded-Host - */ - public function testHttpsProxyForwardedHost() - { - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTP_X_FORWARDED_PROTO' => 'https', - 'HTTP_X_FORWARDED_PORT' => '8080', - 'HTTP_X_FORWARDED_HOST' => 'host.tld' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld:4974', - server_url( - array( - 'HTTP_X_FORWARDED_PROTO' => 'https, https', - 'HTTP_X_FORWARDED_PORT' => '4974, 80', - 'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com' - ) - ) - ); - } - - /** - * Detect a Proxy with SSL enabled - */ - public function testHttpsProxyForward() - { - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https', - 'HTTP_X_FORWARDED_PORT' => '8080' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https', - 'HTTP_X_FORWARDED_PORT' => '443' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld:4974', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https, https', - 'HTTP_X_FORWARDED_PORT' => '4974, 80' - ) - ) - ); - } - - /** - * Detect if the server uses a specific port (!= 80) - */ - public function testPort() - { - // HTTP - $this->assertEquals( - 'http://host.tld:8080', - server_url( - array( - 'HTTPS' => 'OFF', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080' - ) - ) - ); - - // HTTPS - $this->assertEquals( - 'https://host.tld:8080', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080' - ) - ) - ); - } - - /** - * HTTP server on port 80 - */ - public function testStandardHttpPort() - { - $this->assertEquals( - 'http://host.tld', - server_url( - array( - 'HTTPS' => 'OFF', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80' - ) - ) - ); - } - - /** - * HTTPS server on port 443 - */ - public function testStandardHttpsPort() - { - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'ON', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '443' - ) - ) - ); - } - - /** - * Misconfigured server (see #1022): Proxy HTTP but 443 - */ - public function testHttpWithPort433() - { - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'http', - 'HTTP_X_FORWARDED_PORT' => '443' - ) - ) - ); - - $this->assertEquals( - 'https://host.tld', - server_url( - array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'HTTP_X_FORWARDED_PROTO' => 'https, http', - 'HTTP_X_FORWARDED_PORT' => '443, 80' - ) - ) - ); - } -} -- cgit v1.2.3