Permalink
Browse files

URL-specific responses from Mock adapter (request #19276)

git-svn-id: http://svn.php.net/repository/pear/packages/HTTP_Request2/trunk@324937 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent d6ad9ac commit 1a1fa555d6b87ffd4e1736cea919cc2003124ab8 @sad-spirit sad-spirit committed Apr 7, 2012
Showing with 63 additions and 16 deletions.
  1. +28 −16 HTTP/Request2/Adapter/Mock.php
  2. +35 −0 tests/Request2/Adapter/MockTest.php
@@ -81,7 +81,9 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
/**
* 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 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
*/
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;
}
- } else {
+ }
+ if (!$response) {
return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
+
+ } 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);
}
}
/**
* 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 @@ public function addResponse($response)
) {
throw new HTTP_Request2_Exception('Parameter is not a valid response');
}
- $this->responses[] = $response;
+ $this->responses[] = array($response, $url);
}
/**
@@ -125,6 +125,41 @@ public function testResponsesQueue()
$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();

0 comments on commit 1a1fa55

Please sign in to comment.