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 6a8695e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,12 @@ protected function prepareHeaders($body, $uri)
$headers['Content-Length'] = strlen($body);
}
}

Check failure on line 1258 in src/Client.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.0, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Expected 1 space after closing brace; newline found
else {
if ($this->getMethod() === 'POST' || $this->getMethod() === 'PUT') {
$headers['Content-Length'] = 0;
}
}

Check failure on line 1264 in src/Client.php

View workflow job for this annotation

GitHub Actions / QA Checks (PHPCodeSniffer [8.0, locked], ubuntu-latest, laminas/laminas-continuous-integration-ac...

Unexpected blank line found

// Merge the headers of the request (if any)
// here we need right 'http field' and not lowercase letters
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 6a8695e

Please sign in to comment.