mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Implementing the CakeResponse::compress() method
This commit is contained in:
parent
8780f0b33d
commit
abafdb037b
3 changed files with 28 additions and 3 deletions
|
@ -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');
|
||||
}
|
||||
}
|
|
@ -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 @@ 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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue