HttpClientTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Test\Michel\HttpClient;
  3. use Exception;
  4. use LogicException;
  5. use Michel\HttpClient\HttpClient;
  6. use Michel\UniTester\TestCase;
  7. class HttpClientTest extends TestCase
  8. {
  9. const URL = 'http://localhost:4245';
  10. protected static ?string $serverProcess = null;
  11. protected function setUp(): void
  12. {
  13. $fileToRun = __DIR__ . DIRECTORY_SEPARATOR . 'test_server.php';
  14. $command = sprintf('php -S %s %s > /dev/null 2>&1 & echo $!;', str_replace('http://', '', self::URL), $fileToRun);
  15. self::$serverProcess = exec($command);
  16. if (empty(self::$serverProcess) || !is_numeric(self::$serverProcess)) {
  17. throw new Exception('Could not start test server');
  18. }
  19. $this->log(sprintf('Test web server started on %s', self::URL));
  20. sleep(1);
  21. }
  22. protected function tearDown(): void
  23. {
  24. if (is_numeric(self::$serverProcess)) {
  25. exec('kill ' . self::$serverProcess);
  26. }
  27. }
  28. protected function execute(): void
  29. {
  30. $this->testGetRequest();
  31. $this->testGetWithQueryRequest();
  32. $this->testPostJsonRequest();
  33. $this->testPostFormRequest();
  34. $this->testPostEmptyFormRequest();
  35. $this->testWrongOptions();
  36. $this->testWrongOptions2();
  37. $this->testWrongOptions3();
  38. $this->testWrongMethod();
  39. $this->testWrongUrl();
  40. }
  41. public function testGetRequest()
  42. {
  43. $response = http_client(
  44. ['base_url' => self::URL, 'headers' => ['Authorization' => 'Bearer secret_token']],
  45. function ($info) {
  46. $this->assertEquals( 'GET', $info['request']['method']);
  47. $this->assertEquals( 'Bearer secret_token', $info['request']['headers']['Authorization']);
  48. $this->assertEquals( '{"message":"GET request received"}', $info['response']['body']);
  49. }
  50. )->get('/api/data');
  51. $this->assertEquals(200, $response->getStatusCode());
  52. $this->assertNotEmpty($response->getBody());
  53. }
  54. public function testGetWithQueryRequest()
  55. {
  56. $client = new HttpClient(['base_url' => self::URL, 'headers' => ['Authorization' => 'Bearer secret_token']]);
  57. $response = $client->get('/api/search', [
  58. 'name' => 'foo',
  59. ]);
  60. $this->assertEquals(200, $response->getStatusCode());
  61. $this->assertNotEmpty($response->getBody());
  62. $data = $response->bodyToArray();
  63. $this->assertEquals('foo', $data['name']);
  64. $this->assertEquals(1, $data['page']);
  65. $this->assertEquals(10, $data['limit']);
  66. $response = $client->get('/api/search', [
  67. 'name' => 'foo',
  68. 'page' => 10,
  69. 'limit' => 100
  70. ]);
  71. $this->assertEquals(200, $response->getStatusCode());
  72. $this->assertNotEmpty($response->getBody());
  73. $data = $response->bodyToArray();
  74. $this->assertEquals('foo', $data['name']);
  75. $this->assertEquals(10, $data['page']);
  76. $this->assertEquals(100, $data['limit']);
  77. }
  78. public function testPostJsonRequest()
  79. {
  80. $dataToPost = [
  81. 'title' => 'foo',
  82. 'body' => 'bar',
  83. 'userId' => 1
  84. ];
  85. $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
  86. $response = $client->post(self::URL . '/api/post/data', [
  87. 'title' => 'foo',
  88. 'body' => 'bar',
  89. 'userId' => 1
  90. ], true);
  91. $this->assertEquals(200, $response->getStatusCode());
  92. $this->assertEquals($dataToPost, $response->bodyToArray());
  93. }
  94. public function testPostFormRequest()
  95. {
  96. $dataToPost = [
  97. 'title' => 'foo',
  98. 'body' => 'bar',
  99. 'userId' => 1
  100. ];
  101. $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
  102. $response = $client->post(self::URL . '/api/post/data/form', $dataToPost);
  103. $this->assertEquals(200, $response->getStatusCode());
  104. $this->assertEquals($dataToPost, $response->bodyToArray());
  105. }
  106. public function testPostEmptyFormRequest()
  107. {
  108. $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
  109. $response = $client->post(self::URL . '/api/post/data/form', []);
  110. $this->assertEquals(400, $response->getStatusCode());
  111. }
  112. public function testWrongOptions()
  113. {
  114. $this->expectException(LogicException::class, function () {
  115. new HttpClient(['headers' => 'string']);
  116. });
  117. }
  118. public function testWrongOptions2()
  119. {
  120. $this->expectException(LogicException::class, function () {
  121. new HttpClient(['options_not_supported' => 'value']);
  122. });
  123. }
  124. public function testWrongOptions3()
  125. {
  126. $this->expectException(LogicException::class, function () {
  127. new HttpClient(['timeout' => 'string']);
  128. });
  129. }
  130. public function testWrongMethod()
  131. {
  132. $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
  133. $this->expectException(LogicException::class, function () use($client) {
  134. $client->fetch(self::URL . '/api/data', ['method' => 'WRONG']);
  135. });
  136. }
  137. public function testWrongUrl()
  138. {
  139. $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
  140. $this->expectException(\InvalidArgumentException::class, function () use($client) {
  141. $client->fetch('WRONG_URL');
  142. });
  143. }
  144. }