From 86b8c7dbce8b6f8542ad9a2ecc87c9d71135a003 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 10 Nov 2017 22:36:15 -0500 Subject: [PATCH] Fix a few tests in the Response tests. --- src/Http/Response.php | 45 ++++--- tests/TestCase/Http/ResponseTest.php | 188 ++++++++++++++------------- 2 files changed, 119 insertions(+), 114 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 2a5de0afb90..68ddbeab5dc 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -919,11 +919,7 @@ public function statusCode($code = null) if (!isset($this->_statusCodes[$code])) { throw new InvalidArgumentException('Unknown status code'); } - if (isset($this->_statusCodes[$code])) { - $this->_reasonPhrase = $this->_statusCodes[$code]; - } - $this->_status = $code; - $this->_setContentType(); + $this->_setStatus($code); return $code; } @@ -967,16 +963,28 @@ public function getStatusCode() public function withStatus($code, $reasonPhrase = '') { $new = clone $this; - $new->_status = $code; - if (empty($reasonPhrase) && isset($new->_statusCodes[$code])) { - $reasonPhrase = $new->_statusCodes[$code]; - } - $new->_reasonPhrase = $reasonPhrase; - $new->_setContentType(); + $new->_setStatus($code, $reasonPhrase); return $new; } + /** + * Modifier for response status + * + * @param int $code The code to set. + * @param string $reasonPhrase The response reason phrase. + * @return void + */ + protected function _setStatus($code, $reasonPhrase = '') + { + $this->_status = $code; + if (empty($reasonPhrase) && isset($this->_statusCodes[$code])) { + $reasonPhrase = $this->_statusCodes[$code]; + } + $this->_reasonPhrase = $reasonPhrase; + $this->_setContentType(); + } + /** * Gets the response reason phrase associated with the status code. * @@ -2578,20 +2586,15 @@ protected function _fileRange($file, $httpRange) } if ($start > $end || $end > $lastByte || $start > $lastByte) { - $this->statusCode(416); - $this->header([ - 'Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize - ]); + $this->_setStatus(416); + $this->_setHeader('Content-Range', 'bytes 0-' . $lastByte . '/' . $fileSize); return; } - $this->header([ - 'Content-Length' => $end - $start + 1, - 'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize - ]); - - $this->statusCode(206); + $this->_setHeader('Content-Length', $end - $start + 1); + $this->_setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $fileSize); + $this->_setStatus(206); $this->_fileRange = [$start, $end]; } diff --git a/tests/TestCase/Http/ResponseTest.php b/tests/TestCase/Http/ResponseTest.php index 6c11e63c84f..2e3ec8987f3 100644 --- a/tests/TestCase/Http/ResponseTest.php +++ b/tests/TestCase/Http/ResponseTest.php @@ -2696,143 +2696,145 @@ public function testWithFileReversedRange() /** * testFileRangeOffsetsNoDownload method * + * @group deprecated * @dataProvider rangeProvider * @return void */ public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse) { - $_SERVER['HTTP_RANGE'] = $range; - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - '_sendHeader', - '_isActive', - ]) - ->getMock(); + $this->deprecated(function () use ($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(1)) + ->method('header') + ->with('Accept-Ranges', 'bytes'); - $response->expects($this->at(2)) - ->method('header') - ->with([ - 'Content-Length' => $length, - 'Content-Range' => $offsetResponse, - ]); + $response->expects($this->at(2)) + ->method('header') + ->with([ + 'Content-Length' => $length, + 'Content-Range' => $offsetResponse, + ]); - $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' => false] - ); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => false] + ); - ob_start(); - $result = $response->send(); - ob_get_clean(); + ob_start(); + $result = $response->send(); + ob_get_clean(); + }); } /** * testFileRangeNoDownload method * + * @group deprecated * @return void */ public function testFileRangeNoDownload() { - $_SERVER['HTTP_RANGE'] = 'bytes=8-25'; - $response = $this->getMockBuilder('Cake\Http\Response') - ->setMethods([ - 'header', - 'type', - '_sendHeader', - '_isActive', - ]) - ->getMock(); + $this->deprecated(function () { + $_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->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(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)); + $response->expects($this->at(2)) + ->method('header') + ->with([ + 'Content-Length' => 18, + 'Content-Range' => 'bytes 8-25/38', + ]); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - ['download' => false] - ); + $response->expects($this->any()) + ->method('_isActive') + ->will($this->returnValue(true)); - 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); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => false] + ); + + 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); + }); } /** * testFileRangeInvalidNoDownload method * + * @group deprecated * @return void */ public function testFileRangeInvalidNoDownload() { - $_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('Accept-Ranges', 'bytes'); - - $response->expects($this->at(2)) - ->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', + ]) + ->getMock(); - $response->file( - TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', - ['download' => false] - ); + $response->file( + TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', + ['download' => false] + ); - $this->assertEquals(416, $response->statusCode()); - $result = $response->send(); + $this->assertEquals(416, $response->statusCode()); + $this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges')); + $this->assertEquals('text/css', $response->getType()); + $this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range')); + $result = $response->send(); + }); } /** * Test the location method. * + * @group deprecated * @return void */ public function testLocation() { - $response = new Response(); - $this->assertNull($response->location(), 'No header should be set.'); - $this->assertNull($response->location('http://example.org'), 'Setting a location should return null'); - $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.'); + $this->deprecated(function () { + $response = new Response(); + $this->assertNull($response->location(), 'No header should be set.'); + $this->assertNull($response->location('http://example.org'), 'Setting a location should return null'); + $this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.'); + }); } /**