PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Http/Client/Adapter/Test.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 248 lines | 85 code | 26 blank | 137 comment | 10 complexity | 35e3087f1d6baa3b6fa2cda8228febf9 MD5 | raw file
  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_Http
  17. * @subpackage Client_Adapter
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * @see Zend_Uri_Http
  24. */
  25. #require_once 'Zend/Uri/Http.php';
  26. /**
  27. * @see Zend_Http_Response
  28. */
  29. #require_once 'Zend/Http/Response.php';
  30. /**
  31. * @see Zend_Http_Client_Adapter_Interface
  32. */
  33. #require_once 'Zend/Http/Client/Adapter/Interface.php';
  34. /**
  35. * A testing-purposes adapter.
  36. *
  37. * Should be used to test all components that rely on Zend_Http_Client,
  38. * without actually performing an HTTP request. You should instantiate this
  39. * object manually, and then set it as the client's adapter. Then, you can
  40. * set the expected response using the setResponse() method.
  41. *
  42. * @category Zend
  43. * @package Zend_Http
  44. * @subpackage Client_Adapter
  45. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. */
  48. class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface
  49. {
  50. /**
  51. * Parameters array
  52. *
  53. * @var array
  54. */
  55. protected $config = array();
  56. /**
  57. * Buffer of responses to be returned by the read() method. Can be
  58. * set using setResponse() and addResponse().
  59. *
  60. * @var array
  61. */
  62. protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
  63. /**
  64. * Current position in the response buffer
  65. *
  66. * @var integer
  67. */
  68. protected $responseIndex = 0;
  69. /**
  70. * Wether or not the next request will fail with an exception
  71. *
  72. * @var boolean
  73. */
  74. protected $_nextRequestWillFail = false;
  75. /**
  76. * Adapter constructor, currently empty. Config is set using setConfig()
  77. *
  78. */
  79. public function __construct()
  80. { }
  81. /**
  82. * Set the nextRequestWillFail flag
  83. *
  84. * @param boolean $flag
  85. * @return Zend_Http_Client_Adapter_Test
  86. */
  87. public function setNextRequestWillFail($flag)
  88. {
  89. $this->_nextRequestWillFail = (bool) $flag;
  90. return $this;
  91. }
  92. /**
  93. * Set the configuration array for the adapter
  94. *
  95. * @param Zend_Config | array $config
  96. */
  97. public function setConfig($config = array())
  98. {
  99. if ($config instanceof Zend_Config) {
  100. $config = $config->toArray();
  101. } elseif (! is_array($config)) {
  102. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  103. throw new Zend_Http_Client_Adapter_Exception(
  104. 'Array or Zend_Config object expected, got ' . gettype($config)
  105. );
  106. }
  107. foreach ($config as $k => $v) {
  108. $this->config[strtolower($k)] = $v;
  109. }
  110. }
  111. /**
  112. * Connect to the remote server
  113. *
  114. * @param string $host
  115. * @param int $port
  116. * @param boolean $secure
  117. * @param int $timeout
  118. * @throws Zend_Http_Client_Adapter_Exception
  119. */
  120. public function connect($host, $port = 80, $secure = false)
  121. {
  122. if ($this->_nextRequestWillFail) {
  123. $this->_nextRequestWillFail = false;
  124. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  125. throw new Zend_Http_Client_Adapter_Exception('Request failed');
  126. }
  127. }
  128. /**
  129. * Send request to the remote server
  130. *
  131. * @param string $method
  132. * @param Zend_Uri_Http $uri
  133. * @param string $http_ver
  134. * @param array $headers
  135. * @param string $body
  136. * @return string Request as string
  137. */
  138. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  139. {
  140. $host = $uri->getHost();
  141. $host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
  142. // Build request headers
  143. $path = $uri->getPath();
  144. if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
  145. $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
  146. foreach ($headers as $k => $v) {
  147. if (is_string($k)) $v = ucfirst($k) . ": $v";
  148. $request .= "$v\r\n";
  149. }
  150. // Add the request body
  151. $request .= "\r\n" . $body;
  152. // Do nothing - just return the request as string
  153. return $request;
  154. }
  155. /**
  156. * Return the response set in $this->setResponse()
  157. *
  158. * @return string
  159. */
  160. public function read()
  161. {
  162. if ($this->responseIndex >= count($this->responses)) {
  163. $this->responseIndex = 0;
  164. }
  165. return $this->responses[$this->responseIndex++];
  166. }
  167. /**
  168. * Close the connection (dummy)
  169. *
  170. */
  171. public function close()
  172. { }
  173. /**
  174. * Set the HTTP response(s) to be returned by this adapter
  175. *
  176. * @param Zend_Http_Response|array|string $response
  177. */
  178. public function setResponse($response)
  179. {
  180. if ($response instanceof Zend_Http_Response) {
  181. $response = $response->asString("\r\n");
  182. }
  183. $this->responses = (array)$response;
  184. $this->responseIndex = 0;
  185. }
  186. /**
  187. * Add another response to the response buffer.
  188. *
  189. * @param string Zend_Http_Response|$response
  190. */
  191. public function addResponse($response)
  192. {
  193. if ($response instanceof Zend_Http_Response) {
  194. $response = $response->asString("\r\n");
  195. }
  196. $this->responses[] = $response;
  197. }
  198. /**
  199. * Sets the position of the response buffer. Selects which
  200. * response will be returned on the next call to read().
  201. *
  202. * @param integer $index
  203. */
  204. public function setResponseIndex($index)
  205. {
  206. if ($index < 0 || $index >= count($this->responses)) {
  207. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  208. throw new Zend_Http_Client_Adapter_Exception(
  209. 'Index out of range of response buffer size');
  210. }
  211. $this->responseIndex = $index;
  212. }
  213. /**
  214. * Retrieve the array of all configuration options
  215. *
  216. * @return array
  217. */
  218. public function getConfig()
  219. {
  220. return $this->config;
  221. }
  222. }