ServerUrlTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * HttpUtils' tests
  4. */
  5. require_once 'application/HttpUtils.php';
  6. /**
  7. * Unitary tests for server_url()
  8. */
  9. class ServerUrlTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * Detect if the server uses SSL
  13. */
  14. public function testHttpsScheme()
  15. {
  16. $this->assertEquals(
  17. 'https://host.tld',
  18. server_url(
  19. array(
  20. 'HTTPS' => 'ON',
  21. 'SERVER_NAME' => 'host.tld',
  22. 'SERVER_PORT' => '443'
  23. )
  24. )
  25. );
  26. $this->assertEquals(
  27. 'https://host.tld:8080',
  28. server_url(
  29. array(
  30. 'HTTPS' => 'ON',
  31. 'SERVER_NAME' => 'host.tld',
  32. 'SERVER_PORT' => '8080'
  33. )
  34. )
  35. );
  36. }
  37. /**
  38. * Detect a Proxy that sets Forwarded-Host
  39. */
  40. public function testHttpsProxyForwardedHost()
  41. {
  42. $this->assertEquals(
  43. 'https://host.tld:8080',
  44. server_url(
  45. array(
  46. 'HTTP_X_FORWARDED_PROTO' => 'https',
  47. 'HTTP_X_FORWARDED_PORT' => '8080',
  48. 'HTTP_X_FORWARDED_HOST' => 'host.tld'
  49. )
  50. )
  51. );
  52. $this->assertEquals(
  53. 'https://host.tld:4974',
  54. server_url(
  55. array(
  56. 'HTTP_X_FORWARDED_PROTO' => 'https, https',
  57. 'HTTP_X_FORWARDED_PORT' => '4974, 80',
  58. 'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com'
  59. )
  60. )
  61. );
  62. }
  63. /**
  64. * Detect a Proxy with SSL enabled
  65. */
  66. public function testHttpsProxyForward()
  67. {
  68. $this->assertEquals(
  69. 'https://host.tld:8080',
  70. server_url(
  71. array(
  72. 'HTTPS' => 'Off',
  73. 'SERVER_NAME' => 'host.tld',
  74. 'SERVER_PORT' => '80',
  75. 'HTTP_X_FORWARDED_PROTO' => 'https',
  76. 'HTTP_X_FORWARDED_PORT' => '8080'
  77. )
  78. )
  79. );
  80. $this->assertEquals(
  81. 'https://host.tld',
  82. server_url(
  83. array(
  84. 'HTTPS' => 'Off',
  85. 'SERVER_NAME' => 'host.tld',
  86. 'SERVER_PORT' => '80',
  87. 'HTTP_X_FORWARDED_PROTO' => 'https'
  88. )
  89. )
  90. );
  91. $this->assertEquals(
  92. 'https://host.tld',
  93. server_url(
  94. array(
  95. 'HTTPS' => 'Off',
  96. 'SERVER_NAME' => 'host.tld',
  97. 'SERVER_PORT' => '80',
  98. 'HTTP_X_FORWARDED_PROTO' => 'https',
  99. 'HTTP_X_FORWARDED_PORT' => '443'
  100. )
  101. )
  102. );
  103. $this->assertEquals(
  104. 'https://host.tld:4974',
  105. server_url(
  106. array(
  107. 'HTTPS' => 'Off',
  108. 'SERVER_NAME' => 'host.tld',
  109. 'SERVER_PORT' => '80',
  110. 'HTTP_X_FORWARDED_PROTO' => 'https, https',
  111. 'HTTP_X_FORWARDED_PORT' => '4974, 80'
  112. )
  113. )
  114. );
  115. }
  116. /**
  117. * Detect if the server uses a specific port (!= 80)
  118. */
  119. public function testPort()
  120. {
  121. // HTTP
  122. $this->assertEquals(
  123. 'http://host.tld:8080',
  124. server_url(
  125. array(
  126. 'HTTPS' => 'OFF',
  127. 'SERVER_NAME' => 'host.tld',
  128. 'SERVER_PORT' => '8080'
  129. )
  130. )
  131. );
  132. // HTTPS
  133. $this->assertEquals(
  134. 'https://host.tld:8080',
  135. server_url(
  136. array(
  137. 'HTTPS' => 'ON',
  138. 'SERVER_NAME' => 'host.tld',
  139. 'SERVER_PORT' => '8080'
  140. )
  141. )
  142. );
  143. }
  144. /**
  145. * HTTP server on port 80
  146. */
  147. public function testStandardHttpPort()
  148. {
  149. $this->assertEquals(
  150. 'http://host.tld',
  151. server_url(
  152. array(
  153. 'HTTPS' => 'OFF',
  154. 'SERVER_NAME' => 'host.tld',
  155. 'SERVER_PORT' => '80'
  156. )
  157. )
  158. );
  159. }
  160. /**
  161. * HTTPS server on port 443
  162. */
  163. public function testStandardHttpsPort()
  164. {
  165. $this->assertEquals(
  166. 'https://host.tld',
  167. server_url(
  168. array(
  169. 'HTTPS' => 'ON',
  170. 'SERVER_NAME' => 'host.tld',
  171. 'SERVER_PORT' => '443'
  172. )
  173. )
  174. );
  175. }
  176. /**
  177. * Misconfigured server (see #1022): Proxy HTTP but 443
  178. */
  179. public function testHttpWithPort433()
  180. {
  181. $this->assertEquals(
  182. 'https://host.tld',
  183. server_url(
  184. array(
  185. 'HTTPS' => 'Off',
  186. 'SERVER_NAME' => 'host.tld',
  187. 'SERVER_PORT' => '80',
  188. 'HTTP_X_FORWARDED_PROTO' => 'http',
  189. 'HTTP_X_FORWARDED_PORT' => '443'
  190. )
  191. )
  192. );
  193. $this->assertEquals(
  194. 'https://host.tld',
  195. server_url(
  196. array(
  197. 'HTTPS' => 'Off',
  198. 'SERVER_NAME' => 'host.tld',
  199. 'SERVER_PORT' => '80',
  200. 'HTTP_X_FORWARDED_PROTO' => 'https, http',
  201. 'HTTP_X_FORWARDED_PORT' => '443, 80'
  202. )
  203. )
  204. );
  205. }
  206. }