Make headers in FauxRequest case insensitive
authordaniel <daniel.kinzler@wikimedia.de>
Fri, 31 May 2013 13:43:31 +0000 (15:43 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Mon, 3 Jun 2013 14:23:11 +0000 (16:23 +0200)
HTTP headers are case insensitive per spec, and WebRequest
treats them like that, so FauxRequest should too.

Change-Id: I4257af7a8de2792ac556c670dcc7f28e4af4cb44

includes/WebRequest.php
includes/WebResponse.php
tests/phpunit/includes/FauxRequestTest.php [new file with mode: 0644]
tests/phpunit/includes/FauxResponseTest.php

index 20ee5e5..204268c 100644 (file)
@@ -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;
        }
 
index d1f645c..0deae96 100644 (file)
@@ -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 (file)
index 0000000..dfb0f13
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+class FauxRequestTest extends MediaWikiTestCase {
+
+       function testGetSetHeader() {
+               $value = 'test/test';
+
+               $request = new FauxRequest();
+               $request->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 );
+       }
+}
index 56691c9..977c22b 100644 (file)
@@ -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() {