Skip to content

Commit

Permalink
Merge pull request #2 from snowio/fix/empty-guzzle-response
Browse files Browse the repository at this point in the history
Fix Empty Guzzle Response
  • Loading branch information
Alexander Wanyoike authored Jun 23, 2021
2 parents 99cc272 + e4da136 commit 5a6d659
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"php": ">=5.5.0",
"snowio/php-async-soap": "~1.0",
"snowio/soap-http-binding": "~0.2.0",
"guzzlehttp/guzzle": "^6.1"
"guzzlehttp/guzzle": "^6.1",
"psr/log": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
Expand Down
26 changes: 21 additions & 5 deletions src/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;

class SoapClient implements SoapClientInterface
{
Expand Down Expand Up @@ -42,10 +43,12 @@ function () use ($name, $arguments, $options, $inputHeaders, &$outputHeaders) {

try {
$response = (yield $this->client->sendAsync($request, $requestOptions));
$this->tryLog($response, $options);
yield $this->interpretResponse($httpBinding, $response, $name, $outputHeaders);
} catch (RequestException $exception) {
if ($exception->hasResponse()) {
$response = $exception->getResponse();
$this->tryLog($response, $options);
yield $this->interpretResponse($httpBinding, $response, $name, $outputHeaders);
} else {
throw $exception;
Expand All @@ -57,12 +60,25 @@ function () use ($name, $arguments, $options, $inputHeaders, &$outputHeaders) {
);
}

private function interpretResponse(HttpBinding $httpBinding, ResponseInterface $response, $name, &$outputHeaders)
private function tryLog(ResponseInterface $response, array $options = null)
{
try {
return $httpBinding->response($response, $name, $outputHeaders);
} finally {
$response->getBody()->close();
if ($options && isset($options['logger']) && $options['logger'] instanceof LoggerInterface) {
$responseContents = $response->getBody()->__toString();

$options['logger']->debug("Raw SOAP Response Received", [
'response' => $responseContents
]);

if (empty($responseContents)) {
$options['logger']->warning("Empty response has been detected", [
'response' => $responseContents
]);
}
}
}

private function interpretResponse(HttpBinding $httpBinding, ResponseInterface $response, $name, &$outputHeaders)
{
return $httpBinding->response($response, $name, $outputHeaders);
}
}
17 changes: 13 additions & 4 deletions tests/unit/SoapClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use Meng\Soap\HttpBinding\HttpBinding;
use Meng\Soap\HttpBinding\RequestException;
use Psr\Http\Message\ResponseInterface;

class SoapClientTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -231,10 +232,14 @@ public function resultsAreEquivalent()
'someSoapMethod', [['some-key' => 'some-value']]
);

$response = new Response('200', [], 'body');
$this->httpBindingMock->method('response')->willReturn(
'SoapResult'
);
$response = new Response('200', [], $body = 'body');
$this->httpBindingMock->method('response')->willReturnCallback(function (
ResponseInterface $response,
$name,
&$outputHeaders
) {
return $response->getBody()->__toString();
});

$this->handlerMock->append($response);
$this->handlerMock->append($response);
Expand All @@ -246,5 +251,9 @@ public function resultsAreEquivalent()
$asyncResult = $client->callAsync('someSoapMethod', [['some-key' => 'some-value']])->wait();
$this->assertEquals($magicResult, $asyncResult);
$this->assertEquals($syncResult, $asyncResult);

$this->assertEquals($body, $magicResult);
$this->assertEquals($body, $asyncResult);
$this->assertEquals($body, $syncResult);
}
}

0 comments on commit 5a6d659

Please sign in to comment.