Skip to content

Commit

Permalink
HttpResponse was causing error with HEAD requests, because the check …
Browse files Browse the repository at this point in the history
…for compressed body content assumes first 2 chars are present (and failure was dependent on current error reporting levels). Added unit test for HttpResponse. Also removed spurious whitespace from end of store.class.php which appears in rendered responses.
  • Loading branch information
jhigman committed Sep 23, 2011
1 parent 9089de6 commit 96ee7ee
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
11 changes: 6 additions & 5 deletions httpresponse.class.php
Expand Up @@ -55,13 +55,14 @@ function __construct($status_code = null) {
public function __get($k){
return $this->$k;
}

public function __set($k, $v){
if($k=='body' AND $this->content_is_gzip_encoded()){
//strip the gzip header
if(ord($v[0]) == 0x1f && ord($v[1]) == 0x8b)
{
$v = substr($v,10);
$v = gzinflate($v);
//strip the gzip header, explicitly suppressing errors
if(ord(@$v[0]) == 0x1f && ord(@$v[1]) == 0x8b)
{
$v = substr($v,10);
$v = gzinflate($v);
}
}
$this->$k=$v;
Expand Down
3 changes: 1 addition & 2 deletions store.class.php
Expand Up @@ -297,5 +297,4 @@ function mirror_from_url($url, $rdf_content=false)
}

}
?>

?>
30 changes: 23 additions & 7 deletions tests/httpresponse.test.php
Expand Up @@ -6,15 +6,31 @@

class HttpResponseTest extends PHPUnit_Framework_TestCase {

private $old_error_reporting = 0;

// switch off error suppression while we test test_set_body_empty
// because the error reporting in an end-user environment can't be guaranteed
// after the tests, restore the error reporting

function setUp() {
$this->old_error_reporting = error_reporting();
error_reporting(E_ALL);
}

function tearDown() {
error_reporting($this->old_error_reporting);
}

function test_set_body_with_empty_string(){

function test_get_content_type(){
$response = new HttpResponse(200);

$headers = array();
$headers['content-encoding'] = 'gzip';
$response->headers = $headers;
$response->body = "";

$response = new HttpResponse('200');
$response->headers = array('content-type'=> 'text/html');
$this->assertEquals('text/html', $response->get_content_type(), 'Content type should be text/html');
$response->headers = array('Content-Type'=> 'text/xml');
$this->assertEquals('text/xml', $response->get_content_type(), 'Content type should be text/xml');

$this->assertTrue($response->is_success(), 'Should have handled empty body without causing error');
}
}
?>
3 changes: 3 additions & 0 deletions tests/runtests.php
Expand Up @@ -17,6 +17,7 @@
require_once MORIARTY_TEST_DIR . 'fieldpredicatemap.test.php';
require_once MORIARTY_TEST_DIR . 'valuepool.test.php';
require_once MORIARTY_TEST_DIR . 'httprequest.test.php';
require_once MORIARTY_TEST_DIR . 'httpresponse.test.php';
require_once MORIARTY_TEST_DIR . 'credentials.test.php';
require_once MORIARTY_TEST_DIR . 'privategraph.test.php';
require_once MORIARTY_TEST_DIR . 'contentbox.test.php';
Expand All @@ -42,6 +43,7 @@
require_once MORIARTY_TEST_DIR . 'datatableresult.test.php';

error_reporting(E_ALL && ~E_STRICT);

function exceptions_error_handler($severity, $message, $filename, $lineno) {
if (error_reporting() == 0) {
return;
Expand Down Expand Up @@ -81,6 +83,7 @@ public static function suite()
$suite->addTestSuite('CredentialsTest');
$suite->addTestSuite('ValuePoolTest');
$suite->addTestSuite('HttpRequestTest');
$suite->addTestSuite('HttpResponseTest');
$suite->addTestSuite('PrivateGraphTest');
$suite->addTestSuite('MetaboxTest');
$suite->addTestSuite('ContentboxTest');
Expand Down

0 comments on commit 96ee7ee

Please sign in to comment.