Skip to content
Permalink
Browse files

Implementing the CakeResponse::compress() method

  • Loading branch information...
lorenzo committed Aug 1, 2010
1 parent 8780f0b commit abafdb037b523b5e09c6f7a64466d00ec0c5753a
@@ -521,10 +521,14 @@ public function cache($since, $time = '+1 day') {
}
/**
* 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');
}
}
@@ -1,5 +1,4 @@
<?php
App::import('Core', 'CakeRequest');
class CakeRequestTestCase extends CakeTestCase {
@@ -2,7 +2,7 @@
App::import('Core', 'CakeResponse');
class CakeRequestTestCase extends CakeTestCase {
class CakeResponseTestCase extends CakeTestCase {
/**
@@ -257,4 +257,26 @@ public function testCache() {
$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();
}
}

0 comments on commit abafdb0

Please sign in to comment.
You can’t perform that action at this time.