Skip to content

Commit

Permalink
Merge pull request #665 from kohana/3.3/bug/request-content-length-af…
Browse files Browse the repository at this point in the history
…ter-render

Reset request `content-length` header during request execution
  • Loading branch information
acoulton committed Mar 10, 2016
2 parents 44a5df1 + 2102fcd commit e352b77
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions classes/Kohana/Request/Client/External.php
Expand Up @@ -127,6 +127,8 @@ public function execute_request(Request $request, Response $response)
->headers('content-type', 'application/x-www-form-urlencoded; charset='.Kohana::$charset);
}

$request->headers('content-length', (string) $request->content_length());

// If Kohana expose, set the user-agent
if (Kohana::$expose)
{
Expand Down
60 changes: 60 additions & 0 deletions tests/kohana/RequestTest.php
Expand Up @@ -736,10 +736,70 @@ public function test_passes_client_params()
$this->assertEquals($client->strict_redirect(), FALSE);
}

/**
* Tests correctness request content-length header after calling render
*/
public function test_content_length_after_render()
{
$request = Request::factory('https://example.org/post')
->client(new Kohana_RequestTest_Header_Spying_Request_Client_External)
->method(Request::POST)
->post(array('aaa' => 'bbb'));

$request->render();

$request->execute();

$headers = $request->client()->get_received_request_headers();

$this->assertEquals(strlen($request->body()), $headers['content-length']);
}

/**
* Tests correctness request content-length header after calling render
* and changing post
*/
public function test_content_length_after_changing_post()
{
$request = Request::factory('https://example.org/post')
->client(new Kohana_RequestTest_Header_Spying_Request_Client_External)
->method(Request::POST)
->post(array('aaa' => 'bbb'));

$request->render();

$request->post(array('one' => 'one', 'two' => 'two', 'three' => 'three'));

$request->execute();

$headers = $request->client()->get_received_request_headers();

$this->assertEquals(strlen($request->body()), $headers['content-length']);
}

} // End Kohana_RequestTest

/**
* A dummy Request_Client_External implementation, that spies on the headers
* of the request
*/
class Kohana_RequestTest_Header_Spying_Request_Client_External extends Request_Client_External
{
private $headers;

protected function _send_message(\Request $request, \Response $response)
{
$this->headers = $request->headers();

return $response;
}

public function get_received_request_headers()
{
return $this->headers;
}
}

class Controller_Kohana_RequestTest_Dummy extends Controller
{
// hard coded dummy response
Expand Down

0 comments on commit e352b77

Please sign in to comment.