ResponseHeader37.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_Test
  17. * @subpackage PHPUnit
  18. * @copyright Copyright (c) 2005-2014 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. if (version_compare(PHPUnit_Runner_Version::id(), '4.0.0', '<')) {
  23. /** @see PHPUnit_Framework_Constraint */
  24. require_once 'PHPUnit/Framework/Constraint.php';
  25. }
  26. /**
  27. * Response header PHPUnit Constraint
  28. *
  29. * @uses PHPUnit_Framework_Constraint
  30. * @category Zend
  31. * @package Zend_Test
  32. * @subpackage PHPUnit
  33. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Test_PHPUnit_Constraint_ResponseHeader extends PHPUnit_Framework_Constraint
  37. {
  38. /**#@+
  39. * Assertion type constants
  40. */
  41. const ASSERT_RESPONSE_CODE = 'assertResponseCode';
  42. const ASSERT_HEADER = 'assertHeader';
  43. const ASSERT_HEADER_CONTAINS = 'assertHeaderContains';
  44. const ASSERT_HEADER_REGEX = 'assertHeaderRegex';
  45. /**#@-*/
  46. /**
  47. * Current assertion type
  48. * @var string
  49. */
  50. protected $_assertType = null;
  51. /**
  52. * Available assertion types
  53. * @var array
  54. */
  55. protected $_assertTypes = array(
  56. self::ASSERT_RESPONSE_CODE,
  57. self::ASSERT_HEADER,
  58. self::ASSERT_HEADER_CONTAINS,
  59. self::ASSERT_HEADER_REGEX,
  60. );
  61. /**
  62. * @var int Response code
  63. */
  64. protected $_code = 200;
  65. /**
  66. * @var int Actual response code
  67. */
  68. protected $_actualCode = null;
  69. /**
  70. * @var string Header
  71. */
  72. protected $_header = null;
  73. /**
  74. * @var string pattern against which to compare header content
  75. */
  76. protected $_match = null;
  77. /**
  78. * Whether or not assertion is negated
  79. * @var bool
  80. */
  81. protected $_negate = false;
  82. /**
  83. * Constructor; setup constraint state
  84. *
  85. * @return void
  86. */
  87. public function __construct()
  88. {
  89. }
  90. /**
  91. * Indicate negative match
  92. *
  93. * @param bool $flag
  94. * @return void
  95. */
  96. public function setNegate($flag = true)
  97. {
  98. $this->_negate = $flag;
  99. }
  100. /**
  101. * Evaluate an object to see if it fits the constraints
  102. *
  103. * @param object of Zend_Controller_Response_Abstract to be evaluated
  104. * @param null|string Assertion type
  105. * @param int|string HTTP response code to evaluate against | header string (haystack)
  106. * @param string (optional) match (needle), may be required depending on assertion type
  107. * @return bool
  108. * NOTE:
  109. * Drastic changes up to PHPUnit 3.5.15 this was:
  110. * public function evaluate($other, $assertType = null)
  111. * In PHPUnit 3.6.0 they changed the interface into this:
  112. * public function evaluate($other, $description = '', $returnResult = FALSE)
  113. * We use the new interface for PHP-strict checking, but emulate the old one
  114. */
  115. public function evaluate($response, $assertType = '', $variable = FALSE)
  116. {
  117. if (!$response instanceof Zend_Controller_Response_Abstract) {
  118. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  119. throw new Zend_Test_PHPUnit_Constraint_Exception('Header constraint assertions require a response object');
  120. }
  121. if (strstr($assertType, 'Not')) {
  122. $this->setNegate(true);
  123. $assertType = str_replace('Not', '', $assertType);
  124. }
  125. if (!in_array($assertType, $this->_assertTypes)) {
  126. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  127. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  128. }
  129. $this->_assertType = $assertType;
  130. $argv = func_get_args();
  131. $argc = func_num_args();
  132. switch ($assertType) {
  133. case self::ASSERT_RESPONSE_CODE:
  134. if (3 > $argc) {
  135. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  136. throw new Zend_Test_PHPUnit_Constraint_Exception('No response code provided against which to match');
  137. }
  138. $this->_code = $code = $argv[2];
  139. return ($this->_negate)
  140. ? $this->_notCode($response, $code)
  141. : $this->_code($response, $code);
  142. case self::ASSERT_HEADER:
  143. if (3 > $argc) {
  144. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  145. throw new Zend_Test_PHPUnit_Constraint_Exception('No header provided against which to match');
  146. }
  147. $this->_header = $header = $argv[2];
  148. return ($this->_negate)
  149. ? $this->_notHeader($response, $header)
  150. : $this->_header($response, $header);
  151. case self::ASSERT_HEADER_CONTAINS:
  152. if (4 > $argc) {
  153. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  154. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . $assertType);
  155. }
  156. $this->_header = $header = $argv[2];
  157. $this->_match = $match = $argv[3];
  158. return ($this->_negate)
  159. ? $this->_notHeaderContains($response, $header, $match)
  160. : $this->_headerContains($response, $header, $match);
  161. case self::ASSERT_HEADER_REGEX:
  162. if (4 > $argc) {
  163. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  164. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . $assertType);
  165. }
  166. $this->_header = $header = $argv[2];
  167. $this->_match = $match = $argv[3];
  168. return ($this->_negate)
  169. ? $this->_notHeaderRegex($response, $header, $match)
  170. : $this->_headerRegex($response, $header, $match);
  171. default:
  172. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  173. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . $assertType);
  174. }
  175. }
  176. /**
  177. * Report Failure
  178. *
  179. * @see PHPUnit_Framework_Constraint for implementation details
  180. * @param mixed CSS selector path
  181. * @param string Failure description
  182. * @param object Cannot be used, null
  183. * @return void
  184. * @throws PHPUnit_Framework_ExpectationFailedException
  185. * NOTE:
  186. * Drastic changes up to PHPUnit 3.5.15 this was:
  187. * public function fail($other, $description, $not = false)
  188. * In PHPUnit 3.6.0 they changed the interface into this:
  189. * protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)
  190. * We use the new interface for PHP-strict checking
  191. */
  192. public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $cannot_be_used = NULL)
  193. {
  194. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  195. switch ($this->_assertType) {
  196. case self::ASSERT_RESPONSE_CODE:
  197. $failure = 'Failed asserting response code "%s"';
  198. if ($this->_negate) {
  199. $failure = 'Failed asserting response code IS NOT "%s"';
  200. }
  201. $failure = sprintf($failure, $this->_code);
  202. if (!$this->_negate && $this->_actualCode) {
  203. $failure .= sprintf(PHP_EOL . 'Was "%s"', $this->_actualCode);
  204. }
  205. break;
  206. case self::ASSERT_HEADER:
  207. $failure = 'Failed asserting response header "%s" found';
  208. if ($this->_negate) {
  209. $failure = 'Failed asserting response response header "%s" WAS NOT found';
  210. }
  211. $failure = sprintf($failure, $this->_header);
  212. break;
  213. case self::ASSERT_HEADER_CONTAINS:
  214. $failure = 'Failed asserting response header "%s" exists and contains "%s"';
  215. if ($this->_negate) {
  216. $failure = 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"';
  217. }
  218. $failure = sprintf($failure, $this->_header, $this->_match);
  219. break;
  220. case self::ASSERT_HEADER_REGEX:
  221. $failure = 'Failed asserting response header "%s" exists and matches regex "%s"';
  222. if ($this->_negate) {
  223. $failure = 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"';
  224. }
  225. $failure = sprintf($failure, $this->_header, $this->_match);
  226. break;
  227. default:
  228. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
  229. }
  230. if (!empty($description)) {
  231. $failure = $description . "\n" . $failure;
  232. }
  233. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  234. }
  235. /**
  236. * Complete implementation
  237. *
  238. * @return string
  239. */
  240. public function toString()
  241. {
  242. return '';
  243. }
  244. /**
  245. * Compare response code for positive match
  246. *
  247. * @param Zend_Controller_Response_Abstract $response
  248. * @param int $code
  249. * @return bool
  250. */
  251. protected function _code(Zend_Controller_Response_Abstract $response, $code)
  252. {
  253. $test = $this->_getCode($response);
  254. $this->_actualCode = $test;
  255. return ($test == $code);
  256. }
  257. /**
  258. * Compare response code for negative match
  259. *
  260. * @param Zend_Controller_Response_Abstract $response
  261. * @param int $code
  262. * @return bool
  263. */
  264. protected function _notCode(Zend_Controller_Response_Abstract $response, $code)
  265. {
  266. $test = $this->_getCode($response);
  267. return ($test != $code);
  268. }
  269. /**
  270. * Retrieve response code
  271. *
  272. * @param Zend_Controller_Response_Abstract $response
  273. * @return int
  274. */
  275. protected function _getCode(Zend_Controller_Response_Abstract $response)
  276. {
  277. $test = $response->getHttpResponseCode();
  278. if (null === $test) {
  279. $test = 200;
  280. }
  281. return $test;
  282. }
  283. /**
  284. * Positive check for response header presence
  285. *
  286. * @param Zend_Controller_Response_Abstract $response
  287. * @param string $header
  288. * @return bool
  289. */
  290. protected function _header(Zend_Controller_Response_Abstract $response, $header)
  291. {
  292. return (null !== $this->_getHeader($response, $header));
  293. }
  294. /**
  295. * Negative check for response header presence
  296. *
  297. * @param Zend_Controller_Response_Abstract $response
  298. * @param string $header
  299. * @return bool
  300. */
  301. protected function _notHeader(Zend_Controller_Response_Abstract $response, $header)
  302. {
  303. return (null === $this->_getHeader($response, $header));
  304. }
  305. /**
  306. * Retrieve response header
  307. *
  308. * @param Zend_Controller_Response_Abstract $response
  309. * @param string $header
  310. * @return string|null
  311. */
  312. protected function _getHeader(Zend_Controller_Response_Abstract $response, $header)
  313. {
  314. $headers = $response->sendHeaders();
  315. $header = strtolower($header);
  316. if (array_key_exists($header, $headers)) {
  317. return $headers[$header];
  318. }
  319. return null;
  320. }
  321. /**
  322. * Positive check for header contents matching pattern
  323. *
  324. * @param Zend_Controller_Response_Abstract $response
  325. * @param string $header
  326. * @param string $match
  327. * @return bool
  328. */
  329. protected function _headerContains(Zend_Controller_Response_Abstract $response, $header, $match)
  330. {
  331. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  332. return false;
  333. }
  334. $contents = str_replace($header . ': ', '', $fullHeader);
  335. return (strstr($contents, $match) !== false);
  336. }
  337. /**
  338. * Negative check for header contents matching pattern
  339. *
  340. * @param Zend_Controller_Response_Abstract $response
  341. * @param string $header
  342. * @param string $match
  343. * @return bool
  344. */
  345. protected function _notHeaderContains(Zend_Controller_Response_Abstract $response, $header, $match)
  346. {
  347. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  348. return true;
  349. }
  350. $contents = str_replace($header . ': ', '', $fullHeader);
  351. return (strstr($contents, $match) === false);
  352. }
  353. /**
  354. * Positive check for header contents matching regex
  355. *
  356. * @param Zend_Controller_Response_Abstract $response
  357. * @param string $header
  358. * @param string $pattern
  359. * @return bool
  360. */
  361. protected function _headerRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  362. {
  363. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  364. return false;
  365. }
  366. $contents = str_replace($header . ': ', '', $fullHeader);
  367. return preg_match($pattern, $contents);
  368. }
  369. /**
  370. * Negative check for header contents matching regex
  371. *
  372. * @param Zend_Controller_Response_Abstract $response
  373. * @param string $header
  374. * @param string $pattern
  375. * @return bool
  376. */
  377. protected function _notHeaderRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  378. {
  379. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  380. return true;
  381. }
  382. $contents = str_replace($header . ': ', '', $fullHeader);
  383. return !preg_match($pattern, $contents);
  384. }
  385. }