forked from thephpleague/flysystem
-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
159 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace League\Flysystem\AdapterTestUtilities; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
/** | ||
* This class provides a client for the HTTP API provided by the proxy that simulates network issues. | ||
* | ||
* @see https://github.com/shopify/toxiproxy#http-api | ||
* | ||
* @phpstan-type RegisteredProxies 'ftp'|'sftp'|'ftpd' | ||
* @phpstan-type StreamDirection 'upstream'|'downstream' | ||
* @phpstan-type Type 'latency'|'bandwidth'|'slow_close'|'timeout'|'reset_peer'|'slicer'|'limit_data' | ||
* @phpstan-type Attributes array{latency?: int, jitter?: int, rate?: int, delay?: int} | ||
* @phpstan-type Toxic array{name?: string, type: Type, stream?: StreamDirection, toxicity?: float, attributes: Attributes} | ||
*/ | ||
final class ToxiproxyManagement | ||
{ | ||
/** @var Client */ | ||
private $apiClient; | ||
|
||
public function __construct(Client $apiClient) | ||
{ | ||
$this->apiClient = $apiClient; | ||
} | ||
|
||
public static function forServer(string $apiUri = 'http://localhost:8474'): self | ||
{ | ||
return new self( | ||
new Client( | ||
[ | ||
'base_uri' => $apiUri, | ||
'base_url' => $apiUri, // Compatibility with older versions of Guzzle | ||
] | ||
) | ||
); | ||
} | ||
|
||
public function removeAllToxics(): void | ||
{ | ||
$this->apiClient->post('/reset'); | ||
} | ||
|
||
/** | ||
* Simulates a peer reset on the client->server direction. | ||
* | ||
* @param RegisteredProxies $proxyName | ||
*/ | ||
public function resetPeerOnRequest( | ||
string $proxyName, | ||
int $timeoutInMilliseconds | ||
): void { | ||
$configuration = [ | ||
'type' => 'reset_peer', | ||
'stream' => 'upstream', | ||
'attributes' => ['timeout' => $timeoutInMilliseconds], | ||
]; | ||
|
||
$this->addToxic($proxyName, $configuration); | ||
} | ||
|
||
/** | ||
* Registers a network toxic for the given proxy. | ||
* | ||
* @param RegisteredProxies $proxyName | ||
* @param Toxic $configuration | ||
*/ | ||
private function addToxic(string $proxyName, array $configuration): void | ||
{ | ||
$this->apiClient->post('/proxies/' . $proxyName . '/toxics', ['json' => $configuration]); | ||
} | ||
} |
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,20 @@ | ||
[ | ||
{ | ||
"name": "sftp", | ||
"listen": "[::]:8222", | ||
"upstream": "sftp:22", | ||
"enabled": true | ||
}, | ||
{ | ||
"name": "ftp", | ||
"listen": "[::]:8121", | ||
"upstream": "ftp:21", | ||
"enabled": true | ||
}, | ||
{ | ||
"name": "ftpd", | ||
"listen": "[::]:8122", | ||
"upstream": "ftpd:21", | ||
"enabled": true | ||
} | ||
] |