Skip to content

Commit

Permalink
Show the request body when verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
dshafik committed Oct 24, 2015
1 parent 03b2105 commit e6b3d77
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 154 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit colors="true" verbose="true" bootstrap="tests/bootstrap.php">
<phpunit colors="true" verbose="true" bootstrap="tests/bootstrap.php" printerFile="vendor/whatthejeff/nyancat-phpunit-resultprinter/src/NyanCat/PHPUnit/ResultPrinter.php" printerClass="NyanCat\PHPUnit\ResultPrinter">
<php>
<ini name="display_errors" value="on"/>
</php>
Expand Down
2 changes: 2 additions & 0 deletions src/Handler/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function __invoke(callable $handler)
throw new \Exception("You must call setSigner before trying to sign a request");
}

$request->getBody()->rewind();

$this->signer->setHttpMethod($request->getMethod())
->setHost($request->getUri()->getHost())
->setPath($request->getUri()->getPath())
Expand Down
34 changes: 25 additions & 9 deletions src/Handler/Verbose.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,19 @@ public function __invoke(callable $handler)
$handler,
$colors
) {
fputs($this->outputStream, "{$colors['cyan']}===> [VERBOSE] Request: \n");
fputs($this->outputStream, "{$colors['yellow']}" . $this->getBody($request));
fputs($this->outputStream, "{$colors['reset']}\n");

return $handler($request, $config)->then(
function (\Psr\Http\Message\ResponseInterface $response) use ($colors) {
$statusCode = $response->getStatusCode();
if ($statusCode > 299 && $statusCode < 400) {
fputs($this->outputStream, "{$colors['yellow']}===> [VERBOSE] Redirected: ");
fputs($this->outputStream, $response->getHeader('Location')[0] . "\n");
fputs($this->outputStream, "{$colors['cyan']}===> [VERBOSE] Redirected: ");
fputs($this->outputStream, $response->getHeader('Location')[0]);
fputs($this->outputStream, "{$colors['reset']}\n");
} else {
$body = trim($response->getBody());
$result = json_decode($body);
if ($result !== null) {
$responseBody = json_encode($result, JSON_PRETTY_PRINT);
} else {
$responseBody = (!empty(trim($body))) ? $body : "No response body returned";
}
$responseBody = $this->getBody($response);

if ($statusCode > 399 && $statusCode < 600) {
fputs($this->errorStream, "{$colors['red']}===> [ERROR] An error occurred: \n");
Expand Down Expand Up @@ -156,4 +154,22 @@ function (\Exception $reason) use ($colors) {
);
};
}

protected function getBody(\Psr\Http\Message\MessageInterface $message)
{
$body = trim($message->getBody());

if ($message->getBody()->getSize() == 0 || empty($body)) {
if ($message instanceof \Psr\Http\Message\ResponseInterface) {
return "No response body returned";
}
return "No request body sent";
}
$result = json_decode($body);
if ($result !== null) {
return json_encode($result, JSON_PRETTY_PRINT);
}

return $body;
}
}
Loading

0 comments on commit e6b3d77

Please sign in to comment.