| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Mock adapter intended for testing | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * PHP version 5 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-07-10 16:23:21 +02:00
										 |  |  |  * LICENSE | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This source file is subject to BSD 3-Clause License that is bundled | 
					
						
							|  |  |  |  * with this package in the file LICENSE and available at the URL | 
					
						
							|  |  |  |  * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category  HTTP | 
					
						
							|  |  |  |  * @package   HTTP_Request2 | 
					
						
							|  |  |  |  * @author    Alexey Borzov <avb@php.net> | 
					
						
							| 
									
										
										
										
											2017-07-09 22:17:52 +02:00
										 |  |  |  * @copyright 2008-2016 Alexey Borzov <avb@php.net> | 
					
						
							| 
									
										
										
										
											2014-07-10 16:23:21 +02:00
										 |  |  |  * @license   http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License | 
					
						
							|  |  |  |  * @link      http://pear.php.net/package/HTTP_Request2 | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Base class for HTTP_Request2 adapters | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | require_once 'HTTP/Request2/Adapter.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Mock adapter intended for testing | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Can be used to test applications depending on HTTP_Request2 package without | 
					
						
							|  |  |  |  * actually performing any HTTP requests. This adapter will return responses | 
					
						
							|  |  |  |  * previously added via addResponse() | 
					
						
							|  |  |  |  * <code> | 
					
						
							|  |  |  |  * $mock = new HTTP_Request2_Adapter_Mock(); | 
					
						
							|  |  |  |  * $mock->addResponse("HTTP/1.1 ... "); | 
					
						
							| 
									
										
										
										
											2011-06-22 15:56:27 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |  * $request = new HTTP_Request2(); | 
					
						
							|  |  |  |  * $request->setAdapter($mock); | 
					
						
							| 
									
										
										
										
											2011-06-22 15:56:27 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |  * // This will return the response set above
 | 
					
						
							|  |  |  |  * $response = $req->send(); | 
					
						
							| 
									
										
										
										
											2011-06-22 15:56:27 -04:00
										 |  |  |  * </code> | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |  * @category HTTP | 
					
						
							|  |  |  |  * @package  HTTP_Request2 | 
					
						
							|  |  |  |  * @author   Alexey Borzov <avb@php.net> | 
					
						
							| 
									
										
										
										
											2014-07-10 16:23:21 +02:00
										 |  |  |  * @license  http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License | 
					
						
							| 
									
										
										
										
											2017-07-09 22:17:52 +02:00
										 |  |  |  * @version  Release: 2.3.0 | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |  * @link     http://pear.php.net/package/HTTP_Request2 | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * A queue of responses to be returned by sendRequest() | 
					
						
							|  |  |  |      * @var  array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |     protected $responses = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns the next response from the queue built by addResponse() | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Only responses without explicit URLs or with URLs equal to request URL | 
					
						
							|  |  |  |      * will be considered. If matching response is not found or the queue is | 
					
						
							|  |  |  |      * empty then default empty response with status 400 will be returned, | 
					
						
							|  |  |  |      * if an Exception object was added to the queue it will be thrown. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param HTTP_Request2 $request HTTP request message | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return   HTTP_Request2_Response | 
					
						
							|  |  |  |      * @throws   Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |     public function sendRequest(HTTP_Request2 $request) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |         $requestUrl = (string)$request->getUrl(); | 
					
						
							|  |  |  |         $response   = null; | 
					
						
							|  |  |  |         foreach ($this->responses as $k => $v) { | 
					
						
							|  |  |  |             if (!$v[1] || $requestUrl == $v[1]) { | 
					
						
							|  |  |  |                 $response = $v[0]; | 
					
						
							|  |  |  |                 array_splice($this->responses, $k, 1); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (!$response) { | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |             return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n"); | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } elseif ($response instanceof HTTP_Request2_Response) { | 
					
						
							|  |  |  |             return $response; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // rethrow the exception
 | 
					
						
							|  |  |  |             $class   = get_class($response); | 
					
						
							|  |  |  |             $message = $response->getMessage(); | 
					
						
							|  |  |  |             $code    = $response->getCode(); | 
					
						
							|  |  |  |             throw new $class($message, $code); | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Adds response to the queue | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param mixed  $response either a string, a pointer to an open file, | 
					
						
							|  |  |  |      *                         an instance of HTTP_Request2_Response or Exception | 
					
						
							|  |  |  |      * @param string $url      A request URL this response should be valid for | 
					
						
							|  |  |  |      *                         (see {@link http://pear.php.net/bugs/bug.php?id=19276}) | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws   HTTP_Request2_Exception | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function addResponse($response, $url = null) | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_string($response)) { | 
					
						
							|  |  |  |             $response = self::createResponseFromString($response); | 
					
						
							|  |  |  |         } elseif (is_resource($response)) { | 
					
						
							|  |  |  |             $response = self::createResponseFromFile($response); | 
					
						
							|  |  |  |         } elseif (!$response instanceof HTTP_Request2_Response && | 
					
						
							|  |  |  |                   !$response instanceof Exception | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             throw new HTTP_Request2_Exception('Parameter is not a valid response'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |         $this->responses[] = array($response, $url); | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates a new HTTP_Request2_Response object from a string | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $str string containing HTTP response message | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return   HTTP_Request2_Response | 
					
						
							|  |  |  |      * @throws   HTTP_Request2_Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |     public static function createResponseFromString($str) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $parts       = preg_split('!(\r?\n){2}!m', $str, 2); | 
					
						
							| 
									
										
										
										
											2011-06-22 15:56:27 -04:00
										 |  |  |         $headerLines = explode("\n", $parts[0]); | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |         $response    = new HTTP_Request2_Response(array_shift($headerLines)); | 
					
						
							|  |  |  |         foreach ($headerLines as $headerLine) { | 
					
						
							|  |  |  |             $response->parseHeaderLine($headerLine); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $response->parseHeaderLine(''); | 
					
						
							|  |  |  |         if (isset($parts[1])) { | 
					
						
							|  |  |  |             $response->appendBody($parts[1]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $response; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-05 14:29:02 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Creates a new HTTP_Request2_Response object from a file | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param resource $fp file pointer returned by fopen() | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return   HTTP_Request2_Response | 
					
						
							|  |  |  |      * @throws   HTTP_Request2_Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-28 15:29:20 -04:00
										 |  |  |     public static function createResponseFromFile($fp) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $response = new HTTP_Request2_Response(fgets($fp)); | 
					
						
							|  |  |  |         do { | 
					
						
							|  |  |  |             $headerLine = fgets($fp); | 
					
						
							|  |  |  |             $response->parseHeaderLine($headerLine); | 
					
						
							|  |  |  |         } while ('' != trim($headerLine)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (!feof($fp)) { | 
					
						
							|  |  |  |             $response->appendBody(fread($fp, 8192)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $response; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ?>
 |