ServerUrlTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2010 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 dirname(__FILE__) . '/../../../TestHelper.php';
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. require_once 'Zend/Controller/Front.php';
  25. require_once 'Zend/View/Helper/ServerUrl.php';
  26. /**
  27. * Tests Zend_View_Helper_ServerUrl
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_View
  35. * @group Zend_View_Helper
  36. */
  37. class Zend_View_Helper_ServerUrlTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Back up of $_SERVER
  41. *
  42. * @var array
  43. */
  44. protected $_serverBackup;
  45. /**
  46. * Prepares the environment before running a test.
  47. */
  48. protected function setUp()
  49. {
  50. $this->_serverBackup = $_SERVER;
  51. unset($_SERVER['HTTPS']);
  52. }
  53. /**
  54. * Cleans up the environment after running a test.
  55. */
  56. protected function tearDown()
  57. {
  58. $_SERVER = $this->_serverBackup;
  59. }
  60. public function testConstructorWithOnlyHost()
  61. {
  62. $_SERVER['HTTP_HOST'] = 'example.com';
  63. $url = new Zend_View_Helper_ServerUrl();
  64. $this->assertEquals('http://example.com', $url->serverUrl());
  65. }
  66. public function testConstructorWithOnlyHostIncludingPort()
  67. {
  68. $_SERVER['HTTP_HOST'] = 'example.com:8000';
  69. $url = new Zend_View_Helper_ServerUrl();
  70. $this->assertEquals('http://example.com:8000', $url->serverUrl());
  71. }
  72. public function testConstructorWithHostAndHttpsOn()
  73. {
  74. $_SERVER['HTTP_HOST'] = 'example.com';
  75. $_SERVER['HTTPS'] = 'on';
  76. $url = new Zend_View_Helper_ServerUrl();
  77. $this->assertEquals('https://example.com', $url->serverUrl());
  78. }
  79. public function testConstructorWithHostAndHttpsTrue()
  80. {
  81. $_SERVER['HTTP_HOST'] = 'example.com';
  82. $_SERVER['HTTPS'] = true;
  83. $url = new Zend_View_Helper_ServerUrl();
  84. $this->assertEquals('https://example.com', $url->serverUrl());
  85. }
  86. public function testConstructorWithHostIncludingPortAndHttpsTrue()
  87. {
  88. $_SERVER['HTTP_HOST'] = 'example.com:8181';
  89. $_SERVER['HTTPS'] = true;
  90. $url = new Zend_View_Helper_ServerUrl();
  91. $this->assertEquals('https://example.com:8181', $url->serverUrl());
  92. }
  93. public function testConstructorWithHttpHostAndServerNameAndPortSet()
  94. {
  95. $_SERVER['HTTP_HOST'] = 'example.com';
  96. $_SERVER['SERVER_NAME'] = 'example.org';
  97. $_SERVER['SERVER_PORT'] = 8080;
  98. $url = new Zend_View_Helper_ServerUrl();
  99. $this->assertEquals('http://example.com', $url->serverUrl());
  100. }
  101. public function testConstructorWithNoHttpHostButServerNameAndPortSet()
  102. {
  103. unset($_SERVER['HTTP_HOST']);
  104. $_SERVER['SERVER_NAME'] = 'example.org';
  105. $_SERVER['SERVER_PORT'] = 8080;
  106. $url = new Zend_View_Helper_ServerUrl();
  107. $this->assertEquals('http://example.org:8080', $url->serverUrl());
  108. }
  109. public function testServerUrlWithTrueParam()
  110. {
  111. $_SERVER['HTTPS'] = 'off';
  112. $_SERVER['HTTP_HOST'] = 'example.com';
  113. $_SERVER['REQUEST_URI'] = '/foo.html';
  114. $url = new Zend_View_Helper_ServerUrl();
  115. $this->assertEquals('http://example.com/foo.html', $url->serverUrl(true));
  116. }
  117. public function testServerUrlWithInteger()
  118. {
  119. $_SERVER['HTTPS'] = 'off';
  120. $_SERVER['HTTP_HOST'] = 'example.com';
  121. $_SERVER['REQUEST_URI'] = '/foo.html';
  122. $url = new Zend_View_Helper_ServerUrl();
  123. $this->assertEquals('http://example.com', $url->serverUrl(1337));
  124. }
  125. public function testServerUrlWithObject()
  126. {
  127. $_SERVER['HTTPS'] = 'off';
  128. $_SERVER['HTTP_HOST'] = 'example.com';
  129. $_SERVER['REQUEST_URI'] = '/foo.html';
  130. $url = new Zend_View_Helper_ServerUrl();
  131. $this->assertEquals('http://example.com', $url->serverUrl(new stdClass()));
  132. }
  133. /**
  134. * @group ZF-9919
  135. */
  136. public function testServerUrlWithScheme()
  137. {
  138. $_SERVER['HTTP_SCHEME'] = 'https';
  139. $_SERVER['HTTP_HOST'] = 'example.com';
  140. $url = new Zend_View_Helper_ServerUrl();
  141. $this->assertEquals('https://example.com', $url->serverUrl());
  142. }
  143. /**
  144. * @group ZF-9919
  145. */
  146. public function testServerUrlWithPort()
  147. {
  148. $_SERVER['SERVER_PORT'] = 443;
  149. $_SERVER['HTTP_HOST'] = 'example.com';
  150. $url = new Zend_View_Helper_ServerUrl();
  151. $this->assertEquals('https://example.com', $url->serverUrl());
  152. }
  153. }