svn: /pear/packages/HTTP_Request2/trunk/ HTTP/Request2/Adapter/Mock.php tests/Request2/Adapter/MockTest.php

From: Date: Sat, 07 Apr 2012 10:05:57 +0000
Subject: svn: /pear/packages/HTTP_Request2/trunk/ HTTP/Request2/Adapter/Mock.php tests/Request2/Adapter/MockTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-64651@lists.php.net to get a copy of this message
avb Sat, 07 Apr 2012 10:05:57 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=324937 Log: URL-specific responses from Mock adapter (request #19276) Bug: http://pear.php.net/bugs/19276 (unknown) Changed paths: U pear/packages/HTTP_Request2/trunk/HTTP/Request2/Adapter/Mock.php U pear/packages/HTTP_Request2/trunk/tests/Request2/Adapter/MockTest.php Modified: pear/packages/HTTP_Request2/trunk/HTTP/Request2/Adapter/Mock.php =================================================================== --- pear/packages/HTTP_Request2/trunk/HTTP/Request2/Adapter/Mock.php 2012-04-07 07:49:03 UTC (rev 324936) +++ pear/packages/HTTP_Request2/trunk/HTTP/Request2/Adapter/Mock.php 2012-04-07 10:05:57 UTC (rev 324937) @@ -81,7 +81,9 @@ /** * Returns the next response from the queue built by addResponse() * - * If the queue is empty it will return default empty response with status 400, + * 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 @@ -91,31 +93,41 @@ */ public function sendRequest(HTTP_Request2 $request) { - if (count($this->responses) > 0) { - $response = array_shift($this->responses); - if ($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); + $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; } + } + if (!$response) { + return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n"); + + } elseif ($response instanceof HTTP_Request2_Response) { + return $response; + } else { - return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n"); + // rethrow the exception + $class = get_class($response); + $message = $response->getMessage(); + $code = $response->getCode(); + throw new $class($message, $code); } } /** * 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 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) + public function addResponse($response, $url = null) { if (is_string($response)) { $response = self::createResponseFromString($response); @@ -126,7 +138,7 @@ ) { throw new HTTP_Request2_Exception('Parameter is not a valid response'); } - $this->responses[] = $response; + $this->responses[] = array($response, $url); } /** Modified: pear/packages/HTTP_Request2/trunk/tests/Request2/Adapter/MockTest.php =================================================================== --- pear/packages/HTTP_Request2/trunk/tests/Request2/Adapter/MockTest.php 2012-04-07 07:49:03 UTC (rev 324936) +++ pear/packages/HTTP_Request2/trunk/tests/Request2/Adapter/MockTest.php 2012-04-07 10:05:57 UTC (rev 324937) @@ -125,6 +125,41 @@ $this->assertEquals(400, $req->send()->getStatus()); } + /** + * Returning URL-specific responses + * @link http://pear.php.net/bugs/bug.php?id=19276 + */ + public function testRequest19276() + { + $mock = new HTTP_Request2_Adapter_Mock(); + $mock->addResponse( + "HTTP/1.1 200 OK\r\n" . + "Content-Type: text/plain; charset=iso-8859-1\r\n" . + "\r\n" . + "This is a response from example.org", + 'http://example.org/' + ); + $mock->addResponse( + "HTTP/1.1 200 OK\r\n" . + "Content-Type: text/plain; charset=iso-8859-1\r\n" . + "\r\n" . + "This is a response from example.com", + 'http://example.com/' + ); + + $req1 = new HTTP_Request2('http://localhost/'); + $req1->setAdapter($mock); + $this->assertEquals(400, $req1->send()->getStatus()); + + $req2 = new HTTP_Request2('http://example.com/'); + $req2->setAdapter($mock); + $this->assertContains('example.com', $req2->send()->getBody()); + + $req3 = new HTTP_Request2('http://example.org'); + $req3->setAdapter($mock); + $this->assertContains('example.org', $req3->send()->getBody()); + } + public function testResponseException() { $mock = new HTTP_Request2_Adapter_Mock();

« previous php.pear.cvs (#64651) next »