HTTP: move server URL functions to HttpUtils.php
Relates to #333 Modifications: - refactor server URL utility functions - do not access global `$_SERVER` variables - add test coverage - improve readability - apply coding conventions Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
parent
7b114771d3
commit
482d67bd52
6 changed files with 388 additions and 53 deletions
38
tests/HttpUtils/GetHttpUrlTest.php
Normal file
38
tests/HttpUtils/GetHttpUrlTest.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
/**
|
||||
* HttpUtils' tests
|
||||
*/
|
||||
|
||||
require_once 'application/HttpUtils.php';
|
||||
|
||||
/**
|
||||
* Unitary tests for get_http_url()
|
||||
*/
|
||||
class GetHttpUrlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Get an invalid local URL
|
||||
*/
|
||||
public function testGetInvalidLocalUrl()
|
||||
{
|
||||
list($headers, $content) = get_http_url('/non/existent', 1);
|
||||
$this->assertEquals('HTTP Error', $headers[0]);
|
||||
$this->assertRegexp(
|
||||
'/failed to open stream: No such file or directory/',
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an invalid remote URL
|
||||
*/
|
||||
public function testGetInvalidRemoteUrl()
|
||||
{
|
||||
list($headers, $content) = get_http_url('http://non.existent', 1);
|
||||
$this->assertEquals('HTTP Error', $headers[0]);
|
||||
$this->assertRegexp(
|
||||
'/Name or service not known/',
|
||||
$content
|
||||
);
|
||||
}
|
||||
}
|
72
tests/HttpUtils/IndexUrlTest.php
Normal file
72
tests/HttpUtils/IndexUrlTest.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
/**
|
||||
* HttpUtils' tests
|
||||
*/
|
||||
|
||||
require_once 'application/HttpUtils.php';
|
||||
|
||||
/**
|
||||
* Unitary tests for index_url()
|
||||
*/
|
||||
class IndexUrlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* If on the main page, remove "index.php" from the URL resource
|
||||
*/
|
||||
public function testRemoveIndex()
|
||||
{
|
||||
$this->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'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
76
tests/HttpUtils/PageUrlTest.php
Normal file
76
tests/HttpUtils/PageUrlTest.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
/**
|
||||
* HttpUtils' tests
|
||||
*/
|
||||
|
||||
require_once 'application/HttpUtils.php';
|
||||
|
||||
/**
|
||||
* Unitary tests for page_url()
|
||||
*/
|
||||
class PageUrlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* If on the main page, remove "index.php" from the URL resource
|
||||
*/
|
||||
public function testRemoveIndex()
|
||||
{
|
||||
$this->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'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
135
tests/HttpUtils/ServerUrlTest.php
Normal file
135
tests/HttpUtils/ServerUrlTest.php
Normal file
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
/**
|
||||
* HttpUtils' tests
|
||||
*/
|
||||
|
||||
require_once 'application/HttpUtils.php';
|
||||
|
||||
/**
|
||||
* Unitary tests for server_url()
|
||||
*/
|
||||
class ServerUrlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Detect if the server uses SSL
|
||||
*/
|
||||
public function testHttpsScheme()
|
||||
{
|
||||
$this->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 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'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue