X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2Fformat%2FApiFormatBaseTest.php;h=55f760f64dfd26eb57342cb6f3a7b6832144b30c;hp=d6a13904eb027b099b429d718ca7a5a4a24d5665;hb=7bc541a4a70899475b88daaf2f26db1e1fc5e18a;hpb=5db74eb56e425ba803b2cc936fbe3e5cc219b482 diff --git a/tests/phpunit/includes/api/format/ApiFormatBaseTest.php b/tests/phpunit/includes/api/format/ApiFormatBaseTest.php index d6a13904eb..55f760f64d 100644 --- a/tests/phpunit/includes/api/format/ApiFormatBaseTest.php +++ b/tests/phpunit/includes/api/format/ApiFormatBaseTest.php @@ -82,7 +82,7 @@ class ApiFormatBaseTest extends ApiFormatTestBase { $this->assertSame( "$ct; charset=utf-8", strtolower( $response->getHeader( 'Content-Type' ) ) ); $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) ); $this->assertSame( $file, $printer->getFilename() ); - $this->assertSame( "inline; filename=\"$file\"", $response->getHeader( 'Content-Disposition' ) ); + $this->assertSame( "inline; filename=$file", $response->getHeader( 'Content-Disposition' ) ); $this->assertSame( $status, $response->getStatusCode() ); return $text; @@ -144,6 +144,49 @@ class ApiFormatBaseTest extends ApiFormatTestBase { ]; } + /** + * @dataProvider provideFilenameEncoding + */ + public function testFilenameEncoding( $filename, $expect ) { + $ret = parent::encodeData( [], [], [ + 'name' => 'mock', + 'class' => ApiFormatBase::class, + 'factory' => function ( ApiMain $main, $format ) use ( $filename ) { + $mock = $this->getMockFormatter( $main, $format, [ 'getFilename' ] ); + $mock->method( 'getFilename' )->willReturn( $filename ); + return $mock; + }, + 'returnPrinter' => true, + ] ); + $response = $ret['printer']->getMain()->getRequest()->response(); + + $this->assertSame( "inline; $expect", $response->getHeader( 'Content-Disposition' ) ); + } + + public static function provideFilenameEncoding() { + return [ + 'something simple' => [ + 'foo.xyz', 'filename=foo.xyz' + ], + 'more complicated, but still simple' => [ + 'foo.!#$%&\'*+-^_`|~', 'filename=foo.!#$%&\'*+-^_`|~' + ], + 'Needs quoting' => [ + 'foo\\bar.xyz', 'filename="foo\\\\bar.xyz"' + ], + 'Needs quoting (2)' => [ + 'foo (bar).xyz', 'filename="foo (bar).xyz"' + ], + 'Needs quoting (3)' => [ + "foo\t\"b\x5car\"\0.xyz", "filename=\"foo\x5c\t\x5c\"b\x5c\x5car\x5c\"\x5c\0.xyz\"" + ], + 'Non-ASCII characters' => [ + 'fóo bár.🙌!', + "filename=\"f\xF3o b\xE1r.?!\"; filename*=UTF-8''f%C3%B3o%20b%C3%A1r.%F0%9F%99%8C!" + ] + ]; + } + public function testBasics() { $printer = $this->getMockFormatter( null, 'mock' ); $this->assertTrue( $printer->canPrintErrors() ); @@ -220,7 +263,7 @@ class ApiFormatBaseTest extends ApiFormatTestBase { ); $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) ); $this->assertSame( - 'inline; filename="api-result.html"', $response->getHeader( 'Content-Disposition' ) + 'inline; filename=api-result.html', $response->getHeader( 'Content-Disposition' ) ); }