Skip to content

Commit 86b8c7d

Browse files
committed
Fix a few tests in the Response tests.
1 parent 52c751c commit 86b8c7d

File tree

2 files changed

+119
-114
lines changed

2 files changed

+119
-114
lines changed

src/Http/Response.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,7 @@ public function statusCode($code = null)
919919
if (!isset($this->_statusCodes[$code])) {
920920
throw new InvalidArgumentException('Unknown status code');
921921
}
922-
if (isset($this->_statusCodes[$code])) {
923-
$this->_reasonPhrase = $this->_statusCodes[$code];
924-
}
925-
$this->_status = $code;
926-
$this->_setContentType();
922+
$this->_setStatus($code);
927923

928924
return $code;
929925
}
@@ -967,16 +963,28 @@ public function getStatusCode()
967963
public function withStatus($code, $reasonPhrase = '')
968964
{
969965
$new = clone $this;
970-
$new->_status = $code;
971-
if (empty($reasonPhrase) && isset($new->_statusCodes[$code])) {
972-
$reasonPhrase = $new->_statusCodes[$code];
973-
}
974-
$new->_reasonPhrase = $reasonPhrase;
975-
$new->_setContentType();
966+
$new->_setStatus($code, $reasonPhrase);
976967

977968
return $new;
978969
}
979970

971+
/**
972+
* Modifier for response status
973+
*
974+
* @param int $code The code to set.
975+
* @param string $reasonPhrase The response reason phrase.
976+
* @return void
977+
*/
978+
protected function _setStatus($code, $reasonPhrase = '')
979+
{
980+
$this->_status = $code;
981+
if (empty($reasonPhrase) && isset($this->_statusCodes[$code])) {
982+
$reasonPhrase = $this->_statusCodes[$code];
983+
}
984+
$this->_reasonPhrase = $reasonPhrase;
985+
$this->_setContentType();
986+
}
987+
980988
/**
981989
* Gets the response reason phrase associated with the status code.
982990
*
@@ -2578,20 +2586,15 @@ protected function _fileRange($file, $httpRange)
25782586
}
25792587

25802588
if ($start > $end || $end > $lastByte || $start > $lastByte) {
2581-
$this->statusCode(416);
2582-
$this->header([
2583-
'Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize
2584-
]);
2589+
$this->_setStatus(416);
2590+
$this->_setHeader('Content-Range', 'bytes 0-' . $lastByte . '/' . $fileSize);
25852591

25862592
return;
25872593
}
25882594

2589-
$this->header([
2590-
'Content-Length' => $end - $start + 1,
2591-
'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize
2592-
]);
2593-
2594-
$this->statusCode(206);
2595+
$this->_setHeader('Content-Length', $end - $start + 1);
2596+
$this->_setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $fileSize);
2597+
$this->_setStatus(206);
25952598
$this->_fileRange = [$start, $end];
25962599
}
25972600

tests/TestCase/Http/ResponseTest.php

Lines changed: 95 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,143 +2696,145 @@ public function testWithFileReversedRange()
26962696
/**
26972697
* testFileRangeOffsetsNoDownload method
26982698
*
2699+
* @group deprecated
26992700
* @dataProvider rangeProvider
27002701
* @return void
27012702
*/
27022703
public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse)
27032704
{
2704-
$_SERVER['HTTP_RANGE'] = $range;
2705-
$response = $this->getMockBuilder('Cake\Http\Response')
2706-
->setMethods([
2707-
'header',
2708-
'type',
2709-
'_sendHeader',
2710-
'_isActive',
2711-
])
2712-
->getMock();
2705+
$this->deprecated(function () use ($range, $length, $offsetResponse) {
2706+
$_SERVER['HTTP_RANGE'] = $range;
2707+
$response = $this->getMockBuilder('Cake\Http\Response')
2708+
->setMethods([
2709+
'header',
2710+
'type',
2711+
'_sendHeader',
2712+
'_isActive',
2713+
])
2714+
->getMock();
27132715

2714-
$response->expects($this->at(1))
2715-
->method('header')
2716-
->with('Accept-Ranges', 'bytes');
2716+
$response->expects($this->at(1))
2717+
->method('header')
2718+
->with('Accept-Ranges', 'bytes');
27172719

2718-
$response->expects($this->at(2))
2719-
->method('header')
2720-
->with([
2721-
'Content-Length' => $length,
2722-
'Content-Range' => $offsetResponse,
2723-
]);
2720+
$response->expects($this->at(2))
2721+
->method('header')
2722+
->with([
2723+
'Content-Length' => $length,
2724+
'Content-Range' => $offsetResponse,
2725+
]);
27242726

2725-
$response->expects($this->any())
2726-
->method('_isActive')
2727-
->will($this->returnValue(true));
2727+
$response->expects($this->any())
2728+
->method('_isActive')
2729+
->will($this->returnValue(true));
27282730

2729-
$response->file(
2730-
TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
2731-
['download' => false]
2732-
);
2731+
$response->file(
2732+
TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
2733+
['download' => false]
2734+
);
27332735

2734-
ob_start();
2735-
$result = $response->send();
2736-
ob_get_clean();
2736+
ob_start();
2737+
$result = $response->send();
2738+
ob_get_clean();
2739+
});
27372740
}
27382741

27392742
/**
27402743
* testFileRangeNoDownload method
27412744
*
2745+
* @group deprecated
27422746
* @return void
27432747
*/
27442748
public function testFileRangeNoDownload()
27452749
{
2746-
$_SERVER['HTTP_RANGE'] = 'bytes=8-25';
2747-
$response = $this->getMockBuilder('Cake\Http\Response')
2748-
->setMethods([
2749-
'header',
2750-
'type',
2751-
'_sendHeader',
2752-
'_isActive',
2753-
])
2754-
->getMock();
2750+
$this->deprecated(function () {
2751+
$_SERVER['HTTP_RANGE'] = 'bytes=8-25';
2752+
$response = $this->getMockBuilder('Cake\Http\Response')
2753+
->setMethods([
2754+
'header',
2755+
'type',
2756+
'_sendHeader',
2757+
'_isActive',
2758+
])
2759+
->getMock();
27552760

2756-
$response->expects($this->exactly(1))
2757-
->method('type')
2758-
->with('css')
2759-
->will($this->returnArgument(0));
2761+
$response->expects($this->exactly(1))
2762+
->method('type')
2763+
->with('css')
2764+
->will($this->returnArgument(0));
27602765

2761-
$response->expects($this->at(1))
2762-
->method('header')
2763-
->with('Accept-Ranges', 'bytes');
2766+
$response->expects($this->at(1))
2767+
->method('header')
2768+
->with('Accept-Ranges', 'bytes');
27642769

2765-
$response->expects($this->at(2))
2766-
->method('header')
2767-
->with([
2768-
'Content-Length' => 18,
2769-
'Content-Range' => 'bytes 8-25/38',
2770-
]);
2771-
2772-
$response->expects($this->any())
2773-
->method('_isActive')
2774-
->will($this->returnValue(true));
2770+
$response->expects($this->at(2))
2771+
->method('header')
2772+
->with([
2773+
'Content-Length' => 18,
2774+
'Content-Range' => 'bytes 8-25/38',
2775+
]);
27752776

2776-
$response->file(
2777-
TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
2778-
['download' => false]
2779-
);
2777+
$response->expects($this->any())
2778+
->method('_isActive')
2779+
->will($this->returnValue(true));
27802780

2781-
ob_start();
2782-
$result = $response->send();
2783-
$output = ob_get_clean();
2784-
$this->assertEquals(206, $response->statusCode());
2785-
$this->assertEquals('is the test asset ', $output);
2786-
$this->assertNotSame(false, $result);
2781+
$response->file(
2782+
TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
2783+
['download' => false]
2784+
);
2785+
2786+
ob_start();
2787+
$result = $response->send();
2788+
$output = ob_get_clean();
2789+
$this->assertEquals(206, $response->statusCode());
2790+
$this->assertEquals('is the test asset ', $output);
2791+
$this->assertNotSame(false, $result);
2792+
});
27872793
}
27882794

27892795
/**
27902796
* testFileRangeInvalidNoDownload method
27912797
*
2798+
* @group deprecated
27922799
* @return void
27932800
*/
27942801
public function testFileRangeInvalidNoDownload()
27952802
{
2796-
$_SERVER['HTTP_RANGE'] = 'bytes=30-2';
2797-
$response = $this->getMockBuilder('Cake\Http\Response')
2798-
->setMethods([
2799-
'header',
2800-
'type',
2801-
'_sendHeader',
2802-
'_isActive',
2803-
])
2804-
->getMock();
2805-
2806-
$response->expects($this->at(1))
2807-
->method('header')
2808-
->with('Accept-Ranges', 'bytes');
2809-
2810-
$response->expects($this->at(2))
2811-
->method('header')
2812-
->with([
2813-
'Content-Range' => 'bytes 0-37/38',
2814-
]);
2803+
$this->deprecated(function () {
2804+
$_SERVER['HTTP_RANGE'] = 'bytes=30-2';
2805+
$response = $this->getMockBuilder('Cake\Http\Response')
2806+
->setMethods([
2807+
'_sendHeader',
2808+
])
2809+
->getMock();
28152810

2816-
$response->file(
2817-
TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
2818-
['download' => false]
2819-
);
2811+
$response->file(
2812+
TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css',
2813+
['download' => false]
2814+
);
28202815

2821-
$this->assertEquals(416, $response->statusCode());
2822-
$result = $response->send();
2816+
$this->assertEquals(416, $response->statusCode());
2817+
$this->assertEquals('bytes', $response->getHeaderLine('Accept-Ranges'));
2818+
$this->assertEquals('text/css', $response->getType());
2819+
$this->assertEquals('bytes 0-37/38', $response->getHeaderLine('Content-Range'));
2820+
$result = $response->send();
2821+
});
28232822
}
28242823

28252824
/**
28262825
* Test the location method.
28272826
*
2827+
* @group deprecated
28282828
* @return void
28292829
*/
28302830
public function testLocation()
28312831
{
2832-
$response = new Response();
2833-
$this->assertNull($response->location(), 'No header should be set.');
2834-
$this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
2835-
$this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
2832+
$this->deprecated(function () {
2833+
$response = new Response();
2834+
$this->assertNull($response->location(), 'No header should be set.');
2835+
$this->assertNull($response->location('http://example.org'), 'Setting a location should return null');
2836+
$this->assertEquals('http://example.org', $response->location(), 'Reading a location should return the value.');
2837+
});
28362838
}
28372839

28382840
/**

0 commit comments

Comments
 (0)