Bicycle/tests/Unit/HttpClientRequestTest.php
Egor Isaev b516ca07dc Initial commit: Bicycle PHP MVC micro-framework
Core MVC, HTTP client, Session, Cookie, Config, HTTPException — 111 PHPUnit tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 10:04:19 +03:00

88 lines
2.7 KiB
PHP

<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use System\Classes\HTTP\Client\Request;
class HttpClientRequestTest extends TestCase
{
public function testFactoryCreatesInstance(): void
{
$req = Request::factory('https://example.com/api');
$this->assertInstanceOf(Request::class, $req);
$this->assertSame('https://example.com/api', $req->getUrl());
}
public function testDefaultMethodIsGet(): void
{
$req = Request::factory('https://example.com');
$this->assertSame('GET', $req->getMethod());
}
public function testMethodSetter(): void
{
$req = Request::factory('https://example.com')->method('post');
$this->assertSame('POST', $req->getMethod());
}
public function testHeaderSetter(): void
{
$req = Request::factory('https://example.com')
->header('Authorization', 'Bearer token123');
$this->assertSame(['Authorization' => 'Bearer token123'], $req->getHeaders());
}
public function testBodySetter(): void
{
$req = Request::factory('https://example.com')->body('raw body');
$this->assertSame('raw body', $req->getBody());
}
public function testDefaultBodyIsNull(): void
{
$req = Request::factory('https://example.com');
$this->assertNull($req->getBody());
}
public function testJsonSetsBodyAndHeaders(): void
{
$req = Request::factory('https://example.com')->json(['key' => 'value']);
$this->assertStringContainsString('"key":"value"', $req->getBody());
$this->assertSame('application/json', $req->getHeaders()['Content-Type']);
$this->assertSame('application/json', $req->getHeaders()['Accept']);
}
public function testJsonEncodesUnicode(): void
{
$req = Request::factory('https://example.com')->json(['name' => 'Иван']);
$this->assertStringContainsString('Иван', $req->getBody());
}
public function testTimeoutDefault(): void
{
$req = Request::factory('https://example.com');
$this->assertSame(30, $req->getTimeout());
}
public function testTimeoutSetter(): void
{
$req = Request::factory('https://example.com')->timeout(10);
$this->assertSame(10, $req->getTimeout());
}
public function testFluentInterface(): void
{
$req = Request::factory('https://example.com')
->method('POST')
->header('X-Custom', 'value')
->timeout(5)
->body('data');
$this->assertSame('POST', $req->getMethod());
$this->assertSame('value', $req->getHeaders()['X-Custom']);
$this->assertSame(5, $req->getTimeout());
$this->assertSame('data', $req->getBody());
}
}