Server.php 5.0 KB

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