0d3e63f98a9a1c197204aa1f88531d2eb83f3913
7 class ApiFormatRawTest
extends ApiFormatTestBase
{
9 protected $printerName = 'raw';
12 * Test basic encoding and missing mime and text exceptions
13 * @return array datasets
15 public static function provideGeneralEncoding() {
17 'class' => 'ApiFormatRaw',
18 'factory' => function ( ApiMain
$main ) {
19 return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
25 [ 'mime' => 'text/plain', 'text' => 'foo' ],
31 [ 'mime' => 'text/plain', 'text' => 'fóo' ],
37 [ 'text' => 'some text' ],
38 new MWException( 'No MIME type set for raw formatter' ),
43 [ 'mime' => 'text/plain' ],
44 new MWException( 'No text given for raw formatter' ),
48 'test error fallback' => [
49 [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
50 '{"mime":"text/plain","text":"some text","error":"some error"}',
58 * Test specifying filename
60 public function testFilename() {
61 $printer = new ApiFormatRaw( new ApiMain
);
62 $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
63 $this->assertSame( 'whatever.raw', $printer->getFilename() );
67 * Test specifying filename with error fallback printer
69 public function testErrorFallbackFilename() {
70 $apiMain = new ApiMain
;
71 $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
72 $printer->getResult()->addValue( null, 'error', 'some error' );
73 $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
74 $this->assertSame( 'api-result.json', $printer->getFilename() );
78 * Test specifying mime
80 public function testMime() {
81 $printer = new ApiFormatRaw( new ApiMain
);
82 $printer->getResult()->addValue( null, 'mime', 'text/plain' );
83 $this->assertSame( 'text/plain', $printer->getMimeType() );
87 * Test specifying mime with error fallback printer
89 public function testErrorFallbackMime() {
90 $apiMain = new ApiMain
;
91 $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
92 $printer->getResult()->addValue( null, 'error', 'some error' );
93 $printer->getResult()->addValue( null, 'mime', 'text/plain' );
94 $this->assertSame( 'application/json', $printer->getMimeType() );
98 * Check that setting failWithHTTPError to true will result in 400 response status code
100 public function testFailWithHTTPError() {
103 $this->testGeneralEncoding(
104 [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
105 '{"mime":"text/plain","text":"some text","error":"some error"}',
108 'class' => 'ApiFormatRaw',
109 'factory' => function ( ApiMain
$main ) use ( &$apiMain ) {
111 $printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
112 $printer->setFailWithHTTPError( true );
117 $this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );