3 namespace MediaWiki\Tests\Api\Format
;
5 use MediaWiki\Api\ApiFormatJson
;
6 use MediaWiki\Api\ApiFormatRaw
;
7 use MediaWiki\Api\ApiMain
;
12 * @covers \MediaWiki\Api\ApiFormatRaw
14 class ApiFormatRawTest
extends ApiFormatTestBase
{
17 protected $printerName = 'raw';
20 * Test basic encoding and missing mime and text exceptions
21 * @return array datasets
23 public static function provideGeneralEncoding() {
25 'class' => ApiFormatRaw
::class,
26 'factory' => static function ( ApiMain
$main ) {
27 return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
33 [ 'mime' => 'text/plain', 'text' => 'foo' ],
39 [ 'mime' => 'text/plain', 'text' => 'fóo' ],
45 [ 'text' => 'some text' ],
46 new MWException( 'No MIME type set for raw formatter' ),
51 [ 'mime' => 'text/plain' ],
52 new MWException( 'No text given for raw formatter' ),
56 'test error fallback' => [
57 [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
58 '{"mime":"text/plain","text":"some text","error":"some error"}',
66 * Test specifying filename
68 public function testFilename() {
69 $printer = new ApiFormatRaw( new ApiMain
);
70 $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
71 $this->assertSame( 'whatever.raw', $printer->getFilename() );
75 * Test specifying filename with error fallback printer
77 public function testErrorFallbackFilename() {
78 $apiMain = new ApiMain
;
79 $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
80 $printer->getResult()->addValue( null, 'error', 'some error' );
81 $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
82 $this->assertSame( 'api-result.json', $printer->getFilename() );
86 * Test specifying mime
88 public function testMime() {
89 $printer = new ApiFormatRaw( new ApiMain
);
90 $printer->getResult()->addValue( null, 'mime', 'text/plain' );
91 $this->assertSame( 'text/plain', $printer->getMimeType() );
95 * Test specifying mime with error fallback printer
97 public function testErrorFallbackMime() {
98 $apiMain = new ApiMain
;
99 $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
100 $printer->getResult()->addValue( null, 'error', 'some error' );
101 $printer->getResult()->addValue( null, 'mime', 'text/plain' );
102 $this->assertSame( 'application/json', $printer->getMimeType() );
106 * Check that setting failWithHTTPError to true will result in 400 response status code
108 public function testFailWithHTTPError() {
111 $this->testGeneralEncoding(
112 [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
113 '{"mime":"text/plain","text":"some text","error":"some error"}',
116 'class' => ApiFormatRaw
::class,
117 'factory' => static function ( ApiMain
$main ) use ( &$apiMain ) {
119 $printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
120 $printer->setFailWithHTTPError( true );
125 $this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );