ResponseHeader.php 13 KB

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