From d16ce47411277bb6e316ce6af2ebd2052a1d3085 Mon Sep 17 00:00:00 2001 From: Jacques ROUSSEL Date: Tue, 3 Oct 2023 08:02:05 +0200 Subject: [PATCH] Fix Content-Length for POST when data empty Signed-off-by: Jacques ROUSSEL --- src/Client.php | 4 ++++ test/ClientTest.php | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Client.php b/src/Client.php index 2d144102..7b72f6a2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1255,6 +1255,10 @@ protected function prepareHeaders($body, $uri) } else { $headers['Content-Length'] = strlen($body); } + } else { + if ($this->getMethod() === 'POST' || $this->getMethod() === 'PUT') { + $headers['Content-Length'] = 0; + } } // Merge the headers of the request (if any) diff --git a/test/ClientTest.php b/test/ClientTest.php index 21dd8770..52dbc4f8 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -505,6 +505,29 @@ public function testClientRequestMethod() $this->assertSame(Client::ENC_URLENCODED, $client->getEncType()); } + public function testClientEmptyPostPut() + { + $client = new Client(); + $prepareHeadersReflection = new ReflectionMethod($client, 'prepareHeaders'); + $prepareHeadersReflection->setAccessible(true); + $request = new Request(); + $request->setMethod(Request::METHOD_POST); + $client->setRequest($request); + $this->assertSame($client->getRequest(), $request); + $headers = $prepareHeadersReflection->invoke($client, '', new Http('http://localhost:5984')); + $this->assertIsArray($headers); + $this->assertArrayHasKey('Content-Length', $headers); + $this->assertSame($headers['Content-Length'], 0); + $request = new Request(); + $request->setMethod(Request::METHOD_PUT); + $client->setRequest($request); + $this->assertSame($client->getRequest(), $request); + $headers = $prepareHeadersReflection->invoke($client, '', new Http('http://localhost:5984')); + $this->assertIsArray($headers); + $this->assertArrayHasKey('Content-Length', $headers); + $this->assertSame($headers['Content-Length'], 0); + } + /** * @group 7332 */