3 namespace MediaWiki\Tests\Rest\Handler
;
6 use GuzzleHttp\Psr7\Uri
;
7 use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer
;
8 use MediaWiki\Rest\RequestData
;
9 use MediaWiki\Rest\ResponseFactory
;
10 use MediaWiki\Rest\Router
;
11 use MediaWiki\Rest\Validator\Validator
;
12 use Psr\Container\ContainerInterface
;
13 use Wikimedia\ObjectFactory
;
17 * @covers \MediaWiki\Rest\Handler\HelloHandler
19 class HelloHandlerTest
extends \MediaWikiUnitTestCase
{
20 public static function provideTestViaRouter() {
25 'uri' => self
::makeUri( '/user/Tim/hello' ),
29 'reasonPhrase' => 'OK',
30 'protocolVersion' => '1.1',
31 'body' => '{"message":"Hello, Tim!"}',
34 'method not allowed' => [
37 'uri' => self
::makeUri( '/user/Tim/hello' ),
41 'reasonPhrase' => 'Method Not Allowed',
42 'protocolVersion' => '1.1',
43 'body' => '{"httpCode":405,"httpReason":"Method Not Allowed"}',
49 private static function makeUri( $path ) {
50 return new Uri( "http://www.example.com/rest$path" );
53 /** @dataProvider provideTestViaRouter */
54 public function testViaRouter( $requestInfo, $responseInfo ) {
55 $objectFactory = new ObjectFactory(
56 $this->getMockForAbstractClass( ContainerInterface
::class )
59 $request = new RequestData( $requestInfo );
61 [ __DIR__
. '/../testRoutes.json' ],
65 new ResponseFactory(),
66 new StaticBasicAuthorizer(),
68 new Validator( $objectFactory, $request, new User
)
70 $response = $router->execute( $request );
71 if ( isset( $responseInfo['statusCode'] ) ) {
72 $this->assertSame( $responseInfo['statusCode'], $response->getStatusCode() );
74 if ( isset( $responseInfo['reasonPhrase'] ) ) {
75 $this->assertSame( $responseInfo['reasonPhrase'], $response->getReasonPhrase() );
77 if ( isset( $responseInfo['protocolVersion'] ) ) {
78 $this->assertSame( $responseInfo['protocolVersion'], $response->getProtocolVersion() );
80 if ( isset( $responseInfo['body'] ) ) {
81 $this->assertSame( $responseInfo['body'], $response->getBody()->getContents() );
85 array_diff( array_keys( $responseInfo ), [
91 '$responseInfo may not contain unknown keys' );