| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace Test\Michel\HttpClient;
- use Exception;
- use LogicException;
- use Michel\HttpClient\HttpClient;
- use Michel\UniTester\TestCase;
- class HttpClientTest extends TestCase
- {
- const URL = 'http://localhost:4245';
- protected static ?string $serverProcess = null;
- protected function setUp(): void
- {
- $fileToRun = __DIR__ . DIRECTORY_SEPARATOR . 'test_server.php';
- $command = sprintf('php -S %s %s > /dev/null 2>&1 & echo $!;', str_replace('http://', '', self::URL), $fileToRun);
- self::$serverProcess = exec($command);
- if (empty(self::$serverProcess) || !is_numeric(self::$serverProcess)) {
- throw new Exception('Could not start test server');
- }
- $this->log(sprintf('Test web server started on %s', self::URL));
- sleep(1);
- }
- protected function tearDown(): void
- {
- if (is_numeric(self::$serverProcess)) {
- exec('kill ' . self::$serverProcess);
- }
- }
- protected function execute(): void
- {
- $this->testGetRequest();
- $this->testGetWithQueryRequest();
- $this->testPostJsonRequest();
- $this->testPostFormRequest();
- $this->testPostEmptyFormRequest();
- $this->testWrongOptions();
- $this->testWrongOptions2();
- $this->testWrongOptions3();
- $this->testWrongMethod();
- $this->testWrongUrl();
- }
- public function testGetRequest()
- {
- $response = http_client(
- ['base_url' => self::URL, 'headers' => ['Authorization' => 'Bearer secret_token']],
- function ($info) {
- $this->assertEquals( 'GET', $info['request']['method']);
- $this->assertEquals( 'Bearer secret_token', $info['request']['headers']['Authorization']);
- $this->assertEquals( '{"message":"GET request received"}', $info['response']['body']);
- }
- )->get('/api/data');
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertNotEmpty($response->getBody());
- }
- public function testGetWithQueryRequest()
- {
- $client = new HttpClient(['base_url' => self::URL, 'headers' => ['Authorization' => 'Bearer secret_token']]);
- $response = $client->get('/api/search', [
- 'name' => 'foo',
- ]);
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertNotEmpty($response->getBody());
- $data = $response->bodyToArray();
- $this->assertEquals('foo', $data['name']);
- $this->assertEquals(1, $data['page']);
- $this->assertEquals(10, $data['limit']);
- $response = $client->get('/api/search', [
- 'name' => 'foo',
- 'page' => 10,
- 'limit' => 100
- ]);
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertNotEmpty($response->getBody());
- $data = $response->bodyToArray();
- $this->assertEquals('foo', $data['name']);
- $this->assertEquals(10, $data['page']);
- $this->assertEquals(100, $data['limit']);
- }
- public function testPostJsonRequest()
- {
- $dataToPost = [
- 'title' => 'foo',
- 'body' => 'bar',
- 'userId' => 1
- ];
- $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
- $response = $client->post(self::URL . '/api/post/data', [
- 'title' => 'foo',
- 'body' => 'bar',
- 'userId' => 1
- ], true);
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($dataToPost, $response->bodyToArray());
- }
- public function testPostFormRequest()
- {
- $dataToPost = [
- 'title' => 'foo',
- 'body' => 'bar',
- 'userId' => 1
- ];
- $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
- $response = $client->post(self::URL . '/api/post/data/form', $dataToPost);
- $this->assertEquals(200, $response->getStatusCode());
- $this->assertEquals($dataToPost, $response->bodyToArray());
- }
- public function testPostEmptyFormRequest()
- {
- $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
- $response = $client->post(self::URL . '/api/post/data/form', []);
- $this->assertEquals(400, $response->getStatusCode());
- }
- public function testWrongOptions()
- {
- $this->expectException(LogicException::class, function () {
- new HttpClient(['headers' => 'string']);
- });
- }
- public function testWrongOptions2()
- {
- $this->expectException(LogicException::class, function () {
- new HttpClient(['options_not_supported' => 'value']);
- });
- }
- public function testWrongOptions3()
- {
- $this->expectException(LogicException::class, function () {
- new HttpClient(['timeout' => 'string']);
- });
- }
- public function testWrongMethod()
- {
- $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
- $this->expectException(LogicException::class, function () use($client) {
- $client->fetch(self::URL . '/api/data', ['method' => 'WRONG']);
- });
- }
- public function testWrongUrl()
- {
- $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]);
- $this->expectException(\InvalidArgumentException::class, function () use($client) {
- $client->fetch('WRONG_URL');
- });
- }
- }
|