Implementing the CakeResponse::compress() method

This commit is contained in:
José Lorenzo Rodríguez 2010-07-31 19:30:25 -04:30
parent 8780f0b33d
commit abafdb037b
3 changed files with 28 additions and 3 deletions

View file

@ -521,10 +521,14 @@ class CakeResponse {
}
/**
* Sets the correct headers and output buffering handler to send a compressed response
* Sets the correct output buffering handler to send a compressed response
*
* @return boolean false if client does not accept compressed responses or no handler is available, true otherwise
*/
public function compress() {
$compressionEnabled = ini_get("zlib.output_compression") !== '1' &&
extension_loaded("zlib") &&
(strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false);
return $compressionEnabled && ob_start('ob_gzhandler');
}
}

View file

@ -1,5 +1,4 @@
<?php
App::import('Core', 'CakeRequest');
class CakeRequestTestCase extends CakeTestCase {

View file

@ -2,7 +2,7 @@
App::import('Core', 'CakeResponse');
class CakeRequestTestCase extends CakeTestCase {
class CakeResponseTestCase extends CakeTestCase {
/**
@ -257,4 +257,26 @@ class CakeRequestTestCase extends CakeTestCase {
$response->cache($since, $time);
$this->assertEquals($response->header(), $expected);
}
/**
* Tests the compress method
*
*/
public function testCompress() {
$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()));
ob_end_clean();
}
}