2
0

ServerUrlTest.php 4.6 KB

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