rpcii-php/tests/CurlHttpTest.php

83 lines
3 KiB
PHP

<?php
// CurlHttpTest.php
// Created: 2024-08-13
// Updated: 2025-01-17
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use RPCii\Client\CurlHttpRequest;
#[CoversClass(CurlHttpRequest::class)]
final class CurlHttpTest extends TestCase {
public function testGetRequest(): void {
$request = new CurlHttpRequest;
$request->post = false;
$request->url = 'https://httpbin.org/get?alreadyhere=true';
$request->params = 'soap=beans';
$request->setHeader('X-Test', 'teste');
$response = $request->execute();
$this->assertArrayHasKey('status', $response);
$this->assertIsInt($response['status']);
$this->assertEquals(200, $response['status']);
$this->assertArrayHasKey('format', $response);
$this->assertIsInt($response['format']);
$this->assertEquals(1, $response['format']);
$this->assertArrayHasKey('body', $response);
$this->assertIsString($response['body']);
$body = json_decode($response['body'], true);
$this->assertIsArray($body);
$this->assertArrayHasKey('headers', $body);
$this->assertIsArray($body['headers']);
$this->assertArrayHasKey('X-Test', $body['headers']);
$this->assertEquals('teste', $body['headers']['X-Test']);
$this->assertArrayHasKey('args', $body);
$this->assertIsArray($body['args']);
$this->assertArrayHasKey('alreadyhere', $body['args']);
$this->assertEquals('true', $body['args']['alreadyhere']);
$this->assertArrayHasKey('soap', $body['args']);
$this->assertEquals('beans', $body['args']['soap']);
}
public function testPostRequest(): void {
$request = new CurlHttpRequest;
$request->post = true;
$request->url = 'https://httpbin.org/post';
$request->params = 'windows=xp&macos=leopard';
$request->setHeader('X-Meow', 'soap');
$response = $request->execute();
$this->assertArrayHasKey('status', $response);
$this->assertIsInt($response['status']);
$this->assertEquals(200, $response['status']);
$this->assertArrayHasKey('format', $response);
$this->assertIsInt($response['format']);
$this->assertEquals(1, $response['format']);
$this->assertArrayHasKey('body', $response);
$this->assertIsString($response['body']);
$body = json_decode($response['body'], true);
$this->assertIsArray($body);
$this->assertArrayHasKey('headers', $body);
$this->assertIsArray($body['headers']);
$this->assertArrayHasKey('X-Meow', $body['headers']);
$this->assertEquals('soap', $body['headers']['X-Meow']);
$this->assertArrayHasKey('form', $body);
$this->assertIsArray($body['form']);
$this->assertArrayHasKey('windows', $body['form']);
$this->assertEquals('xp', $body['form']['windows']);
$this->assertArrayHasKey('macos', $body['form']);
$this->assertEquals('leopard', $body['form']['macos']);
}
}