wiki.techinc.nl/tests/phpunit/unit/includes/Rest/RouterTest.php
Brad Jorsch ebfbd2d42a rest: Use ParamValidator library, add BodyValidator
Parameter validation is based on parameter definitions like those in the
Action API, using the new ParamValidator library. Handlers should use
the provided Handler methods to access parameters rather than fetching
them directly from the RequestInterface.

Body validation allows the handler to have the (non-form-data) body of a
request parsed and validated. The only validator included in this patch
ignores the body entirely; future patches may implement validation for
JSON bodies based on JSON schemas, or the like.

Bug: T223239
Change-Id: I3c37ea2b432840514b6bff90007c8403989225d5
2019-09-04 10:12:35 -04:00

96 lines
3.1 KiB
PHP

<?php
namespace MediaWiki\Tests\Rest;
use GuzzleHttp\Psr7\Uri;
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\HttpException;
use MediaWiki\Rest\RequestData;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\ResponseFactory;
use MediaWiki\Rest\Router;
use MediaWiki\Rest\Validator\Validator;
use Psr\Container\ContainerInterface;
use Wikimedia\ObjectFactory;
use User;
/**
* @covers \MediaWiki\Rest\Router
*/
class RouterTest extends \MediaWikiUnitTestCase {
/** @return Router */
private function createRouter( RequestInterface $request, $authError = null ) {
$objectFactory = new ObjectFactory(
$this->getMockForAbstractClass( ContainerInterface::class )
);
return new Router(
[ __DIR__ . '/testRoutes.json' ],
[],
'/rest',
new \EmptyBagOStuff(),
new ResponseFactory(),
new StaticBasicAuthorizer( $authError ),
$objectFactory,
new Validator( $objectFactory, $request, new User )
);
}
public function testPrefixMismatch() {
$request = new RequestData( [ 'uri' => new Uri( '/bogus' ) ] );
$router = $this->createRouter( $request );
$response = $router->execute( $request );
$this->assertSame( 404, $response->getStatusCode() );
}
public function testWrongMethod() {
$request = new RequestData( [
'uri' => new Uri( '/rest/user/joe/hello' ),
'method' => 'OPTIONS'
] );
$router = $this->createRouter( $request );
$response = $router->execute( $request );
$this->assertSame( 405, $response->getStatusCode() );
$this->assertSame( 'Method Not Allowed', $response->getReasonPhrase() );
$this->assertSame( 'GET', $response->getHeaderLine( 'Allow' ) );
}
public function testNoMatch() {
$request = new RequestData( [ 'uri' => new Uri( '/rest/bogus' ) ] );
$router = $this->createRouter( $request );
$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 );
}
};
}
public function testException() {
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/RouterTest/throw' ) ] );
$router = $this->createRouter( $request );
$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'] );
}
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' ) ] );
$router = $this->createRouter( $request, 'test-error' );
$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'] );
}
}