2019-06-26 02:33:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Uri;
|
|
|
|
|
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
|
|
|
|
|
use MediaWiki\Rest\Handler;
|
|
|
|
|
use MediaWiki\Rest\HttpException;
|
2020-08-20 23:49:14 +00:00
|
|
|
use MediaWiki\Rest\RedirectException;
|
2021-07-02 11:12:00 +00:00
|
|
|
use MediaWiki\Rest\Reporter\ErrorReporter;
|
2019-06-26 02:33:35 +00:00
|
|
|
use MediaWiki\Rest\RequestData;
|
2019-06-12 19:51:59 +00:00
|
|
|
use MediaWiki\Rest\RequestInterface;
|
2020-08-20 23:49:14 +00:00
|
|
|
use MediaWiki\Rest\ResponseException;
|
2019-06-26 02:33:35 +00:00
|
|
|
use MediaWiki\Rest\ResponseFactory;
|
|
|
|
|
use MediaWiki\Rest\Router;
|
2019-06-12 19:51:59 +00:00
|
|
|
use MediaWiki\Rest\Validator\Validator;
|
2021-01-22 00:20:44 +00:00
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
2021-07-02 11:12:00 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-06-12 19:51:59 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2021-07-02 11:12:00 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
use Throwable;
|
2022-03-09 22:16:22 +00:00
|
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
2019-06-26 02:33:35 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Rest\Router
|
|
|
|
|
*/
|
2019-07-10 16:47:39 +00:00
|
|
|
class RouterTest extends \MediaWikiUnitTestCase {
|
2021-01-22 00:20:44 +00:00
|
|
|
use MockAuthorityTrait;
|
|
|
|
|
|
2021-07-02 11:12:00 +00:00
|
|
|
/** @var Throwable[] */
|
|
|
|
|
private $reportedErrors = [];
|
|
|
|
|
|
2021-01-16 19:44:17 +00:00
|
|
|
/**
|
|
|
|
|
* @param RequestInterface $request
|
|
|
|
|
* @param string|null $authError
|
|
|
|
|
* @param string[] $additionalRouteFiles
|
|
|
|
|
* @return Router
|
|
|
|
|
*/
|
2020-03-20 04:23:51 +00:00
|
|
|
private function createRouter(
|
|
|
|
|
RequestInterface $request,
|
|
|
|
|
$authError = null,
|
|
|
|
|
$additionalRouteFiles = []
|
|
|
|
|
) {
|
2019-06-12 19:51:59 +00:00
|
|
|
$objectFactory = new ObjectFactory(
|
|
|
|
|
$this->getMockForAbstractClass( ContainerInterface::class )
|
|
|
|
|
);
|
2020-03-20 04:23:51 +00:00
|
|
|
$routeFiles = array_merge( [ __DIR__ . '/testRoutes.json' ], $additionalRouteFiles );
|
2021-01-22 00:20:44 +00:00
|
|
|
$authority = $this->mockAnonUltimateAuthority();
|
2021-07-02 11:12:00 +00:00
|
|
|
|
|
|
|
|
/** @var MockObject|ErrorReporter $mockErrorReporter */
|
|
|
|
|
$mockErrorReporter = $this->createNoOpMock( ErrorReporter::class, [ 'reportError' ] );
|
|
|
|
|
$mockErrorReporter->method( 'reportError' )
|
|
|
|
|
->willReturnCallback( function ( $e ) {
|
|
|
|
|
$this->reportedErrors[] = $e;
|
|
|
|
|
} );
|
|
|
|
|
|
2019-06-26 02:33:35 +00:00
|
|
|
return new Router(
|
2020-03-20 04:23:51 +00:00
|
|
|
$routeFiles,
|
2019-06-26 02:33:35 +00:00
|
|
|
[],
|
2020-03-06 15:53:01 +00:00
|
|
|
'http://wiki.example.com',
|
2019-06-26 02:33:35 +00:00
|
|
|
'/rest',
|
|
|
|
|
new \EmptyBagOStuff(),
|
2019-07-16 22:43:43 +00:00
|
|
|
new ResponseFactory( [] ),
|
2019-06-12 19:51:59 +00:00
|
|
|
new StaticBasicAuthorizer( $authError ),
|
2021-01-06 18:10:15 +00:00
|
|
|
$authority,
|
2019-06-12 19:51:59 +00:00
|
|
|
$objectFactory,
|
2021-01-06 18:10:15 +00:00
|
|
|
new Validator( $objectFactory, $request, $authority ),
|
2021-07-02 11:12:00 +00:00
|
|
|
$mockErrorReporter,
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->createHookContainer()
|
2019-06-12 19:51:59 +00:00
|
|
|
);
|
2019-06-26 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPrefixMismatch() {
|
|
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/bogus' ) ] );
|
2019-06-12 19:51:59 +00:00
|
|
|
$router = $this->createRouter( $request );
|
2019-06-26 02:33:35 +00:00
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 404, $response->getStatusCode() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testWrongMethod() {
|
|
|
|
|
$request = new RequestData( [
|
2020-03-18 16:05:05 +00:00
|
|
|
'uri' => new Uri( '/rest/mock/RouterTest/hello' ),
|
2020-08-22 19:26:19 +00:00
|
|
|
'method' => 'TRACE'
|
2019-06-26 02:33:35 +00:00
|
|
|
] );
|
2019-06-12 19:51:59 +00:00
|
|
|
$router = $this->createRouter( $request );
|
2019-06-26 02:33:35 +00:00
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 405, $response->getStatusCode() );
|
|
|
|
|
$this->assertSame( 'Method Not Allowed', $response->getReasonPhrase() );
|
2020-08-22 19:26:19 +00:00
|
|
|
$this->assertSame( 'HEAD, GET', $response->getHeaderLine( 'Allow' ) );
|
2019-06-26 02:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-10 06:26:53 +00:00
|
|
|
public function testHeadToGet() {
|
|
|
|
|
$request = new RequestData( [
|
2020-03-18 16:05:05 +00:00
|
|
|
'uri' => new Uri( '/rest/mock/RouterTest/hello' ),
|
2019-09-10 06:26:53 +00:00
|
|
|
'method' => 'HEAD'
|
|
|
|
|
] );
|
|
|
|
|
$router = $this->createRouter( $request );
|
|
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 02:33:35 +00:00
|
|
|
public function testNoMatch() {
|
|
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/bogus' ) ] );
|
2019-06-12 19:51:59 +00:00
|
|
|
$router = $this->createRouter( $request );
|
2019-06-26 02:33:35 +00:00
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 404, $response->getStatusCode() );
|
|
|
|
|
// TODO: add more information to the response body and test for its presence here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function throwHandlerFactory() {
|
|
|
|
|
return new class extends Handler {
|
|
|
|
|
public function execute() {
|
|
|
|
|
throw new HttpException( 'Mock error', 555 );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 11:12:00 +00:00
|
|
|
public static function fatalHandlerFactory() {
|
|
|
|
|
return new class extends Handler {
|
|
|
|
|
public function execute() {
|
|
|
|
|
throw new RuntimeException( 'Fatal mock error', 12345 );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 23:49:14 +00:00
|
|
|
public static function throwRedirectHandlerFactory() {
|
|
|
|
|
return new class extends Handler {
|
|
|
|
|
public function execute() {
|
|
|
|
|
throw new RedirectException( 301, 'http://example.com' );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function throwWrappedHandlerFactory() {
|
|
|
|
|
return new class extends Handler {
|
|
|
|
|
public function execute() {
|
|
|
|
|
$response = $this->getResponseFactory()->create();
|
|
|
|
|
$response->setStatus( 200 );
|
|
|
|
|
throw new ResponseException( $response );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 11:12:00 +00:00
|
|
|
public function testHttpException() {
|
2019-06-26 02:33:35 +00:00
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/RouterTest/throw' ) ] );
|
2019-06-12 19:51:59 +00:00
|
|
|
$router = $this->createRouter( $request );
|
2019-06-26 02:33:35 +00:00
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 555, $response->getStatusCode() );
|
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
$body->rewind();
|
|
|
|
|
$data = json_decode( $body->getContents(), true );
|
|
|
|
|
$this->assertSame( 'Mock error', $data['message'] );
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 11:12:00 +00:00
|
|
|
public function testFatalException() {
|
|
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/RouterTest/fatal' ) ] );
|
|
|
|
|
$router = $this->createRouter( $request );
|
|
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 500, $response->getStatusCode() );
|
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
$body->rewind();
|
|
|
|
|
$data = json_decode( $body->getContents(), true );
|
|
|
|
|
$this->assertStringContainsString( 'RuntimeException', $data['message'] );
|
|
|
|
|
$this->assertNotEmpty( $this->reportedErrors );
|
|
|
|
|
$this->assertInstanceOf( RuntimeException::class, $this->reportedErrors[0] );
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 23:49:14 +00:00
|
|
|
public function testRedirectException() {
|
|
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/RouterTest/throwRedirect' ) ] );
|
|
|
|
|
$router = $this->createRouter( $request );
|
|
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 301, $response->getStatusCode() );
|
|
|
|
|
$this->assertSame( 'http://example.com', $response->getHeaderLine( 'Location' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testResponseException() {
|
|
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/RouterTest/throwWrapped' ) ] );
|
|
|
|
|
$router = $this->createRouter( $request );
|
|
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 02:33:35 +00:00
|
|
|
public function testBasicAccess() {
|
|
|
|
|
// Using the throwing handler is a way to assert that the handler is not executed
|
|
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/RouterTest/throw' ) ] );
|
2020-03-20 04:23:51 +00:00
|
|
|
$router = $this->createRouter( $request, 'test-error', [] );
|
2019-06-26 02:33:35 +00:00
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 403, $response->getStatusCode() );
|
|
|
|
|
$body = $response->getBody();
|
|
|
|
|
$body->rewind();
|
|
|
|
|
$data = json_decode( $body->getContents(), true );
|
|
|
|
|
$this->assertSame( 'test-error', $data['error'] );
|
|
|
|
|
}
|
2020-03-20 04:23:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providePaths
|
|
|
|
|
*/
|
|
|
|
|
public function testAdditionalEndpoints( $path ) {
|
|
|
|
|
$request = new RequestData( [
|
|
|
|
|
'uri' => new Uri( $path )
|
|
|
|
|
] );
|
|
|
|
|
$router = $this->createRouter(
|
|
|
|
|
$request,
|
|
|
|
|
null,
|
|
|
|
|
[ __DIR__ . '/testAdditionalRoutes.json' ]
|
|
|
|
|
);
|
|
|
|
|
$response = $router->execute( $request );
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function providePaths() {
|
|
|
|
|
return [
|
|
|
|
|
[ '/rest/mock/RouterTest/hello' ],
|
|
|
|
|
[ '/rest/mock/RouterTest/hello/two' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-03-06 15:53:01 +00:00
|
|
|
|
|
|
|
|
public function provideGetRouteUrl() {
|
2020-06-12 22:29:35 +00:00
|
|
|
yield 'empty' => [ '', '', [], [] ];
|
|
|
|
|
yield 'simple route' => [ '/foo/bar', '/foo/bar' ];
|
2020-06-16 13:52:40 +00:00
|
|
|
yield 'simple route with query' =>
|
|
|
|
|
[ '/foo/bar', '/foo/bar?x=1&y=2', [ 'x' => '1', 'y' => '2' ] ];
|
2020-06-12 22:29:35 +00:00
|
|
|
yield 'simple route with strange query chars' =>
|
|
|
|
|
[ '/foo+bar', '/foo+bar?x=%23&y=%25&z=%2B', [ 'x' => '#', 'y' => '%', 'z' => '+' ] ];
|
2020-06-16 13:52:40 +00:00
|
|
|
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%25%2F%2Bz/baz', [], [ 'test' => 'b%/+z' ] ];
|
|
|
|
|
yield 'space in path does not become a plus' =>
|
|
|
|
|
[ '/foo/{test}/baz', '/foo/b%20z/baz', [], [ 'test' => 'b z' ] ];
|
2020-06-12 22:29:35 +00:00
|
|
|
yield 'route with simple path params and query' =>
|
|
|
|
|
[ '/foo/{test}/baz', '/foo/bar/baz?x=1', [ 'x' => '1' ], [ 'test' => 'bar' ] ];
|
2020-03-06 15:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetRouteUrl
|
|
|
|
|
*/
|
2020-06-12 22:29:35 +00:00
|
|
|
public function testGetRouteUrl( $route, $expectedUrl, $query = [], $path = [] ) {
|
2020-03-06 15:53:01 +00:00
|
|
|
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/route' ) ] );
|
|
|
|
|
$router = $this->createRouter( $request );
|
|
|
|
|
|
2020-06-12 22:29:35 +00:00
|
|
|
$url = $router->getRouteUrl( $route, $path, $query );
|
2020-03-06 15:53:01 +00:00
|
|
|
$this->assertRegExp( '!^https?://[\w.]+/!', $url );
|
|
|
|
|
|
|
|
|
|
$uri = new Uri( $url );
|
2020-06-12 22:29:35 +00:00
|
|
|
$this->assertStringContainsString( $expectedUrl, $uri );
|
2020-03-06 15:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-26 02:33:35 +00:00
|
|
|
}
|