Merge pull request #101 from zachborboa/master

Fix #98: Implement download() method
This commit is contained in:
Zach Borboa
2014-12-11 22:00:54 -08:00
3 changed files with 90 additions and 0 deletions
+21
View File
@@ -182,6 +182,27 @@ class Curl
return $this->exec();
}
public function download($url, $filename)
{
$fh = fopen($filename, 'wb');
$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;
}
public function setBasicAuthentication($username, $password = '')
{
$this->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+44
View File
@@ -330,6 +330,50 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertEquals('OPTIONS', $test->curl->response_headers['X-REQUEST-METHOD']);
}
public function testDownload()
{
// 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->file_path;
$this->assertNotEquals($upload_file_path, $uploaded_file_path);
$this->assertEquals(md5_file($upload_file_path), $upload_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);
$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']);
// 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));
// Remove server file.
$this->assertEquals('true', $download_test->server('upload_cleanup', 'POST', array(
'file_path' => $uploaded_file_path,
)));
unlink($upload_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()
{
$test = new Test();
+25
View File
@@ -1,4 +1,8 @@
<?php
require 'Helper.php';
use \Helper\Test;
$http_raw_post_data = file_get_contents('php://input');
$_PUT = array();
$_PATCH = array();
@@ -117,6 +121,27 @@ 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('Content-Type: application/json');
header('ETag: ' . md5_file($tmp_filename));
echo json_encode(array(
'file_path' => $tmp_filename,
));
exit;
} elseif ($test === 'upload_cleanup') {
$unsafe_file_path = $_POST['file_path'];
echo var_export(unlink($unsafe_file_path), true);
exit;
} elseif ($test === 'download_response') {
$unsafe_file_path = $_GET['file_path'];
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="image.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')) {
http_response_code(401);