Skip to content

Commit

Permalink
Merge branch 'main' into gitauto/issue-#82-51f588c0-a7e2-45bc-b113-f1…
Browse files Browse the repository at this point in the history
…93f86e4d7f
  • Loading branch information
gstraccini[bot] authored Jan 16, 2025
2 parents 09d03b4 + 1f2734b commit 494beed
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GuiBranco\Pancake;

use GuiBranco\Pancake\RequestException;
use JsonException;

class Response
{
Expand Down Expand Up @@ -71,6 +72,46 @@ public function getBody(): ?string
return $this->body;
}

/**
* Get the HTTP request body as a decoded JSON object.
*
* @return object|null Returns an object if the body is valid JSON, or null otherwise.
* @throws JsonException If JSON decoding fails.
*/
public function getBodyAsJson(): ?object
{
if ($this->body === null) {
return null;
}

try {
$decoded = json_decode($this->body, false, 512, JSON_THROW_ON_ERROR);
return $decoded;
} catch (JsonException $e) {
throw new JsonException('Invalid JSON string: ' . $e->getMessage(), 0, $e);
}
}

/**
* Get the HTTP request body as a decoded JSON array.
*
* @return array|null Returns an array if the body is valid JSON, or null otherwise.
* @throws JsonException If JSON decoding fails.
*/
public function getBodyAsArray(): ?array
{
if ($this->body === null) {
return null;
}

try {
$decoded = json_decode($this->body, true, 512, JSON_THROW_ON_ERROR);
return $decoded;
} catch (JsonException $e) {
throw new JsonException('Invalid JSON string: ' . $e->getMessage(), 0, $e);
}
}

/**
* Retrieves the message.
*
Expand Down
51 changes: 51 additions & 0 deletions tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,55 @@ public function testToJson()

$this->assertJsonStringEqualsJsonString($expectedJson, $response->toJson());
}

public function testGetBodyAsJson()
{
$response = Response::success(json_encode(['key' => 'value']), "http://example.com", ["Content-Type" => "application/json"], 200);

$this->assertEquals((object) ['key' => 'value'], $response->getBodyAsJson());
}

public function testGetBodyAsJsonReturnsNull()
{
$response = Response::error("Error occurred", "http://example.com", 400);

$this->assertEquals(null, $response->getBodyAsJson());
}

public function testGetBodyAsJsonThrowsException()
{
$response = Response::success("Invalid JSON", "http://example.com", ["Content-Type" => "application/json"], 200);

$this->expectException(JsonException::class);
$response->getBodyAsJson();
}

public function testGetBodyAsJsonWithEmptyObject()
{
$response = Response::success('{}', "http://example.com", ["Content-Type" => "application/json"], 200);

$this->assertEquals(new \stdClass(), $response->getBodyAsJson());
}

public function testGetBodyAsArray()
{
$response = Response::success(json_encode(['key' => 'value']), "http://example.com", ["Content-Type" => "application/json"], 200);

$this->assertEquals(['key' => 'value'], $response->getBodyAsArray());
}

public function testGetBodyAsArrayReturnsNull()
{
$response = Response::error("Error occurred", "http://example.com", 400);

$this->assertEquals(null, $response->getBodyAsArray());
}

public function testGetBodyAsArrayThrowsException()
{
$response = Response::success("Invalid JSON", "http://example.com", ["Content-Type" => "application/json"], 200);

$this->expectException(JsonException::class);
$response->getBodyAsArray();
}
}

0 comments on commit 494beed

Please sign in to comment.