HttpConnectionTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Cookie\CookieJar;
  4. use GuzzleHttp\Psr7\Utils;
  5. use Symfony\Component\Process\PhpProcess;
  6. $process = null;
  7. beforeAll(function () use (&$process) {
  8. $process = new PhpProcess(file_get_contents(__DIR__ . '/Stub/HttpServer.php'));
  9. $process->start();
  10. usleep(250000);
  11. });
  12. afterAll(function () use (&$process) {
  13. echo $process->getOutput();
  14. $process->stop();
  15. });
  16. it('tests http connection', function () {
  17. $client = new Client([
  18. 'base_uri' => 'http://127.0.0.1:8080',
  19. 'cookies' => true,
  20. 'http_errors' => false,
  21. ]);
  22. $response = $client->get('/');
  23. expect($response->getStatusCode())
  24. ->toBe(200)
  25. ->and($response->getHeaderLine('Server'))
  26. ->tobe('workerman')
  27. ->and($response->getHeaderLine('Content-Length'))
  28. ->tobe('12')
  29. ->and($response->getBody()->getContents())
  30. ->toBe('Hello Chance');
  31. $data = [
  32. 'foo' => 'bar',
  33. 'key' => ['hello', 'chance']
  34. ];
  35. $response = $client->get('/get', [
  36. 'query' => $data
  37. ]);
  38. expect($response->getBody()->getContents())
  39. ->toBeJson()
  40. ->json()
  41. ->toBe($data);
  42. $response = $client->post('/post', [
  43. 'json' => $data
  44. ]);
  45. expect($response->getBody()->getContents())
  46. ->toBeJson()
  47. ->json()
  48. ->toBe($data);
  49. $response = $client->post('/header', [
  50. 'headers' => [
  51. 'foo' => 'bar'
  52. ]
  53. ]);
  54. expect($response->getBody()->getContents())
  55. ->toBe('bar');
  56. $cookie = new CookieJar();
  57. $client->get('/setSession', [
  58. 'cookies' => $cookie
  59. ]);
  60. $response = $client->get('/session', [
  61. 'cookies' => $cookie
  62. ]);
  63. expect($response->getBody()->getContents())
  64. ->toBe('bar');
  65. $response = $client->get('/session', [
  66. 'cookies' => $cookie
  67. ]);
  68. expect($response->getBody()->getContents())
  69. ->toBe('');
  70. $response = $client->get('/sse', [
  71. 'stream' => true,
  72. ]);
  73. $stream = $response->getBody();
  74. $i = 0;
  75. while (!$stream->eof()) {
  76. if ($i >= 5) {
  77. expect($stream->read(1024))->toBeEmpty();
  78. continue;
  79. }
  80. $i++;
  81. expect($stream->read(1024))->toBe("data: hello$i\n\n");
  82. }
  83. $file = Utils::tryFopen(__DIR__ . '/Stub/HttpServer.php', 'r');
  84. $response = $client->post('/file', [
  85. 'multipart' => [
  86. [
  87. 'name' => 'file',
  88. 'contents' => $file
  89. ]
  90. ]
  91. ]);
  92. expect($response->getBody()->getContents())
  93. ->toBeJson()
  94. ->json()
  95. ->toMatchArray([
  96. 'name' => 'HttpServer.php',
  97. 'error' => 0,
  98. ]);
  99. $response = $client->get('/404');
  100. expect($response->getStatusCode())
  101. ->toBe(404)
  102. ->and($response->getBody()->getContents())
  103. ->toBe('404 not found');
  104. });