diff --git a/CHANGELOG.md b/CHANGELOG.md index c2f4f279e8..c2543875ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ All notable changes to this project will be documented in this file based on the ### Added +* Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients +[#1529](https://github.com/ruflin/Elastica/pull/1529) +* [Backported] Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients +[#1592](https://github.com/ruflin/Elastica/pull/1592) + ### Improvements - [Backported] Reduced memory footprint of response by not keeping the raw JSON data when JSON after JSON has been parsed. [#1588](https://github.com/ruflin/Elastica/pull/1588) diff --git a/lib/Elastica/Transport/Guzzle.php b/lib/Elastica/Transport/Guzzle.php index ae22d7115e..bee603e25a 100644 --- a/lib/Elastica/Transport/Guzzle.php +++ b/lib/Elastica/Transport/Guzzle.php @@ -83,7 +83,8 @@ public function exec(Request $request, array $params) throw new GuzzleException($ex, $request, new Response($ex->getMessage())); } - $response = new Response((string) $res->getBody(), $res->getStatusCode()); + $responseBody = (string) $res->getBody(); + $response = new Response($responseBody, $res->getStatusCode()); $response->setQueryTime($end - $start); if ($connection->hasConfig('bigintConversion')) { $response->setJsonBigintConversion($connection->getConfig('bigintConversion')); @@ -93,6 +94,7 @@ public function exec(Request $request, array $params) [ 'request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode(), + 'body' => $responseBody, ] ); diff --git a/lib/Elastica/Transport/NullTransport.php b/lib/Elastica/Transport/NullTransport.php index d94be7ed5d..932b5b505f 100644 --- a/lib/Elastica/Transport/NullTransport.php +++ b/lib/Elastica/Transport/NullTransport.php @@ -12,18 +12,50 @@ * host but still returns a valid response object * * @author James Boehmer + * @author Jan Domanski */ class NullTransport extends AbstractTransport { /** - * Null transport. + * Response you want to get from the transport + * + * @var Response Response + */ + protected $_response = null; + + /** + * Set response object the transport returns + * + * @param \Elastica\Response $response + * + * @return $this + */ + public function getResponse() + { + return $this->_response; + } + + /** + * Set response object the transport returns + * + * @param \Elastica\Response $response + * + * @return $this + */ + public function setResponse(Response $response) + { + $this->_response = $response; + return $this->_response; + } + + /** + * Generate an example response object * - * @param \Elastica\Request $request * @param array $params Hostname, port, path, ... * - * @return \Elastica\Response Response empty object + * @return \Elastica\Response $response */ - public function exec(Request $request, array $params) + public function generateDefaultResponse(array $params) { $response = [ 'took' => 0, @@ -40,7 +72,25 @@ public function exec(Request $request, array $params) ], 'params' => $params, ]; - return new Response(JSON::stringify($response)); } + + /** + * Null transport. + * + * @param \Elastica\Request $request + * @param array $params Hostname, port, path, ... + * + * @return \Elastica\Response Response empty object + */ + public function exec(Request $request, array $params) + { + $response = $this->getResponse(); + + if (!$response) { + $response = $this->generateDefaultResponse($params); + } + + return $response; + } } diff --git a/test/Elastica/Transport/NullTransportTest.php b/test/Elastica/Transport/NullTransportTest.php index a62e55bd19..0a212c38ef 100644 --- a/test/Elastica/Transport/NullTransportTest.php +++ b/test/Elastica/Transport/NullTransportTest.php @@ -12,9 +12,20 @@ * Elastica Null Transport Test. * * @author James Boehmer + * @author Jan Domanski */ class NullTransportTest extends BaseTest { + + /** @var NullTransport NullTransport */ + protected $transport; + + public function setUp() + { + parent::setUp(); + $this->transport = new NullTransport(); + } + /** * @group functional */ @@ -96,4 +107,29 @@ public function testOldObject() $data = $response->getData(); $this->assertEquals($params, $data['params']); } + + /** + * @group unit + */ + public function testResponse() + { + $resposeString = ''; + $response = new Response($resposeString); + $this->transport->setResponse($response); + $this->assertEquals($response, $this->transport->getResponse()); + } + + /** + * @group unit + */ + public function testGenerateDefaultResponse() + { + $params = [ 'blah' => 123 ]; + $response = $this->transport->generateDefaultResponse($params); + $this->assertEquals([], $response->getTransferInfo()); + + $responseData = $response->getData(); + $this->assertContains('params', $responseData); + $this->assertEquals($params, $responseData['params']); + } }