-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
317 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
use Composer\Config; | ||
use Composer\IO; | ||
use Composer\Downloader; | ||
use Composer\Util; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
require_once dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
// start test server | ||
execInBackground('php ' . __DIR__ . '/workspace/sampleapi.php'); | ||
|
||
function execInBackground($cmd) { | ||
if (substr(php_uname(), 0, 7) === 'Windows') { | ||
pclose(popen('start /B ' . $cmd, 'r')); | ||
} else { | ||
exec($cmd . ' >/dev/null &'); | ||
} | ||
} | ||
|
||
register_shutdown_function(function(){ | ||
$ch = curl_init('http://localhost:1337/?exit=1'); | ||
curl_exec($ch); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
namespace Hirak\Prestissimo; | ||
|
||
use Composer\Config; | ||
use Composer\IO; | ||
|
||
class CurlRemoteFilesystemTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $rfs; | ||
|
||
protected function setUp() | ||
{ | ||
$io = new IO\NullIO; | ||
$config = new Config; | ||
$this->rfs = new CurlRemoteFilesystem($io, $config); | ||
|
||
$this->rfs->setPluginConfig(array( | ||
'maxConnections' => 6, | ||
'minConnections' => 3, | ||
'pipeline' => false, | ||
'verbose' => false, | ||
'insecure' => false, | ||
'capath' => '', | ||
'privatePackages' => array(), | ||
)); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
$targetFile = 'tests/workspace/target/0.txt'; | ||
if (file_exists($targetFile)) { | ||
unlink($targetFile); | ||
rmdir(dirname($targetFile)); | ||
} | ||
} | ||
|
||
public function testConstruct() | ||
{ | ||
self::assertInstanceOf('Hirak\Prestissimo\CurlRemoteFilesystem', $this->rfs); | ||
} | ||
|
||
public function testGetOptions() | ||
{ | ||
self::assertInternalType('array', $this->rfs->getOptions()); | ||
} | ||
|
||
public function testGetLastHeaders() | ||
{ | ||
self::assertInternalType('array', $this->rfs->getLastHeaders()); | ||
} | ||
|
||
public function testCopy() | ||
{ | ||
$targetUrl = 'http://localhost:1337/?wait=0'; | ||
$targetFile = 'tests/workspace/target/0.txt'; | ||
|
||
$this->rfs->copy('localhost', $targetUrl, $targetFile); | ||
|
||
self::assertFileExists($targetFile); | ||
self::assertStringEqualsFile($targetFile, '0'); | ||
|
||
// clean | ||
unlink($targetFile); | ||
rmdir(dirname($targetFile)); | ||
} | ||
|
||
public function testCopyFailure() | ||
{ | ||
$targetUrl = 'http://localhost:1337/?status=404'; | ||
$targetFile = 'tests/workspace/target/0.txt'; | ||
|
||
$this->rfs->copy('localhost', $targetUrl, $targetFile); | ||
|
||
self::assertFileNotExists($targetFile); | ||
self::assertFileNotExists(dirname($targetFile)); | ||
} | ||
|
||
/** | ||
* @expectedException Composer\Downloader\TransportException | ||
*/ | ||
public function testCopyPromptAndRetry1() | ||
{ | ||
$targetUrl = 'http://localhost:1337/?status=401'; | ||
$targetFile = 'tests/workspace/target/0.txt'; | ||
|
||
$this->rfs->copy('localhost', $targetUrl, $targetFile); | ||
} | ||
|
||
/** | ||
* @expectedException Composer\Downloader\TransportException | ||
*/ | ||
public function testCopyPromptAndRetry2() | ||
{ | ||
$targetUrl = 'http://localhost:1337/?status=403'; | ||
$targetFile = 'tests/workspace/target/0.txt'; | ||
|
||
$this->rfs->copy('localhost', $targetUrl, $targetFile); | ||
} | ||
|
||
public function testGetContents() | ||
{ | ||
$targetUrl = 'http://localhost:1337/?wait=0'; | ||
|
||
$progress = false; | ||
$response = $this->rfs->getContents('localhost', $targetUrl, $progress); | ||
|
||
self::assertEquals('0', $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace Hirak\Prestissimo; | ||
|
||
use Composer\Config; | ||
use Composer\IO; | ||
use Composer\Package; | ||
|
||
class ParallelDownloaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $downloader; | ||
|
||
protected $pluginConfig = array( | ||
'maxConnections' => 6, | ||
'minConnections' => 3, | ||
'pipeline' => false, | ||
'verbose' => false, | ||
'insecure' => false, | ||
'capath' => '', | ||
'privatePackages' => array(), | ||
); | ||
|
||
protected function setUp() | ||
{ | ||
$io = new IO\NullIO; | ||
$config = new Config; | ||
$this->downloader = new ParallelDownloader($io, $config); | ||
} | ||
|
||
public function testConstruct() | ||
{ | ||
self::assertInstanceOf('Hirak\Prestissimo\ParallelDownloader', $this->downloader); | ||
} | ||
|
||
public function testDownloadSimple() | ||
{ | ||
$packages = array( | ||
self::createPackage('vendor/package1', 'http://localhost:1337/', '1234'), | ||
self::createPackage('vendor/package2', 'http://localhost:1337/', '2345'), | ||
); | ||
|
||
$this->markTestIncomplete(); | ||
} | ||
|
||
private static function createPackage($name, $url, $ref) | ||
{ | ||
$package = new Package\Package($name, '1.0.0', 'v1.0.0'); | ||
$package->setDistUrl($url); | ||
$package->setDistReference($ref); | ||
|
||
return $package; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* test http server | ||
* | ||
* php http.php | ||
* | ||
* exit: http://localhost:1337/?exit=1 | ||
* delay: http://localhost:1337/?wait=1 (seconds) | ||
*/ | ||
set_time_limit(0); | ||
ob_implicit_flush(); | ||
|
||
$response =<<<_HTTP_ | ||
HTTP/1.1 200 OK\r | ||
Content-Type: text/plain\r | ||
Content-Length: 1\r | ||
\r | ||
_HTTP_; | ||
|
||
$context['socket']['backlog'] = 128; | ||
$server = stream_socket_server( | ||
'tcp://0.0.0.0:1337', | ||
$errno, | ||
$errmsg, | ||
STREAM_SERVER_LISTEN | STREAM_SERVER_BIND, | ||
stream_context_create($context) | ||
); | ||
stream_set_blocking($server, 0); | ||
stream_set_timeout($server, 0); | ||
|
||
$readOrigin = array(); | ||
$readOrigin[(int)$server] = $server; | ||
$writeOrigin = array(); | ||
$except = null; | ||
|
||
$waiting = array(); | ||
|
||
LOOP: { | ||
$read = $readOrigin; | ||
$write = $writeOrigin; | ||
|
||
stream_select($read, $write, $except, 1); //block | ||
|
||
if (PHP_VERSION_ID < 50400) { | ||
$readfix = array(); | ||
foreach ($read as $stream) { | ||
$readfix[(int)$stream] = $stream; | ||
} | ||
$read = $readfix; | ||
} | ||
|
||
if (isset($read[(int)$server])) { | ||
$connection = stream_socket_accept($server, 30, $peername); | ||
stream_set_blocking($connection, 0); | ||
$readOrigin[(int)$connection] = $connection; | ||
unset($read[(int)$server]); | ||
} | ||
|
||
$now = microtime(true); | ||
foreach ($waiting as $waitingStream) { | ||
list($stream, $until, $messageBody) = $waitingStream; | ||
if ($now > $until) { | ||
$index = (int)$stream; | ||
fwrite($stream, $response . $messageBody); | ||
fclose($stream); | ||
unset($waiting[$index]); | ||
unset($readOrigin[$index]); | ||
} | ||
} | ||
|
||
foreach ($read as $stream) { | ||
$requestLine = stream_get_line($stream, 4096, "\r\n"); | ||
|
||
list(, $path,) = explode(' ', $requestLine); | ||
parse_str(parse_url($path, PHP_URL_QUERY), $query); | ||
if (isset($query['exit']) && $query['exit'] === '1') exit; | ||
|
||
if (isset($query['status'])) { | ||
switch ($query['status']) { | ||
case '401': | ||
$errorResponse = <<<_HTTP_ | ||
HTTP/1.1 401 Unauthorized\r | ||
Connection: close\r | ||
\r | ||
_HTTP_; | ||
break; | ||
case '403': | ||
$errorResponse = <<<_HTTP_ | ||
HTTP/1.1 403 Forbidden\r | ||
Connection: close\r | ||
\r | ||
_HTTP_; | ||
break; | ||
case '404': | ||
default: | ||
$errorResponse = <<<_HTTP_ | ||
HTTP/1.1 404 Not Found\r | ||
Connection: close\r | ||
\r | ||
_HTTP_; | ||
break; | ||
} | ||
fwrite($stream, $errorResponse); | ||
fclose($stream); | ||
unset($readOrigin[(int)$stream]); | ||
continue; | ||
} | ||
|
||
if (isset($query['wait']) && $query['wait'] > 0) { | ||
$waiting[(int)$stream] = array( | ||
$stream, | ||
microtime(true) + (int)$query['wait'], | ||
(int)$query['wait'], | ||
); | ||
} else { | ||
fwrite($stream, $response . '0'); | ||
fclose($stream); | ||
} | ||
unset($readOrigin[(int)$stream]); | ||
} | ||
|
||
goto LOOP; | ||
} |