From a53e3155659cd5c88ee65096d1864e997ede9b5b Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 10 Nov 2017 22:36:15 -0500 Subject: [PATCH] Fix deprecated methods in Http\Response --- src/Http/Response.php | 2 +- tests/TestCase/Http/ResponseTest.php | 752 ++++++++++----------------- 2 files changed, 274 insertions(+), 480 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 68ddbeab5dc..68025feb047 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -2341,7 +2341,7 @@ public function getCookieCollection() */ public function cors(ServerRequest $request, $allowedDomains = [], $allowedMethods = [], $allowedHeaders = []) { - $origin = $request->header('Origin'); + $origin = $request->getHeaderLine('Origin'); $ssl = $request->is('ssl'); $builder = new CorsBuilder($this, $origin, $ssl); if (!$origin) { diff --git a/tests/TestCase/Http/ResponseTest.php b/tests/TestCase/Http/ResponseTest.php index 2e3ec8987f3..62eff04a18f 100644 --- a/tests/TestCase/Http/ResponseTest.php +++ b/tests/TestCase/Http/ResponseTest.php @@ -414,11 +414,13 @@ public static function charsetTypeProvider() */ public function testSendChangingContentType($original, $expected) { - $response = new Response(); - $response->type($original); - $response->body('the response body'); + $this->deprecated(function () use ($original, $expected) { + $response = new Response(); + $response->type($original); + $response->body('the response body'); - $this->assertEquals($expected, $response->getHeaderLine('Content-Type')); + $this->assertEquals($expected, $response->getHeaderLine('Content-Type')); + }); } /** @@ -1663,24 +1665,23 @@ public function testGetCookieCollection() */ public function testCors($request, $origin, $domains, $methods, $headers, $expectedOrigin, $expectedMethods = false, $expectedHeaders = false) { - $request->env('HTTP_ORIGIN', $origin); + $request = $request->withEnv('HTTP_ORIGIN', $origin); $response = new Response(); $result = $response->cors($request, $domains, $methods, $headers); $this->assertInstanceOf('Cake\Network\CorsBuilder', $result); - $headers = $response->header(); if ($expectedOrigin) { - $this->assertArrayHasKey('Access-Control-Allow-Origin', $headers); - $this->assertEquals($expectedOrigin, $headers['Access-Control-Allow-Origin']); + $this->assertTrue($response->hasHeader('Access-Control-Allow-Origin')); + $this->assertEquals($expectedOrigin, $response->getHeaderLine('Access-Control-Allow-Origin')); } if ($expectedMethods) { - $this->assertArrayHasKey('Access-Control-Allow-Methods', $headers); - $this->assertEquals($expectedMethods, $headers['Access-Control-Allow-Methods']); + $this->assertTrue($response->hasHeader('Access-Control-Allow-Methods')); + $this->assertEquals($expectedMethods, $response->getHeaderLine('Access-Control-Allow-Methods')); } if ($expectedHeaders) { - $this->assertArrayHasKey('Access-Control-Allow-Headers', $headers); - $this->assertEquals($expectedHeaders, $headers['Access-Control-Allow-Headers']); + $this->assertTrue($response->hasHeader('Access-Control-Allow-Headers')); + $this->assertEquals($expectedHeaders, $response->getHeaderLine('Access-Control-Allow-Headers')); } unset($_SERVER['HTTP_ORIGIN']); } @@ -1739,13 +1740,16 @@ public function corsData() /** * testFileNotFound * + * @group deprecated * @return void */ public function testFileNotFound() { $this->expectException(\Cake\Network\Exception\NotFoundException::class); - $response = new Response(); - $response->file('/some/missing/folder/file.jpg'); + $this->deprecated(function () { + $response = new Response(); + $response->file('/some/missing/folder/file.jpg'); + }); } /** @@ -1778,17 +1782,20 @@ public function invalidFileProvider() /** * test invalid file paths. * + * @group deprecated * @dataProvider invalidFileProvider * @return void */ public function testFileInvalidPath($path, $expectedMessage) { - $response = new Response(); - try { - $response->file($path); - } catch (NotFoundException $e) { - $this->assertContains($expectedMessage, $e->getMessage()); - } + $this->deprecated(function () use ($path, $expectedMessage) { + $response = new Response(); + try { + $response->file($path); + } catch (NotFoundException $e) { + $this->assertContains($expectedMessage, $e->getMessage()); + } + }); } /** @@ -1810,43 +1817,33 @@ public function testWithFileInvalidPath($path, $expectedMessage) /** * testFile method * + * @group deprecated * @return void */ public function testFile() { - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->exactly(1)) - ->method('type') - ->with('css') - ->will($this->returnArgument(0)); - - $response->expects($this->at(1)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $this->deprecated(function () { + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods(['_sendHeader', '_isActive']) + ->getMock(); - $response->expects($this->exactly(1)) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->expects($this->exactly(1)) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->file(TEST_APP . 'vendor/css/test_asset.css'); + $response->file(TEST_APP . 'vendor/css/test_asset.css'); - ob_start(); - $result = $response->send(); - $this->assertTrue($result !== false); - $output = ob_get_clean(); + ob_start(); + $result = $response->send(); + $this->assertTrue($result !== false); + $output = ob_get_clean(); - $expected = '/* this is the test asset css file */'; - $this->assertEquals($expected, trim($output)); - $this->assertEquals($expected, trim($response->getBody()->getContents())); + $expected = '/* this is the test asset css file */'; + $this->assertEquals($expected, trim($output)); + $this->assertEquals($expected, trim($response->getBody()->getContents())); + $this->assertEquals('text/css', $response->getType()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + }); } /** @@ -1890,109 +1887,77 @@ public function testWithFileDownloadAndName() /** * testFileWithDownloadAndName * + * @group deprecated * @return void */ public function testFileWithDownloadAndName() { - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->exactly(1)) - ->method('type') - ->with('css') - ->will($this->returnArgument(0)); - - $response->expects($this->once()) - ->method('download') - ->with('something_special.css'); - - $response->expects($this->at(2)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); + $this->deprecated(function () { + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods(['download', '_sendHeader', '_isActive']) + ->getMock(); - $response->expects($this->at(3)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $response->expects($this->once()) + ->method('download') + ->with('something_special.css'); - $response->expects($this->exactly(1)) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->expects($this->exactly(1)) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - [ - 'name' => 'something_special.css', - 'download' => true, - ] - ); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + [ + 'name' => 'something_special.css', + 'download' => true, + ] + ); - ob_start(); - $result = $response->send(); - $output = ob_get_clean(); - $this->assertEquals("/* this is the test asset css file */\n", $output); - $this->assertNotSame(false, $result); + ob_start(); + $result = $response->send(); + $output = ob_get_clean(); + $this->assertEquals("/* this is the test asset css file */\n", $output); + $this->assertNotSame(false, $result); + $this->assertEquals('text/css', $response->getType()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + }); } /** * testFileWithUnknownFileTypeGeneric method * + * @group deprecated * @return void */ public function testFileWithUnknownFileTypeGeneric() { - $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; - $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser'; - - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->exactly(1)) - ->method('type') - ->with('ini') - ->will($this->returnValue(false)); - - $response->expects($this->once()) - ->method('download') - ->with('no_section.ini'); + $this->deprecated(function () { + $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser'; - $response->expects($this->at(2)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods(['download', '_sendHeader', '_isActive']) + ->getMock(); - $response->expects($this->at(3)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $response->expects($this->once()) + ->method('download') + ->with('no_section.ini'); - $response->expects($this->exactly(1)) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->expects($this->exactly(1)) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->file(CONFIG . 'no_section.ini'); + $response->file(CONFIG . 'no_section.ini'); - ob_start(); - $result = $response->send(); - $output = ob_get_clean(); - $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output); - $this->assertNotSame(false, $result); - if ($currentUserAgent !== null) { - $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent; - } + ob_start(); + $result = $response->send(); + $output = ob_get_clean(); + $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output); + $this->assertNotSame(false, $result); + $this->assertEquals('text/html', $response->getType()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + }); } /** @@ -2018,60 +1983,37 @@ public function testWithFileUnknownFileTypeGeneric() /** * testFileWithUnknownFileTypeOpera method * + * @group deprecated * @return void */ public function testFileWithUnknownFileTypeOpera() { - $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; - $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10'; - - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->at(0)) - ->method('type') - ->with('ini') - ->will($this->returnValue(false)); - - $response->expects($this->at(1)) - ->method('type') - ->with('application/octet-stream') - ->will($this->returnValue(false)); - - $response->expects($this->once()) - ->method('download') - ->with('no_section.ini'); + $this->deprecated(function () { + $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10'; - $response->expects($this->at(3)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods(['download', '_sendHeader', '_isActive']) + ->getMock(); - $response->expects($this->at(4)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $response->expects($this->once()) + ->method('download') + ->with('no_section.ini'); - $response->expects($this->exactly(1)) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->expects($this->exactly(1)) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->file(CONFIG . 'no_section.ini'); + $response->file(CONFIG . 'no_section.ini'); - ob_start(); - $result = $response->send(); - $output = ob_get_clean(); - $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output); - $this->assertNotSame(false, $result); - if ($currentUserAgent !== null) { - $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent; - } + ob_start(); + $result = $response->send(); + $output = ob_get_clean(); + $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output); + $this->assertNotSame(false, $result); + $this->assertEquals('application/octet-stream', $response->getType()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + }); } /** @@ -2081,7 +2023,6 @@ public function testFileWithUnknownFileTypeOpera() */ public function testWithFileUnknownFileTypeOpera() { - $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10'; $response = new Response(); @@ -2091,69 +2032,44 @@ public function testWithFileUnknownFileTypeOpera() 'attachment; filename="no_section.ini"', $new->getHeaderLine('Content-Disposition') ); - - $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent; } /** * testFileWithUnknownFileTypeIE method * + * @group deprecated * @return void */ public function testFileWithUnknownFileTypeIE() { - $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)'; - - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->at(0)) - ->method('type') - ->with('ini') - ->will($this->returnValue(false)); - - $response->expects($this->at(1)) - ->method('type') - ->with('application/force-download') - ->will($this->returnValue(false)); - - $response->expects($this->once()) - ->method('download') - ->with('config.ini'); + $this->deprecated(function () { + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)'; - $response->expects($this->at(3)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods(['download', '_isActive', '_sendHeade']) + ->getMock(); - $response->expects($this->at(4)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $response->expects($this->once()) + ->method('download') + ->with('config.ini'); - $response->expects($this->exactly(1)) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->expects($this->exactly(1)) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->file(CONFIG . 'no_section.ini', [ - 'name' => 'config.ini' - ]); + $response->file(CONFIG . 'no_section.ini', [ + 'name' => 'config.ini' + ]); - ob_start(); - $result = $response->send(); - $output = ob_get_clean(); - $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output); - $this->assertNotSame(false, $result); - if ($currentUserAgent !== null) { - $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent; - } + ob_start(); + $result = $response->send(); + $output = ob_get_clean(); + $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output); + $this->assertNotSame(false, $result); + $this->assertEquals('application/force-download', $response->getType()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + }); } /** @@ -2163,56 +2079,37 @@ public function testFileWithUnknownFileTypeIE() */ public function testWithFileUnknownFileTypeOldIe() { - $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)'; $response = new Response(); $new = $response->withFile(CONFIG . 'no_section.ini'); $this->assertEquals('application/force-download', $new->getHeaderLine('Content-Type')); - - $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent; } /** * testFileWithUnknownFileNoDownload method * + * @group deprecated * @return void */ public function testFileWithUnknownFileNoDownload() { - $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; - $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser'; - - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->exactly(1)) - ->method('type') - ->with('ini') - ->will($this->returnValue(false)); - - $response->expects($this->at(1)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $this->deprecated(function () { + $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser'; - $response->expects($this->never()) - ->method('download'); + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods(['download']) + ->getMock(); - $response->file(CONFIG . 'no_section.ini', [ - 'download' => false - ]); + $response->expects($this->never()) + ->method('download'); - if ($currentUserAgent !== null) { - $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent; - } + $response->file(CONFIG . 'no_section.ini', [ + 'download' => false + ]); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('text/html', $response->getType()); + }); } /** @@ -2286,31 +2183,16 @@ public function testConnectionAbortedOnBuffering() /** * Test downloading files with UPPERCASE extensions. * + * @group deprecated * @return void */ public function testFileUpperExtension() { - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->any()) - ->method('type') - ->with('jpg') - ->will($this->returnArgument(0)); - - $response->expects($this->at(0)) - ->method('_isActive') - ->will($this->returnValue(true)); - - $response->file(TEST_APP . 'vendor/img/test_2.JPG'); + $this->deprecated(function () { + $response = new Response(); + $response->file(TEST_APP . 'vendor/img/test_2.JPG'); + $this->assertSame('image/jpeg', $response->getType()); + }); } /** @@ -2325,36 +2207,6 @@ public function testWithFileUpperExtension() $this->assertEquals('image/jpeg', $new->getHeaderLine('Content-Type')); } - /** - * Test downloading files with extension not explicitly set. - * - * @return void - */ - public function testFileExtensionNotSet() - { - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - 'download', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->any()) - ->method('type') - ->with('jpg') - ->will($this->returnArgument(0)); - - $response->expects($this->at(0)) - ->method('_isActive') - ->will($this->returnValue(true)); - - $response->file(TEST_APP . 'vendor/img/test_2.JPG'); - } - /** * A data provider for testing various ranges * @@ -2390,53 +2242,42 @@ public static function rangeProvider() /** * Test the various range offset types. * + * @group deprecated * @dataProvider rangeProvider * @return void */ public function testFileRangeOffsets($range, $length, $offsetResponse) { - $_SERVER['HTTP_RANGE'] = $range; - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->at(1)) - ->method('header') - ->with('Content-Disposition', 'attachment; filename="test_asset.css"'); - - $response->expects($this->at(2)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); - - $response->expects($this->at(3)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); - - $response->expects($this->at(4)) - ->method('header') - ->with([ - 'Content-Length' => $length, - 'Content-Range' => $offsetResponse, - ]); + $this->deprecated(function () use ($range, $length, $offsetResponse) { + $_SERVER['HTTP_RANGE'] = $range; + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods([ + '_sendHeader', + '_isActive', + ]) + ->getMock(); - $response->expects($this->any()) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->expects($this->any()) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - ['download' => true] - ); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => true] + ); - ob_start(); - $result = $response->send(); - ob_get_clean(); + ob_start(); + $result = $response->send(); + ob_get_clean(); + $this->assertEquals( + 'attachment; filename="test_asset.css"', + $response->getHeaderLine('Content-Disposition') + ); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals($length, $response->getHeaderLine('Content-Length')); + $this->assertEquals($offsetResponse, $response->getHeaderLine('Content-Range')); + }); } /** @@ -2466,60 +2307,45 @@ public function testWithFileRangeOffsets($range, $length, $offsetResponse) /** * Test fetching ranges from a file. * + * @group deprecated * @return void */ public function testFileRange() { - $_SERVER['HTTP_RANGE'] = 'bytes=8-25'; - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - '_sendHeader', - '_setContentType', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->exactly(1)) - ->method('type') - ->with('css') - ->will($this->returnArgument(0)); - - $response->expects($this->at(1)) - ->method('header') - ->with('Content-Disposition', 'attachment; filename="test_asset.css"'); - - $response->expects($this->at(2)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); - - $response->expects($this->at(3)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); + $this->deprecated(function () { + $_SERVER['HTTP_RANGE'] = 'bytes=8-25'; + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods([ + '_sendHeader', + '_isActive', + ]) + ->getMock(); - $response->expects($this->at(4)) - ->method('header') - ->with([ - 'Content-Length' => 18, - 'Content-Range' => 'bytes 8-25/38', - ]); + $response->expects($this->any()) + ->method('_isActive') + ->will($this->returnValue(true)); - $response->expects($this->any()) - ->method('_isActive') - ->will($this->returnValue(true)); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => true] + ); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - ['download' => true] - ); + ob_start(); + $result = $response->send(); + $output = ob_get_clean(); + $this->assertEquals('is the test asset ', $output); + $this->assertNotSame(false, $result); - ob_start(); - $result = $response->send(); - $output = ob_get_clean(); - $this->assertEquals(206, $response->statusCode()); - $this->assertEquals('is the test asset ', $output); - $this->assertNotSame(false, $result); + $this->assertEquals( + 'attachment; filename="test_asset.css"', + $response->getHeaderLine('Content-Disposition') + ); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('18', $response->getHeaderLine('Content-Length')); + $this->assertEquals('bytes 8-25/38', $response->getHeaderLine('Content-Range')); + $this->assertEquals(206, $response->getStatusCode()); + }); } /** @@ -2570,33 +2396,36 @@ public function invalidFileRangeProvider() /** * Test invalid file ranges. * + * @group deprecated * @dataProvider invalidFileRangeProvider * @return void */ public function testFileRangeInvalid($range) { - $_SERVER['HTTP_RANGE'] = $range; - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - '_sendHeader', - '_isActive', - ]) - ->getMock(); + $this->deprecated(function () use ($range) { + $_SERVER['HTTP_RANGE'] = $range; + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods([ + '_sendHeader', + '_isActive', + ]) + ->getMock(); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - ['download' => true] - ); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => true] + ); - $expected = [ - 'Content-Type' => 'text/css; charset=UTF-8', - 'Content-Disposition' => 'attachment; filename="test_asset.css"', - 'Content-Transfer-Encoding' => 'binary', - 'Accept-Ranges' => 'bytes', - 'Content-Range' => 'bytes 0-37/38', - 'Content-Length' => 38, - ]; - $this->assertEquals($expected, $response->header()); + $this->assertEquals('text/css', $response->getType()); + $this->assertEquals( + 'attachment; filename="test_asset.css"', + $response->getHeaderLine('Content-Disposition') + ); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('38', $response->getHeaderLine('Content-Length')); + $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range')); + }); } /** @@ -2628,45 +2457,34 @@ public function testWithFileInvalidRange($range) /** * Test reversed file ranges. * + * @group deprecated * @return void */ public function testFileRangeReversed() { - $_SERVER['HTTP_RANGE'] = 'bytes=30-2'; - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - '_sendHeader', - '_isActive', - ]) - ->getMock(); - - $response->expects($this->at(1)) - ->method('header') - ->with('Content-Disposition', 'attachment; filename="test_asset.css"'); - - $response->expects($this->at(2)) - ->method('header') - ->with('Content-Transfer-Encoding', 'binary'); - - $response->expects($this->at(3)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); - - $response->expects($this->at(4)) - ->method('header') - ->with([ - 'Content-Range' => 'bytes 0-37/38', - ]); + $this->deprecated(function () { + $_SERVER['HTTP_RANGE'] = 'bytes=30-2'; + $response = $this->getMockBuilder('Cake\Http\Response') + ->setMethods([ + '_sendHeader', + '_isActive', + ]) + ->getMock(); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - ['download' => true] - ); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => true] + ); - $this->assertEquals(416, $response->statusCode()); - $result = $response->send(); + $this->assertEquals(416, $response->getStatusCode()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals( + 'attachment; filename="test_asset.css"', + $response->getHeaderLine('Content-Disposition') + ); + $this->assertEquals('binary', $response->getHeaderLine('Content-Transfer-Encoding')); + $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range')); + }); } /** @@ -2706,24 +2524,11 @@ public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse) $_SERVER['HTTP_RANGE'] = $range; $response = $this->getMockBuilder('Cake\Http\Response') ->setMethods([ - 'header', - 'type', '_sendHeader', '_isActive', ]) ->getMock(); - $response->expects($this->at(1)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); - - $response->expects($this->at(2)) - ->method('header') - ->with([ - 'Content-Length' => $length, - 'Content-Range' => $offsetResponse, - ]); - $response->expects($this->any()) ->method('_isActive') ->will($this->returnValue(true)); @@ -2736,6 +2541,9 @@ public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse) ob_start(); $result = $response->send(); ob_get_clean(); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals($length, $response->getHeaderLine('Content-Length')); + $this->assertEquals($offsetResponse, $response->getHeaderLine('Content-Range')); }); } @@ -2751,29 +2559,11 @@ public function testFileRangeNoDownload() $_SERVER['HTTP_RANGE'] = 'bytes=8-25'; $response = $this->getMockBuilder('Cake\Http\Response') ->setMethods([ - 'header', - 'type', '_sendHeader', '_isActive', ]) ->getMock(); - $response->expects($this->exactly(1)) - ->method('type') - ->with('css') - ->will($this->returnArgument(0)); - - $response->expects($this->at(1)) - ->method('header') - ->with('Accept-Ranges', 'bytes'); - - $response->expects($this->at(2)) - ->method('header') - ->with([ - 'Content-Length' => 18, - 'Content-Range' => 'bytes 8-25/38', - ]); - $response->expects($this->any()) ->method('_isActive') ->will($this->returnValue(true)); @@ -2786,7 +2576,11 @@ public function testFileRangeNoDownload() ob_start(); $result = $response->send(); $output = ob_get_clean(); - $this->assertEquals(206, $response->statusCode()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('18', $response->getHeaderLine('Content-Length')); + $this->assertEquals('bytes 8-25/38', $response->getHeaderLine('Content-Range')); + $this->assertEquals('text/css', $response->getType()); + $this->assertEquals(206, $response->getStatusCode()); $this->assertEquals('is the test asset ', $output); $this->assertNotSame(false, $result); });