Skip to content

Commit

Permalink
URL-specific responses from Mock adapter (request #19276)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/HTTP_Request2/trunk@324937 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
sad-spirit committed Apr 7, 2012
1 parent d6ad9ac commit 1a1fa55
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
44 changes: 28 additions & 16 deletions HTTP/Request2/Adapter/Mock.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
/** /**
* Returns the next response from the queue built by addResponse() * 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. * if an Exception object was added to the queue it will be thrown.
* *
* @param HTTP_Request2 $request HTTP request message * @param HTTP_Request2 $request HTTP request message
Expand All @@ -91,31 +93,41 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
*/ */
public function sendRequest(HTTP_Request2 $request) public function sendRequest(HTTP_Request2 $request)
{ {
if (count($this->responses) > 0) { $requestUrl = (string)$request->getUrl();
$response = array_shift($this->responses); $response = null;
if ($response instanceof HTTP_Request2_Response) { foreach ($this->responses as $k => $v) {
return $response; if (!$v[1] || $requestUrl == $v[1]) {
} else { $response = $v[0];
// rethrow the exception array_splice($this->responses, $k, 1);
$class = get_class($response); break;
$message = $response->getMessage();
$code = $response->getCode();
throw new $class($message, $code);
} }
} else { }
if (!$response) {
return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n"); 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 * Adds response to the queue
* *
* @param mixed $response either a string, a pointer to an open file, * @param mixed $response either a string, a pointer to an open file,
* an instance of HTTP_Request2_Response or Exception * 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 * @throws HTTP_Request2_Exception
*/ */
public function addResponse($response) public function addResponse($response, $url = null)
{ {
if (is_string($response)) { if (is_string($response)) {
$response = self::createResponseFromString($response); $response = self::createResponseFromString($response);
Expand All @@ -126,7 +138,7 @@ public function addResponse($response)
) { ) {
throw new HTTP_Request2_Exception('Parameter is not a valid response'); throw new HTTP_Request2_Exception('Parameter is not a valid response');
} }
$this->responses[] = $response; $this->responses[] = array($response, $url);
} }


/** /**
Expand Down
35 changes: 35 additions & 0 deletions tests/Request2/Adapter/MockTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -125,6 +125,41 @@ public function testResponsesQueue()
$this->assertEquals(400, $req->send()->getStatus()); $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() public function testResponseException()
{ {
$mock = new HTTP_Request2_Adapter_Mock(); $mock = new HTTP_Request2_Adapter_Mock();
Expand Down

0 comments on commit 1a1fa55

Please sign in to comment.