mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-09-11 19:06:36 +00:00
Add CURLOPT_RETURNTRANSFER reset when using download method; Fix download method test failing on PHP 5.3 due to different png files being created on the server and client when using imagepng().
This commit is contained in:
@@ -188,7 +188,18 @@ class Curl
|
||||
$this->setOpt(CURLOPT_FILE, $fh);
|
||||
$this->get($url);
|
||||
fclose($fh);
|
||||
|
||||
// Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
|
||||
// resource has gone away, resetting to default". Using null causes
|
||||
// "curl_setopt(): supplied argument is not a valid File-Handle
|
||||
// resource".
|
||||
$this->setOpt(CURLOPT_FILE, STDOUT);
|
||||
|
||||
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
|
||||
// responses as the return value of curl_exec(). Without this,
|
||||
// curl_exec() will revert to returning boolean values.
|
||||
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
return ! $this->error;
|
||||
}
|
||||
|
||||
|
||||
@@ -332,23 +332,43 @@ class CurlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testDownload()
|
||||
{
|
||||
$save_to_path = tempnam('/tmp', 'php-curl-class.');
|
||||
$file_path = Helper\get_png();
|
||||
// Upload a file.
|
||||
$upload_file_path = Helper\get_png();
|
||||
$upload_test = new Test();
|
||||
$upload_test->server('upload_response', 'POST', array(
|
||||
'image' => '@' . $upload_file_path,
|
||||
));
|
||||
$uploaded_file_path = $upload_test->curl->response;
|
||||
$this->assertNotEquals($upload_file_path, $uploaded_file_path);
|
||||
$this->assertEquals(md5_file($upload_file_path), md5_file($uploaded_file_path));
|
||||
$this->assertEquals(md5_file($upload_file_path), $upload_test->curl->response_headers['ETag']);
|
||||
|
||||
$test = new Test();
|
||||
$test->curl->setHeader('X-DEBUG-TEST', 'download_response');
|
||||
$this->assertTrue($test->curl->download(Test::TEST_URL, $save_to_path));
|
||||
$this->assertEquals(filesize($file_path), filesize($save_to_path));
|
||||
$this->assertEquals(md5_file($file_path), md5_file($save_to_path));
|
||||
$this->assertEquals(md5_file($file_path), $test->curl->response_headers['ETag']);
|
||||
// Download the file.
|
||||
$downloaded_file_path = tempnam('/tmp', 'php-curl-class.');
|
||||
$download_test = new Test();
|
||||
$download_test->curl->setHeader('X-DEBUG-TEST', 'download_response');
|
||||
$this->assertTrue($download_test->curl->download(Test::TEST_URL . '?' . http_build_query(array(
|
||||
'file_path' => $uploaded_file_path,
|
||||
)), $downloaded_file_path));
|
||||
$this->assertNotEquals($uploaded_file_path, $downloaded_file_path);
|
||||
|
||||
$test->curl->setHeader('X-DEBUG-TEST', 'get');
|
||||
$test->curl->get(Test::TEST_URL);
|
||||
$this->assertEquals(filesize($upload_file_path), filesize($downloaded_file_path));
|
||||
$this->assertEquals(md5_file($upload_file_path), md5_file($downloaded_file_path));
|
||||
$this->assertEquals(md5_file($upload_file_path), $download_test->curl->response_headers['ETag']);
|
||||
|
||||
unlink($file_path);
|
||||
unlink($save_to_path);
|
||||
$this->assertFalse(file_exists($file_path));
|
||||
$this->assertFalse(file_exists($save_to_path));
|
||||
// Ensure successive requests set the appropriate values.
|
||||
$this->assertEquals('GET', $download_test->server('server', 'GET', array(
|
||||
'key' => 'REQUEST_METHOD',
|
||||
)));
|
||||
$this->assertFalse(is_bool($download_test->curl->response));
|
||||
$this->assertFalse(is_bool($download_test->curl->raw_response));
|
||||
|
||||
unlink($upload_file_path);
|
||||
unlink($uploaded_file_path);
|
||||
unlink($downloaded_file_path);
|
||||
$this->assertFalse(file_exists($upload_file_path));
|
||||
$this->assertFalse(file_exists($uploaded_file_path));
|
||||
$this->assertFalse(file_exists($downloaded_file_path));
|
||||
}
|
||||
|
||||
public function testBasicHttpAuth401Unauthorized()
|
||||
|
||||
@@ -121,13 +121,19 @@ if ($test == 'http_basic_auth') {
|
||||
$rss->appendChild($channel);
|
||||
echo $doc->saveXML();
|
||||
exit;
|
||||
} elseif ($test === 'upload_response') {
|
||||
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
|
||||
move_uploaded_file($_FILES['image']['tmp_name'], $tmp_filename);
|
||||
header('ETag: ' . md5_file($tmp_filename));
|
||||
echo $tmp_filename;
|
||||
exit;
|
||||
} elseif ($test === 'download_response') {
|
||||
$png = Helper\create_png();
|
||||
$unsafe_file_path = $_GET['file_path'];
|
||||
header('Content-Type: image/png');
|
||||
header('Content-Disposition: attachment; filename="image.png"');
|
||||
header('Content-Length: ' . strlen($png));
|
||||
header('ETag: ' . md5($png));
|
||||
echo $png;
|
||||
header('Content-Length: ' . filesize($unsafe_file_path));
|
||||
header('ETag: ' . md5_file($unsafe_file_path));
|
||||
readfile($unsafe_file_path);
|
||||
exit;
|
||||
} elseif ($test === 'error_message') {
|
||||
if (function_exists('http_response_code')) {
|
||||
|
||||
Reference in New Issue
Block a user