2
0

ServerUrlTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require_once 'PHPUnit/Framework/TestCase.php';
  3. require_once 'Zend/Controller/Front.php';
  4. require_once 'Zend/View/Helper/ServerUrl.php';
  5. /**
  6. * Tests Zend_View_Helper_ServerUrl
  7. */
  8. class Zend_View_Helper_ServerUrlTest extends PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * Back up of $_SERVER
  12. *
  13. * @var array
  14. */
  15. protected $_serverBackup;
  16. /**
  17. * Prepares the environment before running a test.
  18. */
  19. protected function setUp()
  20. {
  21. $this->_serverBackup = $_SERVER;
  22. unset($_SERVER['HTTPS']);
  23. }
  24. /**
  25. * Cleans up the environment after running a test.
  26. */
  27. protected function tearDown()
  28. {
  29. $_SERVER = $this->_serverBackup;
  30. }
  31. public function testConstructorWithOnlyHost()
  32. {
  33. $_SERVER['HTTP_HOST'] = 'example.com';
  34. $url = new Zend_View_Helper_ServerUrl();
  35. $this->assertEquals('http://example.com', $url->serverUrl());
  36. }
  37. public function testConstructorWithOnlyHostIncludingPort()
  38. {
  39. $_SERVER['HTTP_HOST'] = 'example.com:8000';
  40. $url = new Zend_View_Helper_ServerUrl();
  41. $this->assertEquals('http://example.com:8000', $url->serverUrl());
  42. }
  43. public function testConstructorWithHostAndHttpsOn()
  44. {
  45. $_SERVER['HTTP_HOST'] = 'example.com';
  46. $_SERVER['HTTPS'] = 'on';
  47. $url = new Zend_View_Helper_ServerUrl();
  48. $this->assertEquals('https://example.com', $url->serverUrl());
  49. }
  50. public function testConstructorWithHostAndHttpsTrue()
  51. {
  52. $_SERVER['HTTP_HOST'] = 'example.com';
  53. $_SERVER['HTTPS'] = true;
  54. $url = new Zend_View_Helper_ServerUrl();
  55. $this->assertEquals('https://example.com', $url->serverUrl());
  56. }
  57. public function testConstructorWithHostIncludingPortAndHttpsTrue()
  58. {
  59. $_SERVER['HTTP_HOST'] = 'example.com:8181';
  60. $_SERVER['HTTPS'] = true;
  61. $url = new Zend_View_Helper_ServerUrl();
  62. $this->assertEquals('https://example.com:8181', $url->serverUrl());
  63. }
  64. public function testConstructorWithHttpHostAndServerNameAndPortSet()
  65. {
  66. $_SERVER['HTTP_HOST'] = 'example.com';
  67. $_SERVER['SERVER_NAME'] = 'example.org';
  68. $_SERVER['SERVER_PORT'] = 8080;
  69. $url = new Zend_View_Helper_ServerUrl();
  70. $this->assertEquals('http://example.com', $url->serverUrl());
  71. }
  72. public function testConstructorWithNoHttpHostButServerNameAndPortSet()
  73. {
  74. unset($_SERVER['HTTP_HOST']);
  75. $_SERVER['SERVER_NAME'] = 'example.org';
  76. $_SERVER['SERVER_PORT'] = 8080;
  77. $url = new Zend_View_Helper_ServerUrl();
  78. $this->assertEquals('http://example.org:8080', $url->serverUrl());
  79. }
  80. public function testServerUrlWithTrueParam()
  81. {
  82. $_SERVER['HTTPS'] = 'off';
  83. $_SERVER['HTTP_HOST'] = 'example.com';
  84. $_SERVER['REQUEST_URI'] = '/foo.html';
  85. $url = new Zend_View_Helper_ServerUrl();
  86. $this->assertEquals('http://example.com/foo.html', $url->serverUrl(true));
  87. }
  88. public function testServerUrlWithInteger()
  89. {
  90. $_SERVER['HTTPS'] = 'off';
  91. $_SERVER['HTTP_HOST'] = 'example.com';
  92. $_SERVER['REQUEST_URI'] = '/foo.html';
  93. $url = new Zend_View_Helper_ServerUrl();
  94. $this->assertEquals('http://example.com', $url->serverUrl(1337));
  95. }
  96. public function testServerUrlWithObject()
  97. {
  98. $_SERVER['HTTPS'] = 'off';
  99. $_SERVER['HTTP_HOST'] = 'example.com';
  100. $_SERVER['REQUEST_URI'] = '/foo.html';
  101. $url = new Zend_View_Helper_ServerUrl();
  102. $this->assertEquals('http://example.com', $url->serverUrl(new stdClass()));
  103. }
  104. }