diff --git a/CHANGELOG.md b/CHANGELOG.md index c7c1815c..c5aeb79e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Ensure that the `delete()` method of the HTTP Client doesn't set a body by default. + ## v1.2.0 - 2024-09-23 ### Added diff --git a/src/mantle/http-client/class-pending-request.php b/src/mantle/http-client/class-pending-request.php index 766c6df5..8a006b36 100644 --- a/src/mantle/http-client/class-pending-request.php +++ b/src/mantle/http-client/class-pending-request.php @@ -562,8 +562,8 @@ public function get( string $url, array|string|null $query = null ): Response { * * @throws InvalidArgumentException If the request is pooled. * - * @param string $url - * @param array|string|null $query + * @param string $url URL to retrieve. + * @param array|string|null $query Query parameters (assumed to be urlencoded). */ public function head( string $url, array|string|null $query = null ): Response { if ( $this->pooled ) { @@ -582,8 +582,8 @@ public function head( string $url, array|string|null $query = null ): Response { * * @throws InvalidArgumentException If the request is pooled. * - * @param string $url - * @param array $data + * @param string $url URL to post. + * @param array|null $data Data to send with the request. */ public function post( string $url, ?array $data = null ): Response { if ( $this->pooled ) { @@ -602,8 +602,8 @@ public function post( string $url, ?array $data = null ): Response { * * @throws InvalidArgumentException If the request is pooled. * - * @param string $url - * @param array $data + * @param string $url URL to patch. + * @param array|null $data Data to send with the request. */ public function patch( string $url, ?array $data = null ): Response { if ( $this->pooled ) { @@ -622,8 +622,8 @@ public function patch( string $url, ?array $data = null ): Response { * * @throws InvalidArgumentException If the request is pooled. * - * @param string $url - * @param array $data + * @param string $url URL to put. + * @param array|null $data Data to send with the request. */ public function put( string $url, ?array $data = null ): Response { if ( $this->pooled ) { @@ -642,10 +642,10 @@ public function put( string $url, ?array $data = null ): Response { * * @throws InvalidArgumentException If the request is pooled. * - * @param string $url - * @param array $data + * @param string $url URL to delete. + * @param array|null $data Data to send with the request. */ - public function delete( string $url, ?array $data = [] ): Response { + public function delete( string $url, ?array $data = null ): Response { if ( $this->pooled ) { throw new InvalidArgumentException( 'Cannot call delete() on a pooled request.' ); } diff --git a/src/mantle/http-client/class-request.php b/src/mantle/http-client/class-request.php index f9536288..dca4fd4b 100644 --- a/src/mantle/http-client/class-request.php +++ b/src/mantle/http-client/class-request.php @@ -98,8 +98,8 @@ public function header( string $header ) { /** * Retrieve the body of the request. */ - public function body(): string { - return $this->args['body'] ?? ''; + public function body(): ?string { + return $this->args['body'] ?? null; } /** @@ -108,7 +108,7 @@ public function body(): string { * @return array|null */ public function json() { - return json_decode( $this->body(), true ); + return json_decode( (string) $this->body(), true ); } /** diff --git a/tests/HttpClient/HttpClientTest.php b/tests/HttpClient/HttpClientTest.php index 23170622..1d400241 100644 --- a/tests/HttpClient/HttpClientTest.php +++ b/tests/HttpClient/HttpClientTest.php @@ -73,6 +73,19 @@ public function test_make_get_request_with_query() { ); } + public function test_delete_request() { + $this->fake_request(); + + $this->http_factory->delete( 'https://example.com/delete/' ); + + $this->assertRequestSent( 'https://example.com/delete/' ); + $this->assertRequestSent( + fn ( Request $request ) => 'https://example.com/delete/' === $request->url() + && 'DELETE' === $request->method() + && is_null( $request->body() ) + ); + } + public function test_make_request_enum() { $this->fake_request();