2010-07-31 17:03:02 +00:00
|
|
|
|
<?php
|
2011-09-13 03:00:35 +00:00
|
|
|
|
/**
|
|
|
|
|
* CakeResponse Test case file.
|
|
|
|
|
*
|
|
|
|
|
* PHP 5
|
|
|
|
|
*
|
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under The MIT License
|
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
|
*
|
|
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
|
* @package Cake.Test.Case.Network
|
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
|
*/
|
2010-12-09 05:13:11 +00:00
|
|
|
|
App::uses('CakeResponse', 'Network');
|
2010-07-31 17:03:02 +00:00
|
|
|
|
|
2011-09-13 03:00:35 +00:00
|
|
|
|
class CakeResponseTest extends CakeTestCase {
|
2010-07-31 17:03:02 +00:00
|
|
|
|
|
2011-11-05 21:21:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* Setup for tests
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setUp() {
|
|
|
|
|
ob_start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cleanup after tests
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function tearDown() {
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-31 17:34:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests the request object constructor
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-07-31 17:03:02 +00:00
|
|
|
|
public function testConstruct() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$this->assertNull($response->body());
|
2010-07-31 19:10:58 +00:00
|
|
|
|
$this->assertEquals($response->charset(), 'UTF-8');
|
2010-07-31 17:03:02 +00:00
|
|
|
|
$this->assertEquals($response->type(), 'text/html');
|
|
|
|
|
$this->assertEquals($response->statusCode(), 200);
|
|
|
|
|
|
|
|
|
|
$options = array(
|
|
|
|
|
'body' => 'This is the body',
|
2010-07-31 19:10:58 +00:00
|
|
|
|
'charset' => 'my-custom-charset',
|
2010-07-31 17:03:02 +00:00
|
|
|
|
'type' => 'mp3',
|
|
|
|
|
'status' => '203'
|
|
|
|
|
);
|
|
|
|
|
$response = new CakeResponse($options);
|
|
|
|
|
$this->assertEquals($response->body(), 'This is the body');
|
2010-07-31 19:10:58 +00:00
|
|
|
|
$this->assertEquals($response->charset(), 'my-custom-charset');
|
2010-07-31 17:03:02 +00:00
|
|
|
|
$this->assertEquals($response->type(), 'audio/mpeg');
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEquals($response->statusCode(), 203);
|
2010-07-31 17:03:02 +00:00
|
|
|
|
}
|
2010-07-31 17:34:53 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the body method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testBody() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$this->assertNull($response->body());
|
|
|
|
|
$response->body('Response body');
|
|
|
|
|
$this->assertEquals($response->body(), 'Response body');
|
|
|
|
|
$this->assertEquals($response->body('Changed Body'), 'Changed Body');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-07-31 19:10:58 +00:00
|
|
|
|
* Tests the charset method
|
2010-07-31 17:34:53 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
2010-07-31 19:10:58 +00:00
|
|
|
|
public function testCharset() {
|
2010-07-31 17:34:53 +00:00
|
|
|
|
$response = new CakeResponse();
|
2010-07-31 19:10:58 +00:00
|
|
|
|
$this->assertEquals($response->charset(), 'UTF-8');
|
|
|
|
|
$response->charset('iso-8859-1');
|
|
|
|
|
$this->assertEquals($response->charset(), 'iso-8859-1');
|
|
|
|
|
$this->assertEquals($response->charset('UTF-16'), 'UTF-16');
|
2010-07-31 17:34:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the statusCode method
|
|
|
|
|
*
|
2010-12-12 00:01:07 +00:00
|
|
|
|
* @expectedException CakeException
|
2010-07-31 17:34:53 +00:00
|
|
|
|
*/
|
|
|
|
|
public function testStatusCode() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$this->assertEquals($response->statusCode(), 200);
|
|
|
|
|
$response->statusCode(404);
|
|
|
|
|
$this->assertEquals($response->statusCode(), 404);
|
|
|
|
|
$this->assertEquals($response->statusCode(500), 500);
|
|
|
|
|
|
|
|
|
|
//Throws exception
|
|
|
|
|
$response->statusCode(1001);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the type method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testType() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$this->assertEquals($response->type(), 'text/html');
|
|
|
|
|
$response->type('pdf');
|
|
|
|
|
$this->assertEquals($response->type(), 'application/pdf');
|
|
|
|
|
$this->assertEquals($response->type('application/crazy-mime'), 'application/crazy-mime');
|
|
|
|
|
$this->assertEquals($response->type('json'), 'application/json');
|
|
|
|
|
$this->assertEquals($response->type('wap'), 'text/vnd.wap.wml');
|
|
|
|
|
$this->assertEquals($response->type('xhtml-mobile'), 'application/vnd.wap.xhtml+xml');
|
|
|
|
|
$this->assertEquals($response->type('csv'), 'text/csv');
|
2010-08-01 18:52:02 +00:00
|
|
|
|
|
|
|
|
|
$response->type(array('keynote' => 'application/keynote'));
|
|
|
|
|
$this->assertEquals($response->type('keynote'), 'application/keynote');
|
2010-08-02 23:46:19 +00:00
|
|
|
|
|
2010-08-02 23:40:01 +00:00
|
|
|
|
$this->assertFalse($response->type('wackytype'));
|
2010-07-31 17:34:53 +00:00
|
|
|
|
}
|
2010-07-31 18:43:21 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the header method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testHeader() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$headers = array();
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
$response->header('Location', 'http://example.com');
|
|
|
|
|
$headers += array('Location' => 'http://example.com');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
//Headers with the same name are overwritten
|
|
|
|
|
$response->header('Location', 'http://example2.com');
|
|
|
|
|
$headers = array('Location' => 'http://example2.com');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
$response->header(array('WWW-Authenticate' => 'Negotiate'));
|
|
|
|
|
$headers += array('WWW-Authenticate' => 'Negotiate');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
$response->header(array('WWW-Authenticate' => 'Not-Negotiate'));
|
|
|
|
|
$headers['WWW-Authenticate'] = 'Not-Negotiate';
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
$response->header(array('Age' => 12, 'Allow' => 'GET, HEAD'));
|
|
|
|
|
$headers += array('Age' => 12, 'Allow' => 'GET, HEAD');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
// String headers are allowed
|
|
|
|
|
$response->header('Content-Language: da');
|
|
|
|
|
$headers += array('Content-Language' => 'da');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
$response->header('Content-Language: da');
|
|
|
|
|
$headers += array('Content-Language' => 'da');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
|
|
|
|
|
$response->header(array('Content-Encoding: gzip', 'Vary: *', 'Pragma' => 'no-cache'));
|
|
|
|
|
$headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache');
|
|
|
|
|
$this->assertEquals($response->header(), $headers);
|
|
|
|
|
}
|
2010-07-31 21:28:13 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the send method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testSend() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->header(array(
|
|
|
|
|
'Content-Language' => 'es',
|
|
|
|
|
'WWW-Authenticate' => 'Negotiate'
|
|
|
|
|
));
|
|
|
|
|
$response->body('the response body');
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with('the response body');
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 200 OK');
|
|
|
|
|
$response->expects($this->at(1))
|
|
|
|
|
->method('_sendHeader')->with('Content-Language', 'es');
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$response->expects($this->at(2))
|
2010-07-31 21:28:13 +00:00
|
|
|
|
->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate');
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$response->expects($this->at(3))
|
|
|
|
|
->method('_sendHeader')->with('Content-Length', 17);
|
|
|
|
|
$response->expects($this->at(4))
|
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
|
2010-07-31 21:28:13 +00:00
|
|
|
|
$response->send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the send method and changing the content type
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testSendChangingContentYype() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->type('mp3');
|
|
|
|
|
$response->body('the response body');
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with('the response body');
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 200 OK');
|
|
|
|
|
$response->expects($this->at(1))
|
2011-11-04 21:26:08 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Length', 17);
|
|
|
|
|
$response->expects($this->at(2))
|
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'audio/mpeg');
|
2010-07-31 21:28:13 +00:00
|
|
|
|
$response->send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the send method and changing the content type
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testSendChangingContentType() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->type('mp3');
|
|
|
|
|
$response->body('the response body');
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with('the response body');
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 200 OK');
|
|
|
|
|
$response->expects($this->at(1))
|
2011-11-04 21:26:08 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Length', 17);
|
|
|
|
|
$response->expects($this->at(2))
|
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'audio/mpeg');
|
2010-07-31 21:28:13 +00:00
|
|
|
|
$response->send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the send method and changing the content type
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testSendWithLocation() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->header('Location', 'http://www.example.com');
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 302 Found');
|
|
|
|
|
$response->expects($this->at(1))
|
|
|
|
|
->method('_sendHeader')->with('Location', 'http://www.example.com');
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$response->expects($this->at(2))
|
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
|
2010-07-31 21:28:13 +00:00
|
|
|
|
$response->send();
|
|
|
|
|
}
|
2010-07-31 22:10:49 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the disableCache method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testDisableCache() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$expected = array(
|
|
|
|
|
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
|
|
|
|
|
'Last-Modified' => gmdate("D, d M Y H:i:s") . " GMT",
|
|
|
|
|
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
|
|
|
|
|
'Pragma' => 'no-cache'
|
|
|
|
|
);
|
|
|
|
|
$response->disableCache();
|
|
|
|
|
$this->assertEquals($response->header(), $expected);
|
|
|
|
|
}
|
2010-07-31 23:01:50 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the cache method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testCache() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$since = time();
|
|
|
|
|
$time = '+1 day';
|
|
|
|
|
$expected = array(
|
2010-10-03 21:18:07 +00:00
|
|
|
|
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
2010-10-03 23:57:08 +00:00
|
|
|
|
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
2010-07-31 23:01:50 +00:00
|
|
|
|
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
|
2010-10-03 21:18:07 +00:00
|
|
|
|
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
|
2010-07-31 23:01:50 +00:00
|
|
|
|
'Pragma' => 'cache'
|
|
|
|
|
);
|
|
|
|
|
$response->cache($since);
|
|
|
|
|
$this->assertEquals($response->header(), $expected);
|
|
|
|
|
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$since = time();
|
|
|
|
|
$time = '+5 day';
|
|
|
|
|
$expected = array(
|
2010-10-03 21:18:07 +00:00
|
|
|
|
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
2010-10-03 23:57:08 +00:00
|
|
|
|
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
2010-07-31 23:01:50 +00:00
|
|
|
|
'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT",
|
2010-10-03 21:18:07 +00:00
|
|
|
|
'Cache-Control' => 'public, max-age=' . (strtotime($time) - time()),
|
2010-07-31 23:01:50 +00:00
|
|
|
|
'Pragma' => 'cache'
|
|
|
|
|
);
|
|
|
|
|
$response->cache($since, $time);
|
|
|
|
|
$this->assertEquals($response->header(), $expected);
|
|
|
|
|
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$since = time();
|
|
|
|
|
$time = time();
|
|
|
|
|
$expected = array(
|
2010-10-03 21:18:07 +00:00
|
|
|
|
'Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
2010-10-03 23:57:08 +00:00
|
|
|
|
'Last-Modified' => gmdate("D, j M Y G:i:s ", $since) . 'GMT',
|
2010-07-31 23:01:50 +00:00
|
|
|
|
'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT",
|
2010-10-03 21:18:07 +00:00
|
|
|
|
'Cache-Control' => 'public, max-age=0',
|
2010-07-31 23:01:50 +00:00
|
|
|
|
'Pragma' => 'cache'
|
|
|
|
|
);
|
|
|
|
|
$response->cache($since, $time);
|
|
|
|
|
$this->assertEquals($response->header(), $expected);
|
|
|
|
|
}
|
2010-08-01 00:00:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2011-11-13 02:40:51 +00:00
|
|
|
|
* Tests the compress method
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2010-08-01 00:00:25 +00:00
|
|
|
|
public function testCompress() {
|
2011-11-13 02:40:51 +00:00
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
|
$this->markTestSkipped('The response compression can only be tested in cli.');
|
|
|
|
|
}
|
2011-05-31 00:49:46 +00:00
|
|
|
|
|
2010-08-01 00:00:25 +00:00
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
|
|
|
|
|
$this->assertFalse($response->compress());
|
|
|
|
|
$this->markTestSkipped('Is not possible to test output compression');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
|
|
|
|
|
$result = $response->compress();
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
|
|
|
|
|
$result = $response->compress();
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
$this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
|
2010-09-26 01:50:28 +00:00
|
|
|
|
|
|
|
|
|
ob_get_clean();
|
2010-08-01 00:00:25 +00:00
|
|
|
|
}
|
2010-08-01 04:39:09 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the httpCodes method
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-08-02 23:46:19 +00:00
|
|
|
|
public function testHttpCodes() {
|
2010-08-01 04:39:09 +00:00
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$result = $response->httpCodes();
|
2011-11-16 00:07:56 +00:00
|
|
|
|
$this->assertEquals(count($result), 39);
|
2010-08-01 04:39:09 +00:00
|
|
|
|
|
|
|
|
|
$result = $response->httpCodes(100);
|
|
|
|
|
$expected = array(100 => 'Continue');
|
2011-11-16 00:07:56 +00:00
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-08-01 04:39:09 +00:00
|
|
|
|
|
|
|
|
|
$codes = array(
|
|
|
|
|
1337 => 'Undefined Unicorn',
|
|
|
|
|
1729 => 'Hardy-Ramanujan Located'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = $response->httpCodes($codes);
|
|
|
|
|
$this->assertTrue($result);
|
2011-11-16 00:07:56 +00:00
|
|
|
|
$this->assertEquals(count($response->httpCodes()), 41);
|
2010-08-01 04:39:09 +00:00
|
|
|
|
|
|
|
|
|
$result = $response->httpCodes(1337);
|
|
|
|
|
$expected = array(1337 => 'Undefined Unicorn');
|
2011-11-16 00:07:56 +00:00
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-08-01 04:39:09 +00:00
|
|
|
|
|
|
|
|
|
$codes = array(404 => 'Sorry Bro');
|
|
|
|
|
$result = $response->httpCodes($codes);
|
|
|
|
|
$this->assertTrue($result);
|
2011-11-16 00:07:56 +00:00
|
|
|
|
$this->assertEquals(count($response->httpCodes()), 41);
|
2010-08-01 04:39:09 +00:00
|
|
|
|
|
|
|
|
|
$result = $response->httpCodes(404);
|
|
|
|
|
$expected = array(404 => 'Sorry Bro');
|
2011-11-16 00:07:56 +00:00
|
|
|
|
$this->assertEquals($expected, $result);
|
2010-08-01 04:39:09 +00:00
|
|
|
|
}
|
2010-08-02 23:46:19 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the download method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testDownload() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$expected = array(
|
|
|
|
|
'Content-Disposition' => 'attachment; filename="myfile.mp3"'
|
|
|
|
|
);
|
|
|
|
|
$response->download('myfile.mp3');
|
|
|
|
|
$this->assertEquals($response->header(), $expected);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-03 00:03:09 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests the mapType method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testMapType() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
$this->assertEquals('wav', $response->mapType('audio/x-wav'));
|
|
|
|
|
$this->assertEquals('pdf', $response->mapType('application/pdf'));
|
|
|
|
|
$this->assertEquals('xml', $response->mapType('text/xml'));
|
|
|
|
|
$this->assertEquals('html', $response->mapType('*/*'));
|
|
|
|
|
$this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
|
|
|
|
|
$expected = array('json', 'xhtml', 'css');
|
|
|
|
|
$result = $response->mapType(array('application/json', 'application/xhtml+xml', 'text/css'));
|
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
}
|
2011-10-28 02:44:55 +00:00
|
|
|
|
|
2011-11-06 20:34:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests the outputCompressed method
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testOutputCompressed() {
|
|
|
|
|
$response = new CakeResponse();
|
|
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
|
|
|
|
|
$result = $response->outputCompressed();
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
|
|
|
|
|
$result = $response->outputCompressed();
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
|
|
if (!extension_loaded("zlib")) {
|
|
|
|
|
$this->markTestSkipped('Skipping further tests for outputCompressed as zlib extension is not loaded');
|
|
|
|
|
}
|
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
|
$this->markTestSkipped('Testing outputCompressed method with compression enabled done only in cli');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ini_get("zlib.output_compression") !== '1') {
|
|
|
|
|
ob_start('ob_gzhandler');
|
|
|
|
|
}
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
|
|
|
|
|
$result = $response->outputCompressed();
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'] = '';
|
|
|
|
|
$result = $response->outputCompressed();
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
if (ini_get("zlib.output_compression") !== '1') {
|
|
|
|
|
ob_get_clean();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-28 02:44:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* Tests the send and setting of Content-Length
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testSendContentLength() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->body('the response body');
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with('the response body');
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 200 OK');
|
|
|
|
|
$response->expects($this->at(2))
|
2011-11-04 21:26:08 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
|
|
|
|
|
$response->expects($this->at(1))
|
2011-10-28 02:44:55 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Length', strlen('the response body'));
|
|
|
|
|
$response->send();
|
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$body = '長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?';
|
|
|
|
|
$response->body($body);
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with($body);
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 200 OK');
|
|
|
|
|
$response->expects($this->at(2))
|
2011-11-04 21:26:08 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
|
|
|
|
|
$response->expects($this->at(1))
|
2011-10-31 09:31:08 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Length', 116);
|
2011-10-28 02:44:55 +00:00
|
|
|
|
$response->send();
|
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', 'outputCompressed'));
|
|
|
|
|
$body = '長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?';
|
|
|
|
|
$response->body($body);
|
|
|
|
|
$response->expects($this->once())->method('outputCompressed')->will($this->returnValue(true));
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with($body);
|
|
|
|
|
$response->expects($this->exactly(2))->method('_sendHeader');
|
|
|
|
|
$response->send();
|
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent', 'outputCompressed'));
|
|
|
|
|
$body = 'hwy';
|
|
|
|
|
$response->body($body);
|
|
|
|
|
$response->header('Content-Length', 1);
|
|
|
|
|
$response->expects($this->never())->method('outputCompressed');
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with($body);
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$response->expects($this->at(1))
|
2011-10-28 02:44:55 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Length', 1);
|
|
|
|
|
$response->send();
|
2011-10-28 03:48:49 +00:00
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$body = 'content';
|
|
|
|
|
$response->statusCode(301);
|
|
|
|
|
$response->body($body);
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with($body);
|
|
|
|
|
$response->expects($this->exactly(2))->method('_sendHeader');
|
|
|
|
|
$response->send();
|
2011-11-04 01:39:30 +00:00
|
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$goofyOutput = 'I am goofily sending output in the controller';
|
|
|
|
|
echo $goofyOutput;
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$body = '長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?';
|
|
|
|
|
$response->body($body);
|
|
|
|
|
$response->expects($this->once())->method('_sendContent')->with($body);
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.1 200 OK');
|
|
|
|
|
$response->expects($this->at(1))
|
|
|
|
|
->method('_sendHeader')->with('Content-Length', strlen($goofyOutput) + 116);
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$response->expects($this->at(2))
|
|
|
|
|
->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8');
|
2011-11-04 01:39:30 +00:00
|
|
|
|
$response->send();
|
|
|
|
|
ob_end_clean();
|
2011-10-28 02:44:55 +00:00
|
|
|
|
}
|
2011-11-04 19:04:49 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests getting/setting the protocol
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testProtocol() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->protocol('HTTP/1.0');
|
|
|
|
|
$this->assertEquals('HTTP/1.0', $response->protocol());
|
|
|
|
|
$response->expects($this->at(0))
|
|
|
|
|
->method('_sendHeader')->with('HTTP/1.0 200 OK');
|
|
|
|
|
$response->send();
|
|
|
|
|
}
|
2011-11-04 19:34:32 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests getting/setting the Content-Length
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testLength() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->length(100);
|
|
|
|
|
$this->assertEquals(100, $response->length());
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$response->expects($this->at(1))
|
2011-11-04 19:34:32 +00:00
|
|
|
|
->method('_sendHeader')->with('Content-Length', 100);
|
|
|
|
|
$response->send();
|
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->length(false);
|
|
|
|
|
$this->assertFalse($response->length());
|
|
|
|
|
$response->expects($this->exactly(2))
|
|
|
|
|
->method('_sendHeader');
|
|
|
|
|
$response->send();
|
|
|
|
|
}
|
2011-11-04 20:06:47 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that the response body is unset if the status code is 304 or 204
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testUnmodifiedContent() {
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->body('This is a body');
|
|
|
|
|
$response->statusCode(204);
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
|
->method('_sendContent')->with('');
|
|
|
|
|
$response->send();
|
2011-11-04 21:26:08 +00:00
|
|
|
|
$this->assertFalse(array_key_exists('Content-Type', $response->header()));
|
2011-11-04 20:06:47 +00:00
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->body('This is a body');
|
|
|
|
|
$response->statusCode(304);
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
|
->method('_sendContent')->with('');
|
|
|
|
|
$response->send();
|
|
|
|
|
|
|
|
|
|
$response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent'));
|
|
|
|
|
$response->body('This is a body');
|
|
|
|
|
$response->statusCode(200);
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
|
->method('_sendContent')->with('This is a body');
|
|
|
|
|
$response->send();
|
|
|
|
|
}
|
2011-05-16 22:49:00 +00:00
|
|
|
|
}
|