From dfc2ebf22b7517d73ddd54f41acde1df1d434fcc Mon Sep 17 00:00:00 2001 From: Sam de Freyssinet Date: Tue, 26 Apr 2011 16:50:57 +0100 Subject: [PATCH] Fixes #3861, request->headers() no longer returns empty array on Request::$initial. Includes tests Also fixed failing test Request::test_uri_only_trimed_on_internal() --- classes/kohana/request.php | 42 +++-------------- tests/kohana/RequestTest.php | 89 +++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 37 deletions(-) diff --git a/classes/kohana/request.php b/classes/kohana/request.php index 54187522f..8ddcd0c7e 100644 --- a/classes/kohana/request.php +++ b/classes/kohana/request.php @@ -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; } @@ -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(); diff --git a/tests/kohana/RequestTest.php b/tests/kohana/RequestTest.php index 3570b272c..e138fb001 100644 --- a/tests/kohana/RequestTest.php +++ b/tests/kohana/RequestTest.php @@ -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'), @@ -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); } @@ -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 \ No newline at end of file