From 1da2dd79830ee5474086929bea34291841eebc17 Mon Sep 17 00:00:00 2001 From: daniel Date: Fri, 31 May 2013 15:43:31 +0200 Subject: [PATCH] Make headers in FauxRequest case insensitive HTTP headers are case insensitive per spec, and WebRequest treats them like that, so FauxRequest should too. Change-Id: I4257af7a8de2792ac556c670dcc7f28e4af4cb44 --- includes/WebRequest.php | 4 +++- includes/WebResponse.php | 6 +++++- tests/phpunit/includes/FauxRequestTest.php | 15 +++++++++++++++ tests/phpunit/includes/FauxResponseTest.php | 3 +++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/includes/FauxRequestTest.php diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 20ee5e584d..204268c7d5 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -1338,10 +1338,11 @@ class FauxRequest extends WebRequest { } /** - * @param $name + * @param string $name The name of the header to get (case insensitive). * @return bool|string */ public function getHeader( $name ) { + $name = strtoupper( $name ); return isset( $this->headers[$name] ) ? $this->headers[$name] : false; } @@ -1350,6 +1351,7 @@ class FauxRequest extends WebRequest { * @param $val string */ public function setHeader( $name, $val ) { + $name = strtoupper( $name ); $this->headers[$name] = $val; } diff --git a/includes/WebResponse.php b/includes/WebResponse.php index d1f645cbcb..0deae965ac 100644 --- a/includes/WebResponse.php +++ b/includes/WebResponse.php @@ -116,6 +116,8 @@ class FauxResponse extends WebResponse { } else { list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) ); + $key = strtoupper( $key ); + if ( $replace || !isset( $this->headers[$key] ) ) { $this->headers[$key] = $val; } @@ -127,10 +129,12 @@ class FauxResponse extends WebResponse { } /** - * @param $key string + * @param string $key The name of the header to get (case insensitive). * @return string */ public function getheader( $key ) { + $key = strtoupper( $key ); + if ( isset( $this->headers[$key] ) ) { return $this->headers[$key]; } diff --git a/tests/phpunit/includes/FauxRequestTest.php b/tests/phpunit/includes/FauxRequestTest.php new file mode 100644 index 0000000000..dfb0f13ca5 --- /dev/null +++ b/tests/phpunit/includes/FauxRequestTest.php @@ -0,0 +1,15 @@ +setHeader( 'Content-Type', $value ); + + $this->assertEquals( $request->getHeader( 'Content-Type' ), $value ); + $this->assertEquals( $request->getHeader( 'CONTENT-TYPE' ), $value ); + $this->assertEquals( $request->getHeader( 'content-type' ), $value ); + } +} diff --git a/tests/phpunit/includes/FauxResponseTest.php b/tests/phpunit/includes/FauxResponseTest.php index 56691c9e01..977c22bc83 100644 --- a/tests/phpunit/includes/FauxResponseTest.php +++ b/tests/phpunit/includes/FauxResponseTest.php @@ -47,6 +47,9 @@ class FauxResponseTest extends MediaWikiTestCase { $this->response->header( 'Location: http://127.0.0.2/', false ); $this->assertEquals( 'http://127.0.0.1/', $this->response->getheader( 'Location' ), 'Same header with override disabled' ); + + $this->response->header( 'Location: http://localhost/' ); + $this->assertEquals( 'http://localhost/', $this->response->getheader( 'LOCATION' ), 'Get header case insensitive' ); } function testResponseCode() { -- 2.20.1