From 018caa47020d53c4d59d0e8c0c8570074556ea68 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 | 6 ++++++ test/ClientTest.php | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Client.php b/src/Client.php index 2d144102..1525e836 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1256,6 +1256,12 @@ protected function prepareHeaders($body, $uri) $headers['Content-Length'] = strlen($body); } } + else { + if ($this->getMethod() == 'POST') { + $headers['Content-Length'] = 0; + } + } + // Merge the headers of the request (if any) // here we need right 'http field' and not lowercase letters diff --git a/test/ClientTest.php b/test/ClientTest.php index 21dd8770..4ec2a0a2 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -505,6 +505,26 @@ public function testClientRequestMethod() $this->assertSame(Client::ENC_URLENCODED, $client->getEncType()); } + public function testClientEmptyPost() + { + $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); + + } + /** * @group 7332 */