ServerUrlTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Controller/Front.php';
  23. require_once 'Zend/View/Helper/ServerUrl.php';
  24. /**
  25. * Tests Zend_View_Helper_ServerUrl
  26. *
  27. * @category Zend
  28. * @package Zend_View
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_View
  33. * @group Zend_View_Helper
  34. */
  35. class Zend_View_Helper_ServerUrlTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Back up of $_SERVER
  39. *
  40. * @var array
  41. */
  42. protected $_serverBackup;
  43. /**
  44. * Prepares the environment before running a test.
  45. */
  46. protected function setUp()
  47. {
  48. $this->_serverBackup = $_SERVER;
  49. unset($_SERVER['HTTPS']);
  50. }
  51. /**
  52. * Cleans up the environment after running a test.
  53. */
  54. protected function tearDown()
  55. {
  56. $_SERVER = $this->_serverBackup;
  57. }
  58. public function testConstructorWithOnlyHost()
  59. {
  60. $_SERVER['HTTP_HOST'] = 'example.com';
  61. $url = new Zend_View_Helper_ServerUrl();
  62. $this->assertEquals('http://example.com', $url->serverUrl());
  63. }
  64. public function testConstructorWithOnlyHostIncludingPort()
  65. {
  66. $_SERVER['HTTP_HOST'] = 'example.com:8000';
  67. $url = new Zend_View_Helper_ServerUrl();
  68. $this->assertEquals('http://example.com:8000', $url->serverUrl());
  69. }
  70. public function testConstructorWithHostAndHttpsOn()
  71. {
  72. $_SERVER['HTTP_HOST'] = 'example.com';
  73. $_SERVER['HTTPS'] = 'on';
  74. $url = new Zend_View_Helper_ServerUrl();
  75. $this->assertEquals('https://example.com', $url->serverUrl());
  76. }
  77. public function testConstructorWithHostAndHttpsTrue()
  78. {
  79. $_SERVER['HTTP_HOST'] = 'example.com';
  80. $_SERVER['HTTPS'] = true;
  81. $url = new Zend_View_Helper_ServerUrl();
  82. $this->assertEquals('https://example.com', $url->serverUrl());
  83. }
  84. public function testConstructorWithHostIncludingPortAndHttpsTrue()
  85. {
  86. $_SERVER['HTTP_HOST'] = 'example.com:8181';
  87. $_SERVER['HTTPS'] = true;
  88. $url = new Zend_View_Helper_ServerUrl();
  89. $this->assertEquals('https://example.com:8181', $url->serverUrl());
  90. }
  91. public function testConstructorWithHttpHostAndServerNameAndPortSet()
  92. {
  93. $_SERVER['HTTP_HOST'] = 'example.com';
  94. $_SERVER['SERVER_NAME'] = 'example.org';
  95. $_SERVER['SERVER_PORT'] = 8080;
  96. $url = new Zend_View_Helper_ServerUrl();
  97. $this->assertEquals('http://example.com', $url->serverUrl());
  98. }
  99. public function testConstructorWithNoHttpHostButServerNameAndPortSet()
  100. {
  101. unset($_SERVER['HTTP_HOST']);
  102. $_SERVER['SERVER_NAME'] = 'example.org';
  103. $_SERVER['SERVER_PORT'] = 8080;
  104. $url = new Zend_View_Helper_ServerUrl();
  105. $this->assertEquals('http://example.org:8080', $url->serverUrl());
  106. }
  107. public function testServerUrlWithTrueParam()
  108. {
  109. $_SERVER['HTTPS'] = 'off';
  110. $_SERVER['HTTP_HOST'] = 'example.com';
  111. $_SERVER['REQUEST_URI'] = '/foo.html';
  112. $url = new Zend_View_Helper_ServerUrl();
  113. $this->assertEquals('http://example.com/foo.html', $url->serverUrl(true));
  114. }
  115. public function testServerUrlWithInteger()
  116. {
  117. $_SERVER['HTTPS'] = 'off';
  118. $_SERVER['HTTP_HOST'] = 'example.com';
  119. $_SERVER['REQUEST_URI'] = '/foo.html';
  120. $url = new Zend_View_Helper_ServerUrl();
  121. $this->assertEquals('http://example.com', $url->serverUrl(1337));
  122. }
  123. public function testServerUrlWithObject()
  124. {
  125. $_SERVER['HTTPS'] = 'off';
  126. $_SERVER['HTTP_HOST'] = 'example.com';
  127. $_SERVER['REQUEST_URI'] = '/foo.html';
  128. $url = new Zend_View_Helper_ServerUrl();
  129. $this->assertEquals('http://example.com', $url->serverUrl(new stdClass()));
  130. }
  131. /**
  132. * @group ZF-9919
  133. */
  134. public function testServerUrlWithScheme()
  135. {
  136. $_SERVER['HTTP_SCHEME'] = 'https';
  137. $_SERVER['HTTP_HOST'] = 'example.com';
  138. $url = new Zend_View_Helper_ServerUrl();
  139. $this->assertEquals('https://example.com', $url->serverUrl());
  140. }
  141. /**
  142. * @group ZF-9919
  143. */
  144. public function testServerUrlWithPort()
  145. {
  146. $_SERVER['SERVER_PORT'] = 443;
  147. $_SERVER['HTTP_HOST'] = 'example.com';
  148. $url = new Zend_View_Helper_ServerUrl();
  149. $this->assertEquals('https://example.com', $url->serverUrl());
  150. }
  151. }