Merge "Add config for serving main Page from the domain root"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / Rest / Handler / HelloHandlerTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use EmptyBagOStuff;
6 use GuzzleHttp\Psr7\Uri;
7 use MediaWiki\Permissions\PermissionManager;
8 use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer;
9 use MediaWiki\Rest\RequestData;
10 use MediaWiki\Rest\ResponseFactory;
11 use MediaWiki\Rest\Router;
12 use MediaWiki\Rest\Validator\Validator;
13 use Psr\Container\ContainerInterface;
14 use Wikimedia\ObjectFactory;
15 use User;
16
17 /**
18 * @covers \MediaWiki\Rest\Handler\HelloHandler
19 */
20 class HelloHandlerTest extends \MediaWikiUnitTestCase {
21 public static function provideTestViaRouter() {
22 return [
23 'normal' => [
24 [
25 'method' => 'GET',
26 'uri' => self::makeUri( '/user/Tim/hello' ),
27 ],
28 [
29 'statusCode' => 200,
30 'reasonPhrase' => 'OK',
31 'protocolVersion' => '1.1',
32 'body' => '{"message":"Hello, Tim!"}',
33 ],
34 ],
35 'method not allowed' => [
36 [
37 'method' => 'POST',
38 'uri' => self::makeUri( '/user/Tim/hello' ),
39 ],
40 [
41 'statusCode' => 405,
42 'reasonPhrase' => 'Method Not Allowed',
43 'protocolVersion' => '1.1',
44 'body' => '{"httpCode":405,"httpReason":"Method Not Allowed"}',
45 ],
46 ],
47 ];
48 }
49
50 private static function makeUri( $path ) {
51 return new Uri( "http://www.example.com/rest$path" );
52 }
53
54 /** @dataProvider provideTestViaRouter */
55 public function testViaRouter( $requestInfo, $responseInfo ) {
56 $objectFactory = new ObjectFactory(
57 $this->getMockForAbstractClass( ContainerInterface::class )
58 );
59 $permissionManager = $this->createMock( PermissionManager::class );
60 $request = new RequestData( $requestInfo );
61 $router = new Router(
62 [ __DIR__ . '/../testRoutes.json' ],
63 [],
64 '/rest',
65 new EmptyBagOStuff(),
66 new ResponseFactory( [] ),
67 new StaticBasicAuthorizer(),
68 $objectFactory,
69 new Validator( $objectFactory, $permissionManager, $request, new User )
70 );
71 $response = $router->execute( $request );
72 if ( isset( $responseInfo['statusCode'] ) ) {
73 $this->assertSame( $responseInfo['statusCode'], $response->getStatusCode() );
74 }
75 if ( isset( $responseInfo['reasonPhrase'] ) ) {
76 $this->assertSame( $responseInfo['reasonPhrase'], $response->getReasonPhrase() );
77 }
78 if ( isset( $responseInfo['protocolVersion'] ) ) {
79 $this->assertSame( $responseInfo['protocolVersion'], $response->getProtocolVersion() );
80 }
81 if ( isset( $responseInfo['body'] ) ) {
82 $this->assertSame( $responseInfo['body'], $response->getBody()->getContents() );
83 }
84 $this->assertSame(
85 [],
86 array_diff( array_keys( $responseInfo ), [
87 'statusCode',
88 'reasonPhrase',
89 'protocolVersion',
90 'body'
91 ] ),
92 '$responseInfo may not contain unknown keys' );
93 }
94 }