Index: trunk/phase3/tests/phpunit/includes/FauxResponseTest.php |
— | — | @@ -0,0 +1,70 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Tests for the FauxResponse class |
| 5 | + * |
| 6 | + * Copyright @ 2011 Alexandre Emsenhuber |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License along |
| 19 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + * http://www.gnu.org/copyleft/gpl.html |
| 22 | + * |
| 23 | + * @file |
| 24 | + */ |
| 25 | + |
| 26 | +class FauxResponseTest extends MediaWikiTestCase { |
| 27 | + var $response; |
| 28 | + |
| 29 | + function setUp() { |
| 30 | + $this->response = new FauxResponse; |
| 31 | + } |
| 32 | + |
| 33 | + function testCookie() { |
| 34 | + $this->assertEquals( null, $this->response->getcookie( 'key' ), 'Non-existing cookie' ); |
| 35 | + $this->response->setcookie( 'key', 'val' ); |
| 36 | + $this->assertEquals( 'val', $this->response->getcookie( 'key' ), 'Existing cookie' ); |
| 37 | + } |
| 38 | + |
| 39 | + function testHeader() { |
| 40 | + $this->assertEquals( null, $this->response->getheader( 'Location' ), 'Non-existing header' ); |
| 41 | + |
| 42 | + $this->response->header( 'Location: http://localhost/' ); |
| 43 | + $this->assertEquals( 'http://localhost/', $this->response->getheader( 'Location' ), 'Set header' ); |
| 44 | + |
| 45 | + $this->response->header( 'Location: http://127.0.0.1/' ); |
| 46 | + $this->assertEquals( 'http://127.0.0.1/', $this->response->getheader( 'Location' ), 'Same header' ); |
| 47 | + |
| 48 | + $this->response->header( 'Location: http://127.0.0.2/', false ); |
| 49 | + $this->assertEquals( 'http://127.0.0.1/', $this->response->getheader( 'Location' ), 'Same header with override disabled' ); |
| 50 | + } |
| 51 | + |
| 52 | + function testResponseCode() { |
| 53 | + $this->response->header( 'HTTP/1.1 200' ); |
| 54 | + $this->assertEquals( 200, $this->response->getStatusCode(), 'Header with no message' ); |
| 55 | + |
| 56 | + $this->response->header( 'HTTP/1.x 201' ); |
| 57 | + $this->assertEquals( 201, $this->response->getStatusCode(), 'Header with no message and protocol 1.x' ); |
| 58 | + |
| 59 | + $this->response->header( 'HTTP/1.1 202 OK' ); |
| 60 | + $this->assertEquals( 202, $this->response->getStatusCode(), 'Normal header' ); |
| 61 | + |
| 62 | + $this->response->header( 'HTTP/1.x 203 OK' ); |
| 63 | + $this->assertEquals( 203, $this->response->getStatusCode(), 'Normal header with no message and protocol 1.x' ); |
| 64 | + |
| 65 | + $this->response->header( 'HTTP/1.x 204 OK', false, 205 ); |
| 66 | + $this->assertEquals( 205, $this->response->getStatusCode(), 'Third parameter overrides the HTTP/... header' ); |
| 67 | + |
| 68 | + $this->response->header( 'Location: http://localhost/', false, 206 ); |
| 69 | + $this->assertEquals( 206, $this->response->getStatusCode(), 'Third parameter with another header' ); |
| 70 | + } |
| 71 | +} |
Property changes on: trunk/phase3/tests/phpunit/includes/FauxResponseTest.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 72 | + native |
Index: trunk/phase3/includes/WebResponse.php |
— | — | @@ -86,12 +86,11 @@ |
87 | 87 | * @param $http_response_code null|int Forces the HTTP response code to the specified value. |
88 | 88 | */ |
89 | 89 | public function header( $string, $replace = true, $http_response_code = null ) { |
90 | | - $match = array(); |
91 | 90 | if ( substr( $string, 0, 5 ) == 'HTTP/' ) { |
92 | 91 | $parts = explode( ' ', $string, 3 ); |
93 | 92 | $this->code = intval( $parts[1] ); |
94 | 93 | } else { |
95 | | - list( $key, $val ) = explode( ":", $string, 2 ); |
| 94 | + list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) ); |
96 | 95 | |
97 | 96 | if( $replace || !isset( $this->headers[$key] ) ) { |
98 | 97 | $this->headers[$key] = $val; |
— | — | @@ -108,7 +107,10 @@ |
109 | 108 | * @return string |
110 | 109 | */ |
111 | 110 | public function getheader( $key ) { |
112 | | - return $this->headers[$key]; |
| 111 | + if ( isset( $this->headers[$key] ) ) { |
| 112 | + return $this->headers[$key]; |
| 113 | + } |
| 114 | + return null; |
113 | 115 | } |
114 | 116 | |
115 | 117 | /** |