From c35267906f9afd6f25ffbaadf423d30767a53618 Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Wed, 26 Jul 2023 20:14:23 +0200 Subject: [PATCH] Fix http-client content_type method (#409) Using content_type() multiples times (as with as_form(), since the object is created with default as_json()) results in a header array value which trigger an "array to string conversion" error in Requests --- src/mantle/http-client/class-pending-request.php | 2 +- tests/http-client/test-http-client.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/mantle/http-client/class-pending-request.php b/src/mantle/http-client/class-pending-request.php index b75155846..4251c7b00 100644 --- a/src/mantle/http-client/class-pending-request.php +++ b/src/mantle/http-client/class-pending-request.php @@ -228,7 +228,7 @@ public function body_format( string $format ) { * @return static */ public function content_type( string $content_type ) { - return $this->with_headers( [ 'Content-Type' => $content_type ] ); + return $this->with_header( 'Content-Type', $content_type, true ); } /** diff --git a/tests/http-client/test-http-client.php b/tests/http-client/test-http-client.php index 2a6280ca0..094079e4c 100644 --- a/tests/http-client/test-http-client.php +++ b/tests/http-client/test-http-client.php @@ -439,4 +439,11 @@ public function test_conditionable_unless_request() { $this->assertEquals( 'Bar', $request->header( 'X-Foo' ) ); } + + public function test_headers_as_form() { + $request = $this->http_factory + ->as_form(); + + $this->assertEquals( 'application/x-www-form-urlencoded', $request->header( 'Content-Type' ) ); + } }