From c8caa7167007da3e50478bb139b13f60238b89b2 Mon Sep 17 00:00:00 2001 From: Jayan Ratna Date: Sun, 29 Dec 2024 13:06:25 +1300 Subject: [PATCH] fix: empty body requests not converted to objects --- src/ValueObjects/Transporter/Payload.php | 2 +- tests/Pest.php | 7 +++++-- tests/Service/Domain.php | 12 +++++++++--- tests/ValueObjects/Transporter/Payload.php | 14 +++++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/ValueObjects/Transporter/Payload.php b/src/ValueObjects/Transporter/Payload.php index db663d3..f5e8441 100644 --- a/src/ValueObjects/Transporter/Payload.php +++ b/src/ValueObjects/Transporter/Payload.php @@ -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); diff --git a/tests/Pest.php b/tests/Pest.php index ff93925..1f23f4e 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -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 @@ -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; } diff --git a/tests/Service/Domain.php b/tests/Service/Domain.php index 731e099..00f06de 100644 --- a/tests/Service/Domain.php +++ b/tests/Service/Domain.php @@ -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, @@ -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'); diff --git a/tests/ValueObjects/Transporter/Payload.php b/tests/ValueObjects/Transporter/Payload.php index b4ff8e9..11b5bd4 100644 --- a/tests/ValueObjects/Transporter/Payload.php +++ b/tests/ValueObjects/Transporter/Payload.php @@ -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'); +});