Skip to content

Commit

Permalink
Adding tests for JWeb::compress()
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisLandry committed Sep 20, 2011
1 parent f464fad commit 929559e
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 10 deletions.
15 changes: 8 additions & 7 deletions libraries/joomla/application/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,18 @@ protected function compress()
'deflate' => 'deflate'
);

// Get the client supported encoding.
$encodings = $this->client->encodings;
// Get the supported encoding.
$encodings = array_intersect($this->client->encodings, array_keys($supported));


// If no supported encoding is detected do nothing and return.
if (empty($encodings) || array_intersect($encodings, array_keys($supported)))
if (empty($encodings))
{
return;
}

// Verify that headers have not yet been sent, and that our connection is still alive.
if ($this->checkHeadersSent() || $this->checkConnectionAlive())
if ($this->checkHeadersSent() || !$this->checkConnectionAlive())
{
return;
}
Expand All @@ -439,7 +440,7 @@ protected function compress()
$data = $this->getBody();
$gzdata = gzencode($data, 4, ($supported[$encoding] == 'gz') ? FORCE_GZIP : FORCE_DEFLATE);

// If there was a problem encoding the data just return the unencoded data.
// If there was a problem encoding the data just try the next encoding scheme.
if ($gzdata === false)
{
continue;
Expand All @@ -452,8 +453,8 @@ protected function compress()
// Replace the output with the encoded data.
$this->setBody($gzdata);

// Compression complete, let's return.
return;
// Compression complete, let's break out of the loop.
break;
}
}
}
Expand Down
264 changes: 261 additions & 3 deletions tests/suite/joomla/application/JWebTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,21 @@ public function testClearHeaders()
*/
public function testClose()
{
$this->markTestIncomplete();
// Make sure the application is not already closed.
$this->assertSame(
$this->inspector->closed,
null,
'Checks the application doesn\'t start closed.'
);

$this->inspector->close(3);

// Make sure the application is closed with code 3.
$this->assertSame(
$this->inspector->closed,
3,
'Checks the application was closed with exit code 3.'
);
}

/**
Expand All @@ -331,9 +345,253 @@ public function testClose()
*
* @since 11.3
*/
public function testCompress()
public function testCompressWithGzipEncoding()
{
$this->markTestIncomplete();
// Fill the header body with a value.
$this->inspector->setClassProperty(
'response',
(object) array(
'cachable' => null,
'headers' => null,
'body' => array('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'),
)
);

// Load the client encoding with a value.
$this->inspector->setClassProperty(
'client',
(object) array(
'encodings' => array('gzip', 'deflate'),
)
);

$this->inspector->compress();

// Ensure that the compressed body is shorter than the raw body.
$this->assertThat(
strlen($this->inspector->getBody()),
$this->lessThan(471),
'Checks the compressed output is smaller than the uncompressed output.'
);

// Ensure that the compression headers were set.
$this->assertThat(
$this->inspector->getClassProperty('response')->headers,
$this->equalTo(array(
0 => array('name' => 'Content-Encoding', 'value' => 'gzip'),
1 => array('name' => 'X-Content-Encoded-By', 'value' => 'Joomla')
)),
'Checks the headers were set correctly.'
);
}

/**
* Tests the JWeb::compress method.
*
* @return void
*
* @since 11.3
*/
public function testCompressWithDeflateEncoding()
{
// Fill the header body with a value.
$this->inspector->setClassProperty(
'response',
(object) array(
'cachable' => null,
'headers' => null,
'body' => array('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'),
)
);

// Load the client encoding with a value.
$this->inspector->setClassProperty(
'client',
(object) array(
'encodings' => array('deflate', 'gzip'),
)
);

$this->inspector->compress();

// Ensure that the compressed body is shorter than the raw body.
$this->assertThat(
strlen($this->inspector->getBody()),
$this->lessThan(471),
'Checks the compressed output is smaller than the uncompressed output.'
);

// Ensure that the compression headers were set.
$this->assertThat(
$this->inspector->getClassProperty('response')->headers,
$this->equalTo(array(
0 => array('name' => 'Content-Encoding', 'value' => 'deflate'),
1 => array('name' => 'X-Content-Encoded-By', 'value' => 'Joomla')
)),
'Checks the headers were set correctly.'
);
}

/**
* Tests the JWeb::compress method.
*
* @return void
*
* @since 11.3
*/
public function testCompressWithNoAcceptEncodings()
{
// Fill the header body with a value.
$this->inspector->setClassProperty(
'response',
(object) array(
'cachable' => null,
'headers' => null,
'body' => array('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'),
)
);

// Load the client encoding with a value.
$this->inspector->setClassProperty(
'client',
(object) array(
'encodings' => array(),
)
);

$this->inspector->compress();

// Ensure that the compressed body is the same as the raw body since there is no compression.
$this->assertThat(
strlen($this->inspector->getBody()),
$this->equalTo(471),
'Checks the compressed output is the same as the uncompressed output -- no compression.'
);

// Ensure that the compression headers were not set.
$this->assertThat(
$this->inspector->getClassProperty('response')->headers,
$this->equalTo(null),
'Checks the headers were set correctly.'
);
}

/**
* Tests the JWeb::compress method.
*
* @return void
*
* @since 11.3
*/
public function testCompressWithHeadersSent()
{
// Fill the header body with a value.
$this->inspector->setClassProperty(
'response',
(object) array(
'cachable' => null,
'headers' => null,
'body' => array('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'),
)
);

// Load the client encoding with a value.
$this->inspector->setClassProperty(
'client',
(object) array(
'encodings' => array('gzip', 'deflate'),
)
);

// Set the headers sent flag to true.
JWebInspector::$headersSent = true;

$this->inspector->compress();

// Set the headers sent flag back to false.
JWebInspector::$headersSent = false;

// Ensure that the compressed body is the same as the raw body since there is no compression.
$this->assertThat(
strlen($this->inspector->getBody()),
$this->equalTo(471),
'Checks the compressed output is the same as the uncompressed output -- no compression.'
);
// Ensure that the compression headers were not set.
$this->assertThat(
$this->inspector->getClassProperty('response')->headers,
$this->equalTo(null),
'Checks the headers were set correctly.'
);
}

/**
* Tests the JWeb::compress method.
*
* @return void
*
* @since 11.3
*/
public function testCompressWithUnsupportedEncodings()
{
// Fill the header body with a value.
$this->inspector->setClassProperty(
'response',
(object) array(
'cachable' => null,
'headers' => null,
'body' => array('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'),
)
);

// Load the client encoding with a value.
$this->inspector->setClassProperty(
'client',
(object) array(
'encodings' => array('foo', 'bar'),
)
);

$this->inspector->compress();

// Ensure that the compressed body is the same as the raw body since there is no supported compression.
$this->assertThat(
strlen($this->inspector->getBody()),
$this->equalTo(471),
'Checks the compressed output is the same as the uncompressed output -- no supported compression.'
);

// Ensure that the compression headers were not set.
$this->assertThat(
$this->inspector->getClassProperty('response')->headers,
$this->equalTo(null),
'Checks the headers were set correctly.'
);
}

/**
Expand Down

0 comments on commit 929559e

Please sign in to comment.