Skip to content

Commit

Permalink
Merge pull request #3 from j0k3r/head-request
Browse files Browse the repository at this point in the history
Avoid HEAD request to return false
  • Loading branch information
j0k3r committed Nov 23, 2015
2 parents 2c97236 + 7a6a8a3 commit d5b7a0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/SafeCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public function execute($url)
$headerSize = curl_getinfo($this->curlHandle, CURLINFO_HEADER_SIZE);
$response = substr($response, $headerSize);

// substr return false when string goes empty (in case of a HEAD request or when reponse body is empty for example)
if (false === $response) {
return '';
}

return $response;
}
}
17 changes: 16 additions & 1 deletion tests/SafeCurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class SafeCurlTest extends \PHPUnit_Framework_TestCase
{
public function testFunctionnal()
public function testFunctionnalGET()
{
$handle = curl_init();

Expand All @@ -17,6 +17,21 @@ public function testFunctionnal()
$this->assertNotContains('HTTP/1.1 302 Found', $response);
}

public function testFunctionnalHEAD()
{
$handle = curl_init();
// for an unknown reason, HEAD request failed: https://travis-ci.org/j0k3r/safecurl/jobs/91936743
// curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($handle, CURLOPT_NOBODY, true);

$safeCurl = new SafeCurl($handle);
$response = $safeCurl->execute('http://40.media.tumblr.com/39e917383bf5fe228b82fef850251220/tumblr_nxyw8cjiYx1u7jfjwo1_100.jpg');

$this->assertEquals('', $response);
$this->assertEquals($handle, $safeCurl->getCurlHandle());
$this->assertNotContains('HTTP/1.1 302 Found', $response);
}

/**
* @expectedException fin1te\SafeCurl\Exception
* @expectedExceptionMessage SafeCurl expects a valid cURL resource - "NULL" provided.
Expand Down

0 comments on commit d5b7a0d

Please sign in to comment.