Skip to content

Commit

Permalink
Fix Content-Length for POST when data empty
Browse files Browse the repository at this point in the history
Signed-off-by: Jacques ROUSSEL <[email protected]>
  • Loading branch information
rouja committed Oct 30, 2023
1 parent 76de900 commit d16ce47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit d16ce47

Please sign in to comment.