rpcii-php/tests/StreamHttpTest.php

59 lines
2.2 KiB
PHP

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