| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler; |
| 4 |
|
| 5 |
use MediaWiki\Rest\Handler\RedirectHandler; |
| 6 |
use MediaWiki\Rest\RequestData; |
| 7 |
use MediaWiki\Rest\RouteDefinitionException; |
| 8 |
use MediaWikiIntegrationTestCase; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class RedirectHandlerTest extends MediaWikiIntegrationTestCase { |
| 14 |
use HandlerTestTrait; |
| 15 |
|
| 16 |
public function redirectConfigProvider() { |
| 17 |
return [ |
| 18 |
[ |
| 19 |
[ 'path' => '/v1/other/path/{param}', 'code' => 301 ], |
| 20 |
301, |
| 21 |
'/rest/v1/other/path/value' |
| 22 |
], |
| 23 |
[ |
| 24 |
[ 'path' => '/v1/other/path/{param}' ], |
| 25 |
308, |
| 26 |
'/rest/v1/other/path/value' |
| 27 |
], |
| 28 |
|
| 29 |
|
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
public function provideFailure() { |
| 34 |
return [ |
| 35 |
[ |
| 36 |
[ 'path' => '', 'code' => 308 ], |
| 37 |
RouteDefinitionException::class |
| 38 |
], |
| 39 |
[ |
| 40 |
[ 'code' => 308 ], |
| 41 |
RouteDefinitionException::class |
| 42 |
], |
| 43 |
|
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function testExecute( $redirectConfig, $expectedCode, $expectedLocation ) { |
| 51 |
$request = new RequestData( [ 'pathParams' => [ 'param' => 'value' ] ] ); |
| 52 |
$handler = new RedirectHandler(); |
| 53 |
|
| 54 |
|
| 55 |
$response = $this->executeHandler( $handler, $request, [ 'redirect' => $redirectConfig ] ); |
| 56 |
|
| 57 |
|
| 58 |
$this->assertEquals( $expectedCode, $response->getStatusCode() ); |
| 59 |
$this->assertEquals( $expectedLocation, $response->getHeaderLine( 'Location' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function testFailure( $redirectConfig, $expectedException ) { |
| 66 |
$request = new RequestData( [ 'pathParams' => [ 'param' => 'value' ] ] ); |
| 67 |
$handler = new RedirectHandler(); |
| 68 |
|
| 69 |
$this->expectException( $expectedException ); |
| 70 |
|
| 71 |
|
| 72 |
$this->executeHandler( $handler, $request, [ 'redirect' => $redirectConfig ] ); |
| 73 |
} |
| 74 |
} |