Index: trunk/phase3/tests/phpunit/includes/HttpTest.php |
— | — | @@ -124,4 +124,49 @@ |
125 | 125 | ); |
126 | 126 | } |
127 | 127 | |
| 128 | + function testRelativeRedirections() { |
| 129 | + $h = new MWHttpRequestTester( 'http://oldsite/file.ext' ); |
| 130 | + # Forge a Location header |
| 131 | + $h->setRespHeaders( 'location', array( |
| 132 | + 'http://newsite/file.ext', |
| 133 | + '/newfile.ext', |
| 134 | + ) |
| 135 | + ); |
| 136 | + # Verify we correctly fix the Location |
| 137 | + $this->assertEquals( |
| 138 | + 'http://newsite/newfile.ext', |
| 139 | + $h->getFinalUrl(), |
| 140 | + "Relative file path Location: interpreted as full URL" |
| 141 | + ); |
| 142 | + |
| 143 | + $h->setRespHeaders( 'location', array( |
| 144 | + 'https://oldsite/file.ext' |
| 145 | + ) |
| 146 | + ); |
| 147 | + $this->assertEquals( |
| 148 | + 'https://oldsite/file.ext', |
| 149 | + $h->getFinalUrl(), |
| 150 | + "Location to the HTTPS version of the site" |
| 151 | + ); |
| 152 | + |
| 153 | + $h->setRespHeaders( 'location', array( |
| 154 | + '/anotherfile.ext', |
| 155 | + 'http://anotherfile/hoster.ext', |
| 156 | + 'https://anotherfile/hoster.ext' |
| 157 | + ) |
| 158 | + ); |
| 159 | + $this->assertEquals( |
| 160 | + 'https://anotherfile/hoster.ext', |
| 161 | + $h->getFinalUrl( "Relative file path Location: should keep the latest host and scheme!") |
| 162 | + ); |
| 163 | + } |
128 | 164 | } |
| 165 | + |
| 166 | +/** |
| 167 | + * Class to let us overwrite MWHttpREquest respHeaders variable |
| 168 | + */ |
| 169 | +class MWHttpRequestTester extends MWHttpRequest { |
| 170 | + function setRespHeaders( $name, $value ) { |
| 171 | + $this->respHeaders[$name] = $value ; |
| 172 | + } |
| 173 | +} |