Merge "HandlerTestTrait::getRouteUrl behaving differently than Router::getRouteUrl"
This commit is contained in:
commit
275d4a2570
4 changed files with 25 additions and 27 deletions
|
|
@ -86,12 +86,7 @@ abstract class Handler {
|
|||
*/
|
||||
protected function getRouteUrl( $pathParams = [], $queryParams = [] ): string {
|
||||
$path = $this->getConfig()['path'];
|
||||
|
||||
foreach ( $pathParams as $param => $value ) {
|
||||
$path = str_replace( '{' . $param . '}', urlencode( $value ), $path );
|
||||
}
|
||||
|
||||
return $this->router->getRouteUrl( $path, $queryParams );
|
||||
return $this->router->getRouteUrl( $path, $pathParams, $queryParams );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ namespace MediaWiki\Rest;
|
|||
|
||||
use AppendIterator;
|
||||
use BagOStuff;
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use MediaWiki\HookContainer\HookContainer;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Rest\BasicAccess\BasicAuthorizerInterface;
|
||||
|
|
@ -240,21 +239,19 @@ class Router {
|
|||
* Returns a full URL for the given route.
|
||||
* Intended for use in redirects.
|
||||
*
|
||||
* @param string $route the route, with any necessary URL encoding already applied
|
||||
* @param array $query
|
||||
* @param string $route
|
||||
* @param array $pathParams
|
||||
* @param array $queryParams
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function getRouteUrl( $route, $query = [] ) {
|
||||
$url = $this->baseUrl . $this->rootPath . $route;
|
||||
|
||||
if ( $query ) {
|
||||
$uri = new Uri( $url );
|
||||
$uri = Uri::withQueryValues( $uri, $query );
|
||||
$url = "$uri";
|
||||
public function getRouteUrl( $route, $pathParams = [], $queryParams = [] ) {
|
||||
foreach ( $pathParams as $param => $value ) {
|
||||
$route = str_replace( '{' . $param . '}', urlencode( $value ), $route );
|
||||
}
|
||||
|
||||
return $url;
|
||||
$url = $this->baseUrl . $this->rootPath . $route;
|
||||
return wfAppendQuery( $url, $queryParams );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -72,7 +72,10 @@ trait HandlerTestTrait {
|
|||
|
||||
/** @var Router|MockObject $router */
|
||||
$router = $this->createNoOpMock( Router::class, [ 'getRouteUrl' ] );
|
||||
$router->method( 'getRouteUrl' )->willReturnCallback( function ( $route, $query = [] ) {
|
||||
$router->method( 'getRouteUrl' )->willReturnCallback( function ( $route, $path = [], $query = [] ) {
|
||||
foreach ( $path as $param => $value ) {
|
||||
$route = str_replace( '{' . $param . '}', urlencode( $value ), $route );
|
||||
}
|
||||
return wfAppendQuery( 'https://wiki.example.com/rest' . $route, $query );
|
||||
} );
|
||||
|
||||
|
|
|
|||
|
|
@ -137,26 +137,29 @@ class RouterTest extends \MediaWikiUnitTestCase {
|
|||
}
|
||||
|
||||
public function provideGetRouteUrl() {
|
||||
yield 'empty' => [ '' ];
|
||||
yield 'simple route' => [ '/foo/bar' ];
|
||||
yield 'simple route with query' => [ '/foo/bar', [ 'x' => '1', 'y' => '2' ] ];
|
||||
yield 'strange chars' => [ '/foo+bar', [ 'x' => '#', 'y' => '%' ] ];
|
||||
yield 'empty' => [ '', '', [], [] ];
|
||||
yield 'simple route' => [ '/foo/bar', '/foo/bar' ];
|
||||
yield 'simple route with query' => [ '/foo/bar', '/foo/bar?x=1&y=2', [ 'x' => '1', 'y' => '2' ] ];
|
||||
yield 'simple route with strange query chars' =>
|
||||
[ '/foo+bar', '/foo+bar?x=%23&y=%25&z=%2B', [ 'x' => '#', 'y' => '%', 'z' => '+' ] ];
|
||||
yield 'route with simple path params' => [ '/foo/{test}/baz', '/foo/bar/baz', [], [ 'test' => 'bar' ] ];
|
||||
yield 'route with strange path params' => [ '/foo/{test}/baz', '/foo/b%2Bz/baz', [], [ 'test' => 'b+z' ] ];
|
||||
yield 'route with simple path params and query' =>
|
||||
[ '/foo/{test}/baz', '/foo/bar/baz?x=1', [ 'x' => '1' ], [ 'test' => 'bar' ] ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideGetRouteUrl
|
||||
*/
|
||||
public function testGetRouteUrl( $route, $query = [] ) {
|
||||
public function testGetRouteUrl( $route, $expectedUrl, $query = [], $path = [] ) {
|
||||
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/route' ) ] );
|
||||
$router = $this->createRouter( $request );
|
||||
|
||||
$url = $router->getRouteUrl( $route, $query );
|
||||
$url = $router->getRouteUrl( $route, $path, $query );
|
||||
$this->assertRegExp( '!^https?://[\w.]+/!', $url );
|
||||
|
||||
$uri = new Uri( $url );
|
||||
|
||||
$this->assertSame( wfArrayToCgi( $query ), $uri->getQuery() );
|
||||
$this->assertStringContainsString( $route, $uri->getPath() );
|
||||
$this->assertStringContainsString( $expectedUrl, $uri );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue