| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest; |
| 4 |
|
| 5 |
use GuzzleHttp\Psr7\Uri; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer; |
| 9 |
use MediaWiki\Rest\CorsUtils; |
| 10 |
use MediaWiki\Rest\Handler; |
| 11 |
use MediaWiki\Rest\HttpException; |
| 12 |
use MediaWiki\Rest\RedirectException; |
| 13 |
use MediaWiki\Rest\Reporter\ErrorReporter; |
| 14 |
use MediaWiki\Rest\RequestData; |
| 15 |
use MediaWiki\Rest\RequestInterface; |
| 16 |
use MediaWiki\Rest\ResponseException; |
| 17 |
use MediaWiki\Rest\ResponseFactory; |
| 18 |
use MediaWiki\Rest\Router; |
| 19 |
use MediaWiki\Rest\StringStream; |
| 20 |
use MediaWiki\Rest\Validator\JsonBodyValidator; |
| 21 |
use MediaWiki\Tests\Rest\Handler\HelloHandler; |
| 22 |
use MediaWiki\User\UserIdentityValue; |
| 23 |
use MediaWikiUnitTestCase; |
| 24 |
use PHPUnit\Framework\Assert; |
| 25 |
use PHPUnit\Framework\MockObject\MockObject; |
| 26 |
use Psr\Log\NullLogger; |
| 27 |
use RuntimeException; |
| 28 |
use Throwable; |
| 29 |
use UDPTransport; |
| 30 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 31 |
use Wikimedia\ParamValidator\ParamValidator; |
| 32 |
use Wikimedia\Stats\OutputFormats; |
| 33 |
use Wikimedia\Stats\StatsCache; |
| 34 |
use Wikimedia\Stats\StatsFactory; |
| 35 |
use Wikimedia\TestingAccessWrapper; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class RouterTest extends MediaWikiUnitTestCase { |
| 42 |
use RestTestTrait; |
| 43 |
|
| 44 |
private const CANONICAL_SERVER = 'https://wiki.example.com'; |
| 45 |
private const INTERNAL_SERVER = 'http://api.local:8080'; |
| 46 |
|
| 47 |
|
| 48 |
private $reportedErrors = []; |
| 49 |
|
| 50 |
|
| 51 |
private $cacheBag; |
| 52 |
|
| 53 |
protected function setUp(): void { |
| 54 |
parent::setUp(); |
| 55 |
$this->cacheBag = new HashBagOStuff(); |
| 56 |
} |
| 57 |
|
| 58 |
private function createRouter( |
| 59 |
RequestInterface $request, |
| 60 |
?string $authError = null, |
| 61 |
array $routeFiles = [ __DIR__ . '/testRoutes.json' ] |
| 62 |
): Router { |
| 63 |
|
| 64 |
$mockErrorReporter = $this->createNoOpMock( ErrorReporter::class, [ 'reportError' ] ); |
| 65 |
$mockErrorReporter->method( 'reportError' ) |
| 66 |
->willReturnCallback( function ( $e ) { |
| 67 |
$this->reportedErrors[] = $e; |
| 68 |
} ); |
| 69 |
|
| 70 |
$config = [ |
| 71 |
MainConfigNames::CanonicalServer => self::CANONICAL_SERVER, |
| 72 |
MainConfigNames::InternalServer => self::INTERNAL_SERVER, |
| 73 |
MainConfigNames::RestPath => '/rest', |
| 74 |
MainConfigNames::ScriptPath => '/w' |
| 75 |
]; |
| 76 |
|
| 77 |
$extraRoutes = [ |
| 78 |
[ 'path' => '/', 'class' => HelloHandler::class ] |
| 79 |
]; |
| 80 |
|
| 81 |
return $this->newRouter( [ |
| 82 |
'routeFiles' => $routeFiles, |
| 83 |
'extraRoutes' => $extraRoutes, |
| 84 |
'request' => $request, |
| 85 |
'config' => $config, |
| 86 |
'cacheBag' => $this->cacheBag, |
| 87 |
'errorReporter' => $mockErrorReporter, |
| 88 |
'basicAuth' => new StaticBasicAuthorizer( $authError ), |
| 89 |
] ); |
| 90 |
} |
| 91 |
|
| 92 |
private function createMockStatsFactory( string $expectedValue ): StatsFactory { |
| 93 |
$statsCache = new StatsCache(); |
| 94 |
$emitter = OutputFormats::getNewEmitter( |
| 95 |
'mediawiki', |
| 96 |
$statsCache, |
| 97 |
OutputFormats::getNewFormatter( OutputFormats::DOGSTATSD ) |
| 98 |
); |
| 99 |
|
| 100 |
$transport = $this->createMock( UDPTransport::class ); |
| 101 |
|
| 102 |
$transport->expects( $this->once() )->method( "emit" ) |
| 103 |
->with( $this->matchesRegularExpression( $expectedValue ) ); |
| 104 |
|
| 105 |
$emitter = $emitter->withTransport( $transport ); |
| 106 |
return new StatsFactory( $statsCache, $emitter, new NullLogger ); |
| 107 |
} |
| 108 |
|
| 109 |
public function testEmptyPath() { |
| 110 |
|
| 111 |
$request = new RequestData( [ 'uri' => new Uri( '/rest' ) ] ); |
| 112 |
$router = $this->createRouter( $request ); |
| 113 |
$response = $router->execute( $request ); |
| 114 |
$this->assertSame( 308, $response->getStatusCode() ); |
| 115 |
$this->assertSame( '/rest/', $response->getHeaderLine( 'location' ) ); |
| 116 |
} |
| 117 |
|
| 118 |
public function testRootPath() { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/' ) ] ); |
| 123 |
$router = $this->createRouter( $request ); |
| 124 |
$response = $router->execute( $request ); |
| 125 |
$this->assertSame( 200, $response->getStatusCode(), (string)$response->getBody() ); |
| 126 |
} |
| 127 |
|
| 128 |
public function testPrefixMismatch() { |
| 129 |
$request = new RequestData( [ 'uri' => new Uri( '/bogus' ) ] ); |
| 130 |
$router = $this->createRouter( $request ); |
| 131 |
$response = $router->execute( $request ); |
| 132 |
$this->assertSame( 404, $response->getStatusCode() ); |
| 133 |
} |
| 134 |
|
| 135 |
public function testWrongMethod() { |
| 136 |
$request = new RequestData( [ |
| 137 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/hello' ), |
| 138 |
'method' => 'TRACE' |
| 139 |
] ); |
| 140 |
$router = $this->createRouter( $request ); |
| 141 |
$response = $router->execute( $request ); |
| 142 |
$this->assertSame( 405, $response->getStatusCode() ); |
| 143 |
$this->assertSame( 'Method Not Allowed', $response->getReasonPhrase() ); |
| 144 |
$this->assertSame( 'HEAD, GET', $response->getHeaderLine( 'Allow' ) ); |
| 145 |
} |
| 146 |
|
| 147 |
public function testGetFromUglyPath() { |
| 148 |
$request = new RequestData( [ |
| 149 |
'uri' => new Uri( '/w/rest.php/mock/v1/RouterTest/hello' ), |
| 150 |
'method' => 'GET' |
| 151 |
] ); |
| 152 |
$router = $this->createRouter( $request ); |
| 153 |
$response = $router->execute( $request ); |
| 154 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 155 |
} |
| 156 |
|
| 157 |
public function testHeadToGet() { |
| 158 |
$request = new RequestData( [ |
| 159 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/hello' ), |
| 160 |
'method' => 'HEAD' |
| 161 |
] ); |
| 162 |
$router = $this->createRouter( $request ); |
| 163 |
$response = $router->execute( $request ); |
| 164 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 165 |
} |
| 166 |
|
| 167 |
public function testCorsPreflight() { |
| 168 |
$cors = $this->getCorsUtils(); |
| 169 |
|
| 170 |
$request = new RequestData( [ |
| 171 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/hello' ), |
| 172 |
'method' => 'OPTIONS' |
| 173 |
] ); |
| 174 |
$router = $this->createRouter( $request ); |
| 175 |
$router->setCors( $cors ); |
| 176 |
|
| 177 |
$response = $router->execute( $request ); |
| 178 |
$this->assertSame( 204, $response->getStatusCode() ); |
| 179 |
$this->assertSame( |
| 180 |
[ 'HEAD', 'GET', ], |
| 181 |
$response->getHeader( 'Access-Control-Allow-Methods' ) |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
public function testNoMatch() { |
| 186 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/bogus' ) ] ); |
| 187 |
$router = $this->createRouter( $request ); |
| 188 |
$response = $router->execute( $request ); |
| 189 |
$this->assertSame( 404, $response->getStatusCode() ); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public static function throwHandlerFactory(): Handler { |
| 197 |
return new class extends Handler { |
| 198 |
public function execute() { |
| 199 |
throw new HttpException( 'Mock error', 555 ); |
| 200 |
} |
| 201 |
}; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public static function fatalHandlerFactory(): Handler { |
| 208 |
return new class extends Handler { |
| 209 |
public function execute() { |
| 210 |
throw new RuntimeException( 'Fatal mock error', 12345 ); |
| 211 |
} |
| 212 |
}; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public static function throwRedirectHandlerFactory(): Handler { |
| 219 |
return new class extends Handler { |
| 220 |
public function execute() { |
| 221 |
throw new RedirectException( 301, 'http://example.com' ); |
| 222 |
} |
| 223 |
}; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static function throwWrappedHandlerFactory(): Handler { |
| 230 |
return new class extends Handler { |
| 231 |
public function execute() { |
| 232 |
$response = $this->getResponseFactory()->create(); |
| 233 |
$response->setStatus( 200 ); |
| 234 |
throw new ResponseException( $response ); |
| 235 |
} |
| 236 |
}; |
| 237 |
} |
| 238 |
|
| 239 |
public function testHttpException() { |
| 240 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/RouterTest/throw' ) ] ); |
| 241 |
$router = $this->createRouter( $request ); |
| 242 |
|
| 243 |
$stats = $this->createMockStatsFactory( |
| 244 |
"/^mediawiki\.rest_api_errors_total:1\|c\|#path:mock_v1_RouterTest_throw,method:GET,status:555\nmediawiki\.stats_buffered_total:1\|c$/" |
| 245 |
); |
| 246 |
$router->setStats( $stats ); |
| 247 |
|
| 248 |
$response = $router->execute( $request ); |
| 249 |
$stats->flush(); |
| 250 |
$this->assertSame( 555, $response->getStatusCode(), (string)$response->getBody() ); |
| 251 |
$body = $response->getBody(); |
| 252 |
$body->rewind(); |
| 253 |
$data = json_decode( $body->getContents(), true ); |
| 254 |
$this->assertSame( 'Mock error', $data['message'] ); |
| 255 |
} |
| 256 |
|
| 257 |
public function testFatalException() { |
| 258 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/RouterTest/fatal' ) ] ); |
| 259 |
$router = $this->createRouter( $request ); |
| 260 |
$response = $router->execute( $request ); |
| 261 |
$this->assertSame( 500, $response->getStatusCode(), (string)$response->getBody() ); |
| 262 |
$body = $response->getBody(); |
| 263 |
$body->rewind(); |
| 264 |
$data = json_decode( $body->getContents(), true ); |
| 265 |
$this->assertStringContainsString( 'RuntimeException', $data['message'] ); |
| 266 |
$this->assertNotEmpty( $this->reportedErrors ); |
| 267 |
$this->assertInstanceOf( RuntimeException::class, $this->reportedErrors[0] ); |
| 268 |
} |
| 269 |
|
| 270 |
public function testRedirectException() { |
| 271 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/RouterTest/throwRedirect' ) ] ); |
| 272 |
$router = $this->createRouter( $request ); |
| 273 |
|
| 274 |
$stats = $this->createMockStatsFactory( |
| 275 |
"/^mediawiki\.rest_api_latency_seconds:\d+\.\d+\|ms\|#path:mock_v1_RouterTest_throwRedirect,method:GET,status:301\nmediawiki\.stats_buffered_total:1\|c$/" |
| 276 |
); |
| 277 |
$router->setStats( $stats ); |
| 278 |
|
| 279 |
$response = $router->execute( $request ); |
| 280 |
$stats->flush(); |
| 281 |
$this->assertSame( 301, $response->getStatusCode(), (string)$response->getBody() ); |
| 282 |
$this->assertSame( 'http://example.com', $response->getHeaderLine( 'Location' ) ); |
| 283 |
} |
| 284 |
|
| 285 |
public function testRedirectDefinition() { |
| 286 |
|
| 287 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/RouterTest/redirect' ) ] ); |
| 288 |
$router = $this->createRouter( $request ); |
| 289 |
$response = $router->execute( $request ); |
| 290 |
$this->assertSame( 308, $response->getStatusCode(), (string)$response->getBody() ); |
| 291 |
$this->assertSame( '/rest/mock/RouterTest/redirectTarget', $response->getHeaderLine( 'Location' ) ); |
| 292 |
} |
| 293 |
|
| 294 |
public function testResponseException() { |
| 295 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/RouterTest/throwWrapped' ) ] ); |
| 296 |
$router = $this->createRouter( $request ); |
| 297 |
|
| 298 |
$stats = $this->createMockStatsFactory( |
| 299 |
"/^mediawiki\.rest_api_latency_seconds:\d+\.\d+\|ms\|#path:mock_v1_RouterTest_throwWrapped,method:GET,status:200\nmediawiki\.stats_buffered_total:1\|c$/" |
| 300 |
); |
| 301 |
$router->setStats( $stats ); |
| 302 |
|
| 303 |
$response = $router->execute( $request ); |
| 304 |
$stats->flush(); |
| 305 |
$this->assertSame( 200, $response->getStatusCode(), (string)$response->getBody() ); |
| 306 |
} |
| 307 |
|
| 308 |
public function testBasicAccess() { |
| 309 |
|
| 310 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/RouterTest/throw' ) ] ); |
| 311 |
$router = $this->createRouter( $request, 'test-error' ); |
| 312 |
$response = $router->execute( $request ); |
| 313 |
$this->assertSame( 403, $response->getStatusCode() ); |
| 314 |
$body = $response->getBody(); |
| 315 |
$body->rewind(); |
| 316 |
$data = json_decode( $body->getContents(), true ); |
| 317 |
$this->assertSame( 'test-error', $data['error'] ); |
| 318 |
} |
| 319 |
|
| 320 |
public function testAdditionalEndpoints() { |
| 321 |
$request = new RequestData( [ |
| 322 |
'uri' => new Uri( '/rest/mock-too/RouterTest/hello/two' ) |
| 323 |
] ); |
| 324 |
$router = $this->createRouter( |
| 325 |
$request, |
| 326 |
null, |
| 327 |
|
| 328 |
[ __DIR__ . '/testRoutes.json', __DIR__ . '/testAdditionalRoutes.json' ] |
| 329 |
); |
| 330 |
|
| 331 |
|
| 332 |
$this->assertSame( [ 'mock/v1', '' ], $router->getModuleIds() ); |
| 333 |
|
| 334 |
$response = $router->execute( $request ); |
| 335 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 336 |
} |
| 337 |
|
| 338 |
public function testFlatRouteFile() { |
| 339 |
$request = new RequestData( [ |
| 340 |
'uri' => new Uri( '/rest/ModuleTest/hello/you' ) |
| 341 |
] ); |
| 342 |
$router = $this->createRouter( |
| 343 |
$request, |
| 344 |
null, |
| 345 |
[ __DIR__ . '/Module/moduleFlatRoutes.json' ] |
| 346 |
); |
| 347 |
|
| 348 |
$this->assertSame( [ '' ], $router->getModuleIds() ); |
| 349 |
|
| 350 |
$response = $router->execute( $request ); |
| 351 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 352 |
} |
| 353 |
|
| 354 |
public static function providePaths() { |
| 355 |
return [ |
| 356 |
[ '/rest/mock/v1/RouterTest/hello' ], |
| 357 |
[ '/rest/mock-too/RouterTest/hello/two' ], |
| 358 |
]; |
| 359 |
} |
| 360 |
|
| 361 |
public static function provideGetRouteUrl() { |
| 362 |
yield 'empty' => [ '', '', [], [] ]; |
| 363 |
yield 'simple route' => [ '/foo/bar', '/foo/bar' ]; |
| 364 |
yield 'simple route with query' => |
| 365 |
[ '/foo/bar', '/foo/bar?x=1&y=2', [ 'x' => '1', 'y' => '2' ] ]; |
| 366 |
yield 'simple route with strange query chars' => |
| 367 |
[ '/foo+bar', '/foo+bar?x=%23&y=%25&z=%2B', [ 'x' => '#', 'y' => '%', 'z' => '+' ] ]; |
| 368 |
yield 'route with simple path params' => |
| 369 |
[ '/foo/{test}/baz', '/foo/bar/baz', [], [ 'test' => 'bar' ] ]; |
| 370 |
yield 'route with strange path params' => |
| 371 |
[ '/foo/{test}/baz', '/foo/b%25%2F%2Bz/baz', [], [ 'test' => 'b%/+z' ] ]; |
| 372 |
yield 'space in path does not become a plus' => |
| 373 |
[ '/foo/{test}/baz', '/foo/b%20z/baz', [], [ 'test' => 'b z' ] ]; |
| 374 |
yield 'route with simple path params and query' => |
| 375 |
[ '/foo/{test}/baz', '/foo/bar/baz?x=1', [ 'x' => '1' ], [ 'test' => 'bar' ] ]; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function testGetRoutePath( $route, $expectedUrl, $query = [], $path = [] ) { |
| 382 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/route' ) ] ); |
| 383 |
$router = $this->createRouter( $request ); |
| 384 |
|
| 385 |
$path = $router->getRoutePath( $route, $path, $query ); |
| 386 |
$this->assertStringNotContainsString( self::CANONICAL_SERVER, $path ); |
| 387 |
$this->assertStringStartsWith( '/', $path ); |
| 388 |
|
| 389 |
$expected = new Uri( $expectedUrl ); |
| 390 |
$actual = new Uri( $path ); |
| 391 |
$this->assertStringContainsString( $expected->getPath(), $actual->getPath() ); |
| 392 |
$this->assertStringContainsString( $expected->getQuery(), $actual->getQuery() ); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public function testGetRouteUrl( $route, $expectedUrl, $query = [], $path = [] ) { |
| 399 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/route' ) ] ); |
| 400 |
$router = $this->createRouter( $request ); |
| 401 |
|
| 402 |
$url = $router->getRouteUrl( $route, $path, $query ); |
| 403 |
$this->assertStringStartsWith( self::CANONICAL_SERVER, $url ); |
| 404 |
|
| 405 |
$uri = new Uri( $url ); |
| 406 |
$this->assertStringContainsString( $expectedUrl, $uri ); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public function testGetPrivateRouteUrl( $route, $expectedUrl, $query = [], $path = [] ) { |
| 413 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/route' ) ] ); |
| 414 |
$router = $this->createRouter( $request ); |
| 415 |
|
| 416 |
$url = $router->getPrivateRouteUrl( $route, $path, $query ); |
| 417 |
$this->assertStringStartsWith( self::INTERNAL_SERVER, $url ); |
| 418 |
|
| 419 |
$uri = new Uri( $url ); |
| 420 |
$this->assertStringContainsString( $expectedUrl, $uri ); |
| 421 |
} |
| 422 |
|
| 423 |
public function testCaching() { |
| 424 |
$request = new RequestData( [ 'uri' => new Uri( '/rest/mock/v1/route' ) ] ); |
| 425 |
$router1 = $this->createRouter( $request ); |
| 426 |
$router1wrapper = TestingAccessWrapper::newFromObject( $router1 ); |
| 427 |
|
| 428 |
|
| 429 |
$router1->getModule( 'mock' ); |
| 430 |
|
| 431 |
|
| 432 |
$router2 = $this->createRouter( $request ); |
| 433 |
$router2wrapper = TestingAccessWrapper::newFromObject( $router2 ); |
| 434 |
|
| 435 |
|
| 436 |
$router2wrapper->routeFiles = [ '/this/does/not/exist' ]; |
| 437 |
|
| 438 |
|
| 439 |
$router2wrapper->configHash = $router1wrapper->configHash; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
$module2 = $router2->getModule( 'mock/v1' ); |
| 444 |
$this->assertNotNull( $module2 ); |
| 445 |
|
| 446 |
|
| 447 |
$router3 = $this->createRouter( $request ); |
| 448 |
$router3wrapper = TestingAccessWrapper::newFromObject( $router3 ); |
| 449 |
|
| 450 |
|
| 451 |
$router3wrapper->routeFiles = [ __DIR__ . '/testAdditionalRoutes.json' ]; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
$module3 = $router3->getModule( 'mock/v1' ); |
| 456 |
$this->assertNull( $module3 ); |
| 457 |
} |
| 458 |
|
| 459 |
public function testHandlerDisablesBodyParsing() { |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
$payload = '"just a test"'; |
| 464 |
|
| 465 |
$request = new RequestData( [ |
| 466 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/stream' ), |
| 467 |
'method' => 'PUT', |
| 468 |
'bodyContents' => $payload, |
| 469 |
'headers' => [ "content-type" => 'application/json' ] |
| 470 |
] ); |
| 471 |
|
| 472 |
$router = $this->createRouter( $request ); |
| 473 |
$response = $router->execute( $request ); |
| 474 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 475 |
|
| 476 |
$responseStream = $response->getBody(); |
| 477 |
$this->assertSame( $payload, "$responseStream" ); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function testCustomBodyValidator() { |
| 487 |
$this->expectDeprecationAndContinue( '/overrides getBodyValidator/' ); |
| 488 |
$this->expectDeprecationAndContinue( '/Validator::validateBody/' ); |
| 489 |
$this->expectDeprecationAndContinue( '/JsonBodyValidator/' ); |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
$payload = '{ "test": "yes" }'; |
| 495 |
|
| 496 |
$request = new RequestData( [ |
| 497 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/old-body-validator' ), |
| 498 |
'method' => 'PUT', |
| 499 |
'bodyContents' => $payload, |
| 500 |
'headers' => [ "content-type" => 'application/json-patch+json' ] |
| 501 |
] ); |
| 502 |
|
| 503 |
$router = $this->createRouter( $request ); |
| 504 |
$response = $router->execute( $request ); |
| 505 |
$this->assertSame( 200, $response->getStatusCode(), (string)$response->getBody() ); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public static function streamHandlerFactory(): Handler { |
| 512 |
return new class extends Handler { |
| 513 |
public function parseBodyData( RequestInterface $request ): ?array { |
| 514 |
|
| 515 |
return null; |
| 516 |
} |
| 517 |
|
| 518 |
public function execute() { |
| 519 |
Assert::assertNull( $this->getRequest()->getParsedBody() ); |
| 520 |
$body = $this->getRequest()->getBody(); |
| 521 |
$response = $this->getResponseFactory()->create(); |
| 522 |
$response->setBody( new StringStream( "$body" ) ); |
| 523 |
return $response; |
| 524 |
} |
| 525 |
}; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public static function oldBodyValidatorFactory(): Handler { |
| 532 |
return new class extends Handler { |
| 533 |
private $postValidationSetupCalled = false; |
| 534 |
|
| 535 |
public function getBodyValidator( $contentType ) { |
| 536 |
if ( $contentType !== 'application/json-patch+json' ) { |
| 537 |
throw new HttpException( |
| 538 |
"Unsupported Content-Type", |
| 539 |
415, |
| 540 |
); |
| 541 |
} |
| 542 |
|
| 543 |
return new JsonBodyValidator( [ |
| 544 |
'test' => [ |
| 545 |
ParamValidator::PARAM_REQUIRED => true, |
| 546 |
static::PARAM_SOURCE => 'body', |
| 547 |
] |
| 548 |
] ); |
| 549 |
} |
| 550 |
|
| 551 |
public function execute() { |
| 552 |
$body = $this->getValidatedBody(); |
| 553 |
Assert::assertIsArray( $body ); |
| 554 |
Assert::assertArrayHasKey( 'test', $body ); |
| 555 |
Assert::assertTrue( $this->postValidationSetupCalled ); |
| 556 |
return ""; |
| 557 |
} |
| 558 |
|
| 559 |
protected function postValidationSetup() { |
| 560 |
$this->postValidationSetupCalled = true; |
| 561 |
} |
| 562 |
}; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public static function formHandlerFactory(): Handler { |
| 569 |
return new class extends Handler { |
| 570 |
|
| 571 |
public function execute() { |
| 572 |
return $this->getValidatedBody(); |
| 573 |
} |
| 574 |
|
| 575 |
public function getParamSettings(): array { |
| 576 |
return [ |
| 577 |
'foo' => [ |
| 578 |
Handler::PARAM_SOURCE => 'body' |
| 579 |
] |
| 580 |
]; |
| 581 |
} |
| 582 |
|
| 583 |
public function getSupportedRequestTypes(): array { |
| 584 |
return [ |
| 585 |
'application/x-www-form-urlencoded', |
| 586 |
'multipart/form-data' |
| 587 |
]; |
| 588 |
} |
| 589 |
}; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public static function dataHandlerFactory(): Handler { |
| 596 |
return new class extends Handler { |
| 597 |
|
| 598 |
public function execute() { |
| 599 |
return $this->getValidatedBody(); |
| 600 |
} |
| 601 |
|
| 602 |
public function getParamSettings(): array { |
| 603 |
return [ |
| 604 |
'foo' => [ |
| 605 |
Handler::PARAM_SOURCE => 'body' |
| 606 |
] |
| 607 |
]; |
| 608 |
} |
| 609 |
}; |
| 610 |
} |
| 611 |
|
| 612 |
public function testGetRequestFailsWithBody() { |
| 613 |
$this->markTestSkipped( 'T359509' ); |
| 614 |
$request = new RequestData( [ |
| 615 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 616 |
'method' => 'GET', |
| 617 |
'bodyContents' => '{"foo":"bar"}', |
| 618 |
'headers' => [ "content-type" => 'application/json' ] |
| 619 |
] ); |
| 620 |
$router = $this->createRouter( $request ); |
| 621 |
$response = $router->execute( $request ); |
| 622 |
$this->assertSame( 400, $response->getStatusCode() ); |
| 623 |
} |
| 624 |
|
| 625 |
public function testGetRequestIgnoresEmptyBody() { |
| 626 |
$request = new RequestData( [ |
| 627 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 628 |
'method' => 'GET', |
| 629 |
'bodyContents' => '', |
| 630 |
'headers' => [ |
| 631 |
"content-length" => 0, |
| 632 |
"content-type" => 'text/plain' |
| 633 |
] |
| 634 |
] ); |
| 635 |
$router = $this->createRouter( $request ); |
| 636 |
$response = $router->execute( $request ); |
| 637 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 638 |
} |
| 639 |
|
| 640 |
public function testPostRequestFailsWithoutBody() { |
| 641 |
$request = new RequestData( [ |
| 642 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 643 |
'method' => 'POST', |
| 644 |
] ); |
| 645 |
$router = $this->createRouter( $request ); |
| 646 |
$response = $router->execute( $request ); |
| 647 |
$this->assertSame( 411, $response->getStatusCode() ); |
| 648 |
} |
| 649 |
|
| 650 |
public function testEmptyBodyWithoutContentTypePasses() { |
| 651 |
$request = new RequestData( [ |
| 652 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 653 |
'method' => 'POST', |
| 654 |
'headers' => [ 'content-length' => '0' ], |
| 655 |
'bodyContent' => '', |
| 656 |
|
| 657 |
] ); |
| 658 |
|
| 659 |
$router = $this->createRouter( $request ); |
| 660 |
$response = $router->execute( $request ); |
| 661 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 662 |
} |
| 663 |
|
| 664 |
public function testRequestBodyWithoutContentTypeFails() { |
| 665 |
$request = new RequestData( [ |
| 666 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 667 |
'method' => 'POST', |
| 668 |
'bodyContents' => '{"foo":"bar"}', |
| 669 |
] ); |
| 670 |
$router = $this->createRouter( $request ); |
| 671 |
$response = $router->execute( $request ); |
| 672 |
$this->assertSame( 415, $response->getStatusCode() ); |
| 673 |
} |
| 674 |
|
| 675 |
public function testDeleteRequestWithoutBody() { |
| 676 |
|
| 677 |
$requestWithoutBody = new RequestData( [ |
| 678 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 679 |
'method' => 'DELETE', |
| 680 |
] ); |
| 681 |
$router = $this->createRouter( $requestWithoutBody ); |
| 682 |
$responseWithoutBody = $router->execute( $requestWithoutBody ); |
| 683 |
$this->assertSame( 200, $responseWithoutBody->getStatusCode() ); |
| 684 |
} |
| 685 |
|
| 686 |
public function testDeleteRequestWithBody() { |
| 687 |
|
| 688 |
$requestWithBody = new RequestData( [ |
| 689 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 690 |
'method' => 'DELETE', |
| 691 |
'bodyContents' => '{"bodyParam":"bar"}', |
| 692 |
'headers' => [ "content-type" => 'application/json' ] |
| 693 |
] ); |
| 694 |
$router = $this->createRouter( $requestWithBody ); |
| 695 |
$responseWithBody = $router->execute( $requestWithBody ); |
| 696 |
$this->assertSame( 200, $responseWithBody->getStatusCode() ); |
| 697 |
} |
| 698 |
|
| 699 |
public function testUnsupportedContentTypeReturns415() { |
| 700 |
$request = new RequestData( [ |
| 701 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 702 |
'method' => 'POST', |
| 703 |
'bodyContents' => '{"foo":"bar"}', |
| 704 |
'headers' => [ "content-type" => 'text/plain' ] |
| 705 |
] ); |
| 706 |
$router = $this->createRouter( $request ); |
| 707 |
$response = $router->execute( $request ); |
| 708 |
$this->assertSame( 415, $response->getStatusCode() ); |
| 709 |
} |
| 710 |
|
| 711 |
public function testFormDataReturns415() { |
| 712 |
$request = new RequestData( [ |
| 713 |
|
| 714 |
|
| 715 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/data-handler' ), |
| 716 |
'method' => 'POST', |
| 717 |
'postParams' => [ 'foo' => 'bar' ], |
| 718 |
'headers' => [ "content-type" => 'application/x-www-form-urlencoded' ] |
| 719 |
] ); |
| 720 |
$router = $this->createRouter( $request ); |
| 721 |
$response = $router->execute( $request ); |
| 722 |
$this->assertSame( 415, $response->getStatusCode() ); |
| 723 |
} |
| 724 |
|
| 725 |
public function testFormDataSupported() { |
| 726 |
|
| 727 |
$this->expectDeprecationAndContinue( '/The "post" source is deprecated/' ); |
| 728 |
|
| 729 |
$request = new RequestData( [ |
| 730 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo_form_data' ), |
| 731 |
'method' => 'POST', |
| 732 |
'postParams' => [ 'foo' => 'bar' ], |
| 733 |
'headers' => [ "content-type" => 'application/x-www-form-urlencoded' ] |
| 734 |
] ); |
| 735 |
$router = $this->createRouter( $request ); |
| 736 |
$response = $router->execute( $request ); |
| 737 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 738 |
|
| 739 |
|
| 740 |
$body = $response->getBody(); |
| 741 |
$body->rewind(); |
| 742 |
$data = json_decode( $body->getContents(), true ); |
| 743 |
$this->assertSame( [ 'foo' => 'bar' ], $data[ 'parsedBody' ] ); |
| 744 |
} |
| 745 |
|
| 746 |
public function testJsonBody() { |
| 747 |
$request = new RequestData( [ |
| 748 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 749 |
'method' => 'POST', |
| 750 |
'bodyContents' => '{"bodyParam":"bar"}', |
| 751 |
'headers' => [ "content-type" => 'application/json' ] |
| 752 |
] ); |
| 753 |
$router = $this->createRouter( $request ); |
| 754 |
$response = $router->execute( $request ); |
| 755 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 756 |
|
| 757 |
$body = $response->getBody(); |
| 758 |
$body->rewind(); |
| 759 |
$data = json_decode( $body->getContents(), true ); |
| 760 |
|
| 761 |
|
| 762 |
$this->assertEquals( [ 'bodyParam' => 'bar' ], $data['parsedBody'] ); |
| 763 |
$this->assertEquals( [ 'bodyParam' => 'bar' ], $data['validatedBody'] ); |
| 764 |
$this->assertArrayNotHasKey( 'bodyParam', $data['validatedParams'] ); |
| 765 |
} |
| 766 |
|
| 767 |
public function testFormDataBody() { |
| 768 |
$request = new RequestData( [ |
| 769 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo' ), |
| 770 |
'method' => 'POST', |
| 771 |
'postParams' => [ 'bodyParam' => 'bar' ], |
| 772 |
'headers' => [ |
| 773 |
"content-type" => 'application/x-www-form-urlencoded', |
| 774 |
"content-length" => 123, |
| 775 |
] |
| 776 |
] ); |
| 777 |
$router = $this->createRouter( $request ); |
| 778 |
$response = $router->execute( $request ); |
| 779 |
$this->assertSame( 200, $response->getStatusCode(), (string)$response->getBody() ); |
| 780 |
|
| 781 |
$body = $response->getBody(); |
| 782 |
$body->rewind(); |
| 783 |
$data = json_decode( $body->getContents(), true ); |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
$this->assertEquals( [ 'bodyParam' => 'bar' ], $data['parsedBody'] ); |
| 788 |
$this->assertEquals( [ 'bodyParam' => 'bar' ], $data['validatedBody'] ); |
| 789 |
$this->assertArrayNotHasKey( 'bodyParam', $data['validatedParams'] ); |
| 790 |
} |
| 791 |
|
| 792 |
public function testFormDataBody_post() { |
| 793 |
|
| 794 |
$this->expectDeprecationAndContinue( '/The "post" source is deprecated/' ); |
| 795 |
|
| 796 |
$request = new RequestData( [ |
| 797 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo_form_data' ), |
| 798 |
'method' => 'POST', |
| 799 |
'postParams' => [ 'postParam' => 'bar' ], |
| 800 |
'headers' => [ |
| 801 |
"content-type" => 'application/x-www-form-urlencoded', |
| 802 |
"content-length" => 123, |
| 803 |
] |
| 804 |
] ); |
| 805 |
$router = $this->createRouter( $request ); |
| 806 |
$response = $router->execute( $request ); |
| 807 |
$this->assertSame( 200, $response->getStatusCode(), (string)$response->getBody() ); |
| 808 |
|
| 809 |
$body = $response->getBody(); |
| 810 |
$body->rewind(); |
| 811 |
$data = json_decode( $body->getContents(), true ); |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
$this->assertEquals( [ 'postParam' => 'bar' ], $data['parsedBody'] ); |
| 816 |
$this->assertArrayHasKey( 'postParam', $data['validatedParams'] ); |
| 817 |
$this->assertArrayNotHasKey( 'postParam', $data['validatedBody'] ); |
| 818 |
} |
| 819 |
|
| 820 |
public function testHandlerCanAccessValidatedParams() { |
| 821 |
$request = new RequestData( [ |
| 822 |
'uri' => new Uri( '/rest/mock/v1/RouterTest/echo/bar' ), |
| 823 |
'method' => 'POST', |
| 824 |
'headers' => [ "content-type" => 'application/json' ], |
| 825 |
'bodyContents' => '{}' |
| 826 |
] ); |
| 827 |
$router = $this->createRouter( $request ); |
| 828 |
$response = $router->execute( $request ); |
| 829 |
$this->assertSame( 200, $response->getStatusCode(), (string)$response->getBody() ); |
| 830 |
|
| 831 |
|
| 832 |
$body = $response->getBody(); |
| 833 |
$body->rewind(); |
| 834 |
$data = json_decode( $body->getContents(), true ); |
| 835 |
$this->assertArrayHasKey( 'validatedParams', $data ); |
| 836 |
|
| 837 |
|
| 838 |
$validatedParams = $data['validatedParams']; |
| 839 |
$this->assertEquals( 'bar', $validatedParams[ 'pathParam' ], (string)$response->getBody() ); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
private function getCorsUtils(): CorsUtils { |
| 846 |
$cors = new CorsUtils( |
| 847 |
new ServiceOptions( |
| 848 |
CorsUtils::CONSTRUCTOR_OPTIONS, |
| 849 |
[ |
| 850 |
MainConfigNames::AllowedCorsHeaders => [], |
| 851 |
MainConfigNames::AllowCrossOrigin => [], |
| 852 |
MainConfigNames::RestAllowCrossOriginCookieAuth => [], |
| 853 |
MainConfigNames::CanonicalServer => 'testing', |
| 854 |
MainConfigNames::CrossSiteAJAXdomains => [], |
| 855 |
MainConfigNames::CrossSiteAJAXdomainExceptions => [], |
| 856 |
] |
| 857 |
), |
| 858 |
new ResponseFactory( [] ), |
| 859 |
new UserIdentityValue( |
| 860 |
1, |
| 861 |
'Test' |
| 862 |
) |
| 863 |
); |
| 864 |
|
| 865 |
return $cors; |
| 866 |
} |
| 867 |
} |