Skip to content

Commit

Permalink
Fix a few tests in the Response tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 16, 2017
1 parent 52c751c commit 86b8c7d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 114 deletions.
45 changes: 24 additions & 21 deletions src/Http/Response.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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];
}

Expand Down
188 changes: 95 additions & 93 deletions tests/TestCase/Http/ResponseTest.php
Expand Up @@ -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.');
});
}

/**
Expand Down

0 comments on commit 86b8c7d

Please sign in to comment.