Skip to content

Commit

Permalink
Merge pull request #65 from resend/fix-empty-body-requests
Browse files Browse the repository at this point in the history
fix: empty body requests not converted to objects
  • Loading branch information
jayanratna authored Jan 1, 2025
2 parents 4714dc6 + c8caa71 commit 50a554d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function toRequest(BaseUri $baseUri, Headers $headers): RequestInterface
->withContentType($this->contentType);

if ($this->method === Method::POST || $this->method === Method::PATCH || $this->method === Method::PUT) {
$body = json_encode($this->parameters, JSON_THROW_ON_ERROR);
$body = json_encode((object) $this->parameters, JSON_THROW_ON_ERROR);
}

return new Request($this->method->value, $uri, $headers->toArray(), $body);
Expand Down
7 changes: 5 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

function mockClient(string $method, string $resource, array $parameters, array|string $response, $methodName = 'request')
{
/** @var \Mockery\MockInterface|\Resend\Contracts\Transporter $transporter */
/** @var Mockery\MockInterface|Transporter $transporter */
$transporter = Mockery::mock(Transporter::class);

$transporter
Expand All @@ -21,7 +21,10 @@ function mockClient(string $method, string $resource, array $parameters, array|s

$request = $payload->toRequest($baseUri, $headers);

if ($method === 'POST' && (string) $request->getBody() !== json_encode($parameters)) {
if (
($method === 'POST' || $method === 'PATCH' || $method === 'PUT')
&& (string) $request->getBody() !== json_encode((object) $parameters)
) {
return false;
}

Expand Down
12 changes: 9 additions & 3 deletions tests/Service/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
});

it('can update a domain resource', function () {
$client = mockClient('PATCH', 'domains/4dd369bc-aa82-4ff3-97de-514ae3000ee0', [], domain());
$client = mockClient('PATCH', 'domains/4dd369bc-aa82-4ff3-97de-514ae3000ee0', [
'open_tracking' => false,
'click_tracking' => true,
], domain());

$result = $client->domains->update('4dd369bc-aa82-4ff3-97de-514ae3000ee0', [
'open_tracking' => false,
Expand All @@ -54,9 +57,12 @@
});

it('can verify a domain resource', function () {
$client = mockClient('POST', 'domains/re_1234567/verify', [], domain());
$client = mockClient('POST', 'domains/4dd369bc-aa82-4ff3-97de-514ae3000ee0/verify', [], [
'object' => 'domain',
'id' => '4dd369bc-aa82-4ff3-97de-514ae3000ee0',
]);

$result = $client->domains->verify('re_1234567');
$result = $client->domains->verify('4dd369bc-aa82-4ff3-97de-514ae3000ee0');

expect($result)->toBeInstanceOf(Domain::class)
->id->toBe('4dd369bc-aa82-4ff3-97de-514ae3000ee0');
Expand Down
14 changes: 13 additions & 1 deletion tests/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@

$request = $payload->toRequest($baseUri, $headers);

expect($request->getBody()->getContents())->toBe('[]')
expect($request->getBody()->getContents())->toBe('{}')
->and($request->getUri()->getPath())->toBe('/domains/re_123456/verify');
});

it('can convert an empty array body to a JSON object', function () {
$payload = Payload::create('domains', []);

$baseUri = BaseUri::from('api.resend.com');
$headers = Headers::withAuthorization(ApiKey::from('foo'))->withContentType(ContentType::JSON);

$request = $payload->toRequest($baseUri, $headers);

expect($request->getBody()->getContents())->toBe('{}')
->and($request->getUri()->getPath())->toBe('/domains');
});

0 comments on commit 50a554d

Please sign in to comment.