This splits RouteFileModule into two classes, ExtraRoutesModule and SpecBasedModule. ExtraRoutesModule has no module prefix and supports only "flat" route definition files and additional routes from extension.json. SpecBasedModule represents a single module defined in a definition file similar to an OpenAPI spec. The idea is that a full OpenAPI spec can be generated by filling in any missing information based on information provided by the Handler implementation. In particular, the definition of parameters and request body schemas will be generated. A JSON schema for the new file format is added under docs/rest/. Support for the intermediate format introduced in Iebcde4645d4 is removed. It was not included in a release and was not being used outside core tests. Bug: T366837 Change-Id: I4ce306b0997f80b78a3d901e38bbfa8445bed604
143 lines
3.4 KiB
PHP
143 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
use MediaWiki\Config\HashConfig;
|
|
use MediaWiki\Config\ServiceOptions;
|
|
use MediaWiki\Context\RequestContext;
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
|
|
use MediaWiki\Rest\Handler;
|
|
use MediaWiki\Rest\Handler\ModuleSpecHandler;
|
|
use MediaWiki\Rest\Reporter\MWErrorReporter;
|
|
use MediaWiki\Rest\RequestData;
|
|
use MediaWiki\Rest\RequestInterface;
|
|
use MediaWiki\Rest\ResponseFactory;
|
|
use MediaWiki\Rest\Router;
|
|
use MediaWiki\Rest\Validator\Validator;
|
|
use MediaWikiIntegrationTestCase;
|
|
use Wikimedia\Message\ITextFormatter;
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
/**
|
|
* @covers \MediaWiki\Rest\Handler\ModuleSpecHandler
|
|
*
|
|
* @group Database
|
|
*/
|
|
class ModuleSpecHandlerTest extends MediaWikiIntegrationTestCase {
|
|
use HandlerTestTrait;
|
|
|
|
private function createRouter(
|
|
RequestInterface $request,
|
|
$specFile
|
|
): Router {
|
|
$services = $this->getServiceContainer();
|
|
$context = RequestContext::getMain();
|
|
|
|
$conf = $services->getMainConfig();
|
|
|
|
$authority = $context->getAuthority();
|
|
$authorizer = new StaticBasicAuthorizer();
|
|
|
|
$objectFactory = $services->getObjectFactory();
|
|
$restValidator = new Validator( $objectFactory,
|
|
$request,
|
|
$authority
|
|
);
|
|
|
|
$formatter = new class implements ITextFormatter {
|
|
public function getLangCode() {
|
|
return 'qqx';
|
|
}
|
|
|
|
public function format( MessageValue $message ) {
|
|
return $message->dump();
|
|
}
|
|
};
|
|
$responseFactory = new ResponseFactory( [ $formatter ] );
|
|
|
|
return ( new Router(
|
|
[ $specFile ],
|
|
[],
|
|
new ServiceOptions( Router::CONSTRUCTOR_OPTIONS, $conf ),
|
|
$services->getLocalServerObjectCache(),
|
|
$responseFactory,
|
|
$authorizer,
|
|
$authority,
|
|
$objectFactory,
|
|
$restValidator,
|
|
new MWErrorReporter(),
|
|
$services->getHookContainer(),
|
|
$context->getRequest()->getSession()
|
|
) );
|
|
}
|
|
|
|
private function newHandler() {
|
|
$config = new HashConfig( [
|
|
MainConfigNames::RightsUrl => '',
|
|
MainConfigNames::RightsText => '',
|
|
MainConfigNames::EmergencyContact => '',
|
|
MainConfigNames::Sitename => '',
|
|
] );
|
|
return new ModuleSpecHandler(
|
|
$config
|
|
);
|
|
}
|
|
|
|
public static function provideGetInfoSpecSuccess() {
|
|
yield 'named module' => [
|
|
[
|
|
'pathParams' => [ 'module' => 'mock', 'version' => 'v1' ],
|
|
],
|
|
__DIR__ . '/SpecTestRoutes.json'
|
|
];
|
|
yield 'OpenAPI module' => [
|
|
[
|
|
'pathParams' => [ 'module' => 'mock', 'version' => 'v1' ],
|
|
],
|
|
__DIR__ . '/SpecTestOpenAPIModule.json'
|
|
];
|
|
yield 'prefix-less module' => [
|
|
[
|
|
'pathParams' => [ 'module' => '-' ],
|
|
],
|
|
__DIR__ . '/SpecTestFlatRoutes.json'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideGetInfoSpecSuccess
|
|
*/
|
|
public function testGetInfoSpecSuccess( $params, $specFile ) {
|
|
$request = new RequestData( $params );
|
|
|
|
$router = $this->createRouter( $request, $specFile );
|
|
|
|
$handler = $this->newHandler();
|
|
$response = $this->executeHandler(
|
|
$handler,
|
|
$request,
|
|
[],
|
|
[],
|
|
[],
|
|
[],
|
|
null,
|
|
null,
|
|
$router
|
|
);
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
$this->assertArrayHasKey( 'Content-Type', $response->getHeaders() );
|
|
$this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
|
|
$data = json_decode( $response->getBody(), true );
|
|
$this->assertIsArray( $data, 'Body must be a JSON array' );
|
|
}
|
|
|
|
public static function newFooBarHandler() {
|
|
return new class extends Handler {
|
|
public function execute() {
|
|
return 'foo bar';
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|