Mock.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Mock adapter intended for testing
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to BSD 3-Clause License that is bundled
  10. * with this package in the file LICENSE and available at the URL
  11. * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE
  12. *
  13. * @category HTTP
  14. * @package HTTP_Request2
  15. * @author Alexey Borzov <avb@php.net>
  16. * @copyright 2008-2014 Alexey Borzov <avb@php.net>
  17. * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
  18. * @link http://pear.php.net/package/HTTP_Request2
  19. */
  20. /**
  21. * Base class for HTTP_Request2 adapters
  22. */
  23. require_once 'HTTP/Request2/Adapter.php';
  24. /**
  25. * Mock adapter intended for testing
  26. *
  27. * Can be used to test applications depending on HTTP_Request2 package without
  28. * actually performing any HTTP requests. This adapter will return responses
  29. * previously added via addResponse()
  30. * <code>
  31. * $mock = new HTTP_Request2_Adapter_Mock();
  32. * $mock->addResponse("HTTP/1.1 ... ");
  33. *
  34. * $request = new HTTP_Request2();
  35. * $request->setAdapter($mock);
  36. *
  37. * // This will return the response set above
  38. * $response = $req->send();
  39. * </code>
  40. *
  41. * @category HTTP
  42. * @package HTTP_Request2
  43. * @author Alexey Borzov <avb@php.net>
  44. * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
  45. * @version Release: 2.2.1
  46. * @link http://pear.php.net/package/HTTP_Request2
  47. */
  48. class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
  49. {
  50. /**
  51. * A queue of responses to be returned by sendRequest()
  52. * @var array
  53. */
  54. protected $responses = array();
  55. /**
  56. * Returns the next response from the queue built by addResponse()
  57. *
  58. * Only responses without explicit URLs or with URLs equal to request URL
  59. * will be considered. If matching response is not found or the queue is
  60. * empty then default empty response with status 400 will be returned,
  61. * if an Exception object was added to the queue it will be thrown.
  62. *
  63. * @param HTTP_Request2 $request HTTP request message
  64. *
  65. * @return HTTP_Request2_Response
  66. * @throws Exception
  67. */
  68. public function sendRequest(HTTP_Request2 $request)
  69. {
  70. $requestUrl = (string)$request->getUrl();
  71. $response = null;
  72. foreach ($this->responses as $k => $v) {
  73. if (!$v[1] || $requestUrl == $v[1]) {
  74. $response = $v[0];
  75. array_splice($this->responses, $k, 1);
  76. break;
  77. }
  78. }
  79. if (!$response) {
  80. return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
  81. } elseif ($response instanceof HTTP_Request2_Response) {
  82. return $response;
  83. } else {
  84. // rethrow the exception
  85. $class = get_class($response);
  86. $message = $response->getMessage();
  87. $code = $response->getCode();
  88. throw new $class($message, $code);
  89. }
  90. }
  91. /**
  92. * Adds response to the queue
  93. *
  94. * @param mixed $response either a string, a pointer to an open file,
  95. * an instance of HTTP_Request2_Response or Exception
  96. * @param string $url A request URL this response should be valid for
  97. * (see {@link http://pear.php.net/bugs/bug.php?id=19276})
  98. *
  99. * @throws HTTP_Request2_Exception
  100. */
  101. public function addResponse($response, $url = null)
  102. {
  103. if (is_string($response)) {
  104. $response = self::createResponseFromString($response);
  105. } elseif (is_resource($response)) {
  106. $response = self::createResponseFromFile($response);
  107. } elseif (!$response instanceof HTTP_Request2_Response &&
  108. !$response instanceof Exception
  109. ) {
  110. throw new HTTP_Request2_Exception('Parameter is not a valid response');
  111. }
  112. $this->responses[] = array($response, $url);
  113. }
  114. /**
  115. * Creates a new HTTP_Request2_Response object from a string
  116. *
  117. * @param string $str string containing HTTP response message
  118. *
  119. * @return HTTP_Request2_Response
  120. * @throws HTTP_Request2_Exception
  121. */
  122. public static function createResponseFromString($str)
  123. {
  124. $parts = preg_split('!(\r?\n){2}!m', $str, 2);
  125. $headerLines = explode("\n", $parts[0]);
  126. $response = new HTTP_Request2_Response(array_shift($headerLines));
  127. foreach ($headerLines as $headerLine) {
  128. $response->parseHeaderLine($headerLine);
  129. }
  130. $response->parseHeaderLine('');
  131. if (isset($parts[1])) {
  132. $response->appendBody($parts[1]);
  133. }
  134. return $response;
  135. }
  136. /**
  137. * Creates a new HTTP_Request2_Response object from a file
  138. *
  139. * @param resource $fp file pointer returned by fopen()
  140. *
  141. * @return HTTP_Request2_Response
  142. * @throws HTTP_Request2_Exception
  143. */
  144. public static function createResponseFromFile($fp)
  145. {
  146. $response = new HTTP_Request2_Response(fgets($fp));
  147. do {
  148. $headerLine = fgets($fp);
  149. $response->parseHeaderLine($headerLine);
  150. } while ('' != trim($headerLine));
  151. while (!feof($fp)) {
  152. $response->appendBody(fread($fp, 8192));
  153. }
  154. return $response;
  155. }
  156. }
  157. ?>