Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes #3861, request->headers() no longer returns empty array on Requ…
…est::$initial. Includes tests

Also fixed failing test Request::test_uri_only_trimed_on_internal()
  • Loading branch information
samsoir committed Apr 26, 2011
1 parent 85256c4 commit dfc2ebf
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 37 deletions.
42 changes: 6 additions & 36 deletions classes/kohana/request.php
Expand Up @@ -900,45 +900,15 @@ public function param($key = NULL, $default = NULL)
*
* @return $this
* @uses Request::$messages
* @deprecated This should not be here, it belongs in\n
* Response::send_headers() where it is implemented correctly.
*/
public function send_headers()
{
if ( ! headers_sent())
{
if (isset($_SERVER['SERVER_PROTOCOL']))
{
// Use the default server protocol
$protocol = $_SERVER['SERVER_PROTOCOL'];
}
else
{
// Default to using newer protocol
$protocol = 'HTTP/1.1';
}

// HTTP status line
header($protocol.' '.$this->status.' '.Request::$messages[$this->status]);

foreach ($this->headers as $name => $value)
{
if (is_string($name))
{
// Combine the name and value to make a raw header
$value = "{$name}: {$value}";
}

// Send the raw header
header($value, TRUE);
}

foreach (Session::$instances as $session)
{
// Sessions will most likely write cookies, which will be sent
// with the headers
$session->write();
}
}
if ( ! ($response = $this->response()) instanceof Response)
return $this;

$response->send_headers();
return $this;
}

Expand Down Expand Up @@ -1316,7 +1286,7 @@ public function headers($key = NULL, $value = NULL)
return $this;
}

if ( ! $this->_header AND $this->is_initial())
if ($this->_header->count() === 0 AND $this->is_initial())
{
// Lazy load the request headers
$this->_header = HTTP::request_headers();
Expand Down
89 changes: 88 additions & 1 deletion tests/kohana/RequestTest.php
Expand Up @@ -545,6 +545,8 @@ public function test_post_max_size_exceeded($content_length, $expected)
*/
public function provider_uri_only_trimed_on_internal()
{
(Request::$initial === NULL) AND Request::$initial = new Request;

return array(
array(
new Request('http://www.google.com'),
Expand Down Expand Up @@ -573,7 +575,7 @@ public function provider_uri_only_trimed_on_internal()
*
* @return void
*/
public function test_uri_only_trimed_on_internal($request, $expected)
public function test_uri_only_trimed_on_internal(Request $request, $expected)
{
$this->assertSame($request->uri(), $expected);
}
Expand Down Expand Up @@ -647,4 +649,89 @@ public function test_options_set_to_external_client($settings, $expected)
}
}

/**
* Provides data for test_headers_get()
*
* @return array
*/
public function provider_headers_get()
{
$x_powered_by = 'Kohana Unit Test';
$content_type = 'application/x-www-form-urlencoded';

return array(
array(
$request = Request::factory('foo/bar')
->headers(array(
'x-powered-by' => $x_powered_by,
'content-type' => $content_type
)
),
array(
'x-powered-by' => $x_powered_by,
'content-type' => $content_type
)
)
);
}

/**
* Tests getting headers from the Request object
*
* @dataProvider provider_headers_get
*
* @param Request request to test
* @param array headers to test against
* @return void
*/
public function test_headers_get($request, $headers)
{
foreach ($headers as $key => $expected_value)
{
$this->assertSame((string) $request->headers($key), $expected_value);
}
}

/**
* Provides data for test_headers_set
*
* @return array
*/
public function provider_headers_set()
{
return array(
array(
new Request,
array(
'content-type' => 'application/x-www-form-urlencoded',
'x-test-header' => 'foo'
),
"content-type: application/x-www-form-urlencoded\r\nx-test-header: foo\r\n\n"
),
array(
new Request,
array(
'content-type' => 'application/json',
'x-powered-by' => 'kohana'
),
"content-type: application/json\r\nx-powered-by: kohana\r\n\n"
)
);
}

/**
* Tests the setting of headers to the request object
*
* @dataProvider provider_headers_set
*
* @param Request request object
* @param array header(s) to set to the request object
* @param string expected http header
* @return void
*/
public function test_headers_set(Request $request, $headers, $expected)
{
$request->headers($headers);
$this->assertSame($expected, (string) $request->headers());
}
} // End Kohana_RequestTest

1 comment on commit dfc2ebf

@samsoir
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to add a commit comment, but this also refs #3944 as well - sorry, forgot I had done that.

Please sign in to comment.