PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Http/PhpEnvironment/RequestTest.php

http://github.com/zendframework/zf2
PHP | 227 lines | 183 code | 12 blank | 32 comment | 0 complexity | f54af7a46a619625b66ade7d7643c58d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @namespace
  4. */
  5. namespace ZendTest\Http\PhpEnvironment;
  6. use PHPUnit_Framework_TestCase as TestCase,
  7. Zend\Http\PhpEnvironment\Request;
  8. class RequestTest extends TestCase
  9. {
  10. /**
  11. * Original environemnt
  12. *
  13. * @var array
  14. */
  15. protected $originalEnvironment;
  16. /**
  17. * Save the original environment and set up a clean one.
  18. */
  19. public function setUp()
  20. {
  21. $this->originalEnvironment = array(
  22. 'post' => $_POST,
  23. 'get' => $_GET,
  24. 'cookie' => $_COOKIE,
  25. 'server' => $_SERVER,
  26. 'env' => $_ENV,
  27. );
  28. $_POST = array();
  29. $_GET = array();
  30. $_COOKIE = array();
  31. $_SERVER = array();
  32. $_ENV = array();
  33. }
  34. /**
  35. * Restore the original environment
  36. */
  37. public function tearDown()
  38. {
  39. $_POST = $this->originalEnvironment['post'];
  40. $_GET = $this->originalEnvironment['get'];
  41. $_COOKIE = $this->originalEnvironment['cookie'];
  42. $_SERVER = $this->originalEnvironment['server'];
  43. $_ENV = $this->originalEnvironment['env'];
  44. }
  45. /**
  46. * Data provider for testing base URL and path detection.
  47. */
  48. public static function baseUrlandPathProvider()
  49. {
  50. return array(
  51. array(
  52. array(
  53. 'REQUEST_URI' => '/index.php/news/3?var1=val1&var2=val2',
  54. 'QUERY_URI' => 'var1=val1&var2=val2',
  55. 'SCRIPT_NAME' => '/index.php',
  56. 'PHP_SELF' => '/index.php/news/3',
  57. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  58. ),
  59. '/index.php',
  60. ''
  61. ),
  62. array(
  63. array(
  64. 'REQUEST_URI' => '/public/index.php/news/3?var1=val1&var2=val2',
  65. 'QUERY_URI' => 'var1=val1&var2=val2',
  66. 'SCRIPT_NAME' => '/public/index.php',
  67. 'PHP_SELF' => '/public/index.php/news/3',
  68. 'SCRIPT_FILENAME' => '/var/web/html/public/index.php',
  69. ),
  70. '/public/index.php',
  71. '/public'
  72. ),
  73. array(
  74. array(
  75. 'REQUEST_URI' => '/index.php/news/3?var1=val1&var2=val2',
  76. 'SCRIPT_NAME' => '/home.php',
  77. 'PHP_SELF' => '/index.php/news/3',
  78. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  79. ),
  80. '/index.php',
  81. ''
  82. ),
  83. array(
  84. array(
  85. 'REQUEST_URI' => '/index.php/news/3?var1=val1&var2=val2',
  86. 'SCRIPT_NAME' => '/home.php',
  87. 'PHP_SELF' => '/home.php',
  88. 'ORIG_SCRIPT_NAME' => '/index.php',
  89. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  90. ),
  91. '/index.php',
  92. ''
  93. ),
  94. array(
  95. array(
  96. 'REQUEST_URI' => '/index.php/news/3?var1=val1&var2=val2',
  97. 'PHP_SELF' => '/index.php/news/3',
  98. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  99. ),
  100. '/index.php',
  101. ''
  102. ),
  103. array(
  104. array(
  105. 'HTTP_X_REWRITE_URL' => '/index.php/news/3?var1=val1&var2=val2',
  106. 'PHP_SELF' => '/index.php/news/3',
  107. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  108. ),
  109. '/index.php',
  110. ''
  111. ),
  112. array(
  113. array(
  114. 'ORIG_PATH_INFO' => '/index.php/news/3',
  115. 'QUERY_STRING' => 'var1=val1&var2=val2',
  116. 'PHP_SELF' => '/index.php/news/3',
  117. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  118. ),
  119. '/index.php',
  120. ''
  121. ),
  122. array(
  123. array(
  124. 'REQUEST_URI' => '/article/archive?foo=index.php',
  125. 'QUERY_STRING' => 'foo=index.php',
  126. 'SCRIPT_FILENAME' => '/var/www/zftests/index.php',
  127. ),
  128. '',
  129. ''
  130. ),
  131. array(
  132. array(
  133. 'REQUEST_URI' => '/html/index.php/news/3?var1=val1&var2=val2',
  134. 'PHP_SELF' => '/html/index.php/news/3',
  135. 'SCRIPT_FILENAME' => '/var/web/html/index.php',
  136. ),
  137. '/html/index.php',
  138. '/html'
  139. ),
  140. array(
  141. array(
  142. 'REQUEST_URI' => '/dir/action',
  143. 'PHP_SELF' => '/dir/index.php',
  144. 'SCRIPT_FILENAME' => '/var/web/dir/index.php',
  145. ),
  146. '/dir',
  147. '/dir'
  148. ),
  149. );
  150. }
  151. /**
  152. * @dataProvider baseUrlandPathProvider
  153. * @param array $server
  154. * @param string $baseUrl
  155. * @param string $basePath
  156. */
  157. public function testBasePathDetection(array $server, $baseUrl, $basePath)
  158. {
  159. $_SERVER = $server;
  160. $request = new Request();
  161. $this->assertEquals($baseUrl, $request->getBaseUrl());
  162. $this->assertEquals($basePath, $request->getBasePath());
  163. }
  164. /**
  165. * Data provider for testing server provided headers.
  166. */
  167. public static function serverHeaderProvider()
  168. {
  169. return array(
  170. array(
  171. array(
  172. 'HTTP_USER_AGENT' => 'Dummy',
  173. ),
  174. 'User-Agent',
  175. 'Dummy'
  176. ),
  177. array(
  178. array(
  179. 'CONTENT_TYPE' => 'text/html',
  180. ),
  181. 'Content-Type',
  182. 'text/html'
  183. ),
  184. array(
  185. array(
  186. 'CONTENT_LENGTH' => 12,
  187. ),
  188. 'Content-Length',
  189. 12
  190. ),
  191. array(
  192. array(
  193. 'CONTENT_MD5' => md5('a'),
  194. ),
  195. 'Content-MD5',
  196. md5('a')
  197. ),
  198. );
  199. }
  200. /**
  201. * @dataProvider serverHeaderProvider
  202. * @param array $server
  203. * @param string $name
  204. * @param string $value
  205. */
  206. public function testHeadersWithMinus(array $server, $name, $value)
  207. {
  208. $_SERVER = $server;
  209. $request = new Request();
  210. $header = $request->headers()->get($name);
  211. $this->assertNotEquals($header, false);
  212. $this->assertEquals($name, $header->getFieldName($value));
  213. $this->assertEquals($value, $header->getFieldValue($value));
  214. }
  215. }