| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests; |
| 3 |
|
| 4 |
use GuzzleHttp\Client; |
| 5 |
use GuzzleHttp\Psr7; |
| 6 |
use Psr\Http\Message\ResponseInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* The Server class is used to control a scripted webserver using node.js that |
| 10 |
* will respond to HTTP requests with queued responses. |
| 11 |
* |
| 12 |
* Queued responses will be served to requests using a FIFO order. All requests |
| 13 |
* received by the server are stored on the node.js server and can be retrieved |
| 14 |
* by calling {@see Server::received()}. |
| 15 |
* |
| 16 |
* Mock responses that don't require data to be transmitted over HTTP a great |
| 17 |
* for testing. Mock response, however, cannot test the actual sending of an |
| 18 |
* HTTP request using cURL. This test server allows the simulation of any |
| 19 |
* number of HTTP request response transactions to test the actual sending of |
| 20 |
* requests over the wire without having to leave an internal network. |
| 21 |
*/ |
| 22 |
class Server |
| 23 |
{ |
| 24 |
/** @var Client */ |
| 25 |
private static $client; |
| 26 |
private static $started = false; |
| 27 |
public static $url = 'http://127.0.0.1:8126/'; |
| 28 |
public static $port = 8126; |
| 29 |
|
| 30 |
/** |
| 31 |
* Flush the received requests from the server |
| 32 |
* @throws \RuntimeException |
| 33 |
*/ |
| 34 |
public static function flush() |
| 35 |
{ |
| 36 |
return self::getClient()->request('DELETE', 'guzzle-server/requests'); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Queue an array of responses or a single response on the server. |
| 41 |
* |
| 42 |
* Any currently queued responses will be overwritten. Subsequent requests |
| 43 |
* on the server will return queued responses in FIFO order. |
| 44 |
* |
| 45 |
* @param array|ResponseInterface $responses A single or array of Responses |
| 46 |
* to queue. |
| 47 |
* @throws \Exception |
| 48 |
*/ |
| 49 |
public static function enqueue($responses) |
| 50 |
{ |
| 51 |
$data = []; |
| 52 |
foreach ((array) $responses as $response) { |
| 53 |
if (!($response instanceof ResponseInterface)) { |
| 54 |
throw new \Exception('Invalid response given.'); |
| 55 |
} |
| 56 |
$headers = array_map(function ($h) { |
| 57 |
return implode(' ,', $h); |
| 58 |
}, $response->getHeaders()); |
| 59 |
|
| 60 |
$data[] = [ |
| 61 |
'status' => (string) $response->getStatusCode(), |
| 62 |
'reason' => $response->getReasonPhrase(), |
| 63 |
'headers' => $headers, |
| 64 |
'body' => base64_encode((string) $response->getBody()) |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
self::getClient()->request('PUT', 'guzzle-server/responses', [ |
| 69 |
'json' => $data |
| 70 |
]); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get all of the received requests |
| 75 |
* |
| 76 |
* @return ResponseInterface[] |
| 77 |
* @throws \RuntimeException |
| 78 |
*/ |
| 79 |
public static function received() |
| 80 |
{ |
| 81 |
if (!self::$started) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
$response = self::getClient()->request('GET', 'guzzle-server/requests'); |
| 86 |
$data = json_decode($response->getBody(), true); |
| 87 |
|
| 88 |
return array_map( |
| 89 |
function ($message) { |
| 90 |
$uri = $message['uri']; |
| 91 |
if (isset($message['query_string'])) { |
| 92 |
$uri .= '?' . $message['query_string']; |
| 93 |
} |
| 94 |
$response = new Psr7\Request( |
| 95 |
$message['http_method'], |
| 96 |
$uri, |
| 97 |
$message['headers'], |
| 98 |
$message['body'], |
| 99 |
$message['version'] |
| 100 |
); |
| 101 |
return $response->withUri( |
| 102 |
$response->getUri() |
| 103 |
->withScheme('http') |
| 104 |
->withHost($response->getHeaderLine('host')) |
| 105 |
); |
| 106 |
}, |
| 107 |
$data |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Stop running the node.js server |
| 113 |
*/ |
| 114 |
public static function stop() |
| 115 |
{ |
| 116 |
if (self::$started) { |
| 117 |
self::getClient()->request('DELETE', 'guzzle-server'); |
| 118 |
} |
| 119 |
|
| 120 |
self::$started = false; |
| 121 |
} |
| 122 |
|
| 123 |
public static function wait($maxTries = 5) |
| 124 |
{ |
| 125 |
$tries = 0; |
| 126 |
while (!self::isListening() && ++$tries < $maxTries) { |
| 127 |
usleep(100000); |
| 128 |
} |
| 129 |
|
| 130 |
if (!self::isListening()) { |
| 131 |
throw new \RuntimeException('Unable to contact node.js server'); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public static function start() |
| 136 |
{ |
| 137 |
if (self::$started) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if (!self::isListening()) { |
| 142 |
exec('node ' . __DIR__ . '/server.js ' |
| 143 |
. self::$port . ' >> /tmp/server.log 2>&1 &'); |
| 144 |
self::wait(); |
| 145 |
} |
| 146 |
|
| 147 |
self::$started = true; |
| 148 |
} |
| 149 |
|
| 150 |
private static function isListening() |
| 151 |
{ |
| 152 |
try { |
| 153 |
self::getClient()->request('GET', 'guzzle-server/perf', [ |
| 154 |
'connect_timeout' => 5, |
| 155 |
'timeout' => 5 |
| 156 |
]); |
| 157 |
return true; |
| 158 |
} catch (\Exception $e) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
private static function getClient() |
| 164 |
{ |
| 165 |
if (!self::$client) { |
| 166 |
self::$client = new Client([ |
| 167 |
'base_uri' => self::$url, |
| 168 |
'sync' => true, |
| 169 |
]); |
| 170 |
} |
| 171 |
|
| 172 |
return self::$client; |
| 173 |
} |
| 174 |
} |
| 175 |
|