Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure delete() method of http client does not have body #594

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/mantle/http-client/class-pending-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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.' );
}
Expand Down
6 changes: 3 additions & 3 deletions src/mantle/http-client/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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 );
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down