58 lines
2.5 KiB
PHP
58 lines
2.5 KiB
PHP
<?php
|
|
// Define app root for direct CLI execution
|
|
if (!defined('WD')) {
|
|
define('WD', dirname(__DIR__, 2));
|
|
}
|
|
|
|
require_once WD . '/classes/bird.php';
|
|
|
|
function ok($message): void { echo "\n\033[32m✔ $message\033[0m\n"; }
|
|
function fail($message): void { echo "\n\033[31m✖ $message\033[0m\n"; }
|
|
|
|
class FakeBoolVar { private bool $v; public function __construct(bool $v){$this->v=$v;} public function isTrue(): bool { return $this->v; } }
|
|
class FakeStringVar { private string $v; public function __construct(string $v){$this->v=$v;} public function getVariableValue(): string { return $this->v; } }
|
|
class FakeBirdConfig { public $enabled; public $api_key; public $server_url; }
|
|
|
|
class InspectableBird extends \classes\bird
|
|
{
|
|
public array $last = [];
|
|
|
|
public function __construct()
|
|
{
|
|
// Inject fake config to avoid DB
|
|
$cfg = new FakeBirdConfig();
|
|
$cfg->enabled = new FakeBoolVar(true);
|
|
$cfg->api_key = new FakeStringVar('secret_token');
|
|
$cfg->server_url = new FakeStringVar('https://api.bird.com');
|
|
$this->config = $cfg;
|
|
}
|
|
|
|
protected function doHttpRequest(string $method, string $url, array $headers, string $body = ''): array
|
|
{
|
|
$this->last = compact('method', 'url', 'headers', 'body');
|
|
// Return 200 OK with empty JSON object
|
|
return ['status_code' => 200, 'body' => '{}'];
|
|
}
|
|
}
|
|
|
|
$bird = new InspectableBird();
|
|
$payload = ['alpha' => 1, 'beta' => 'two'];
|
|
$bird->sendPostRequest('/v1/demo', $payload);
|
|
|
|
$expectedUrl = 'https://api.bird.com/v1/demo';
|
|
if (($bird->last['url'] ?? '') === $expectedUrl) { ok('URL is correctly composed with base + endpoint'); } else { fail('URL should be ' . $expectedUrl . ' but was ' . ($bird->last['url'] ?? '<none>')); }
|
|
|
|
$headers = $bird->last['headers'] ?? [];
|
|
$hasAuth = false; $hasJson = false;
|
|
foreach ($headers as $h) {
|
|
if (stripos($h, 'authorization:') === 0 && str_contains($h, 'AccessKey secret_token')) { $hasAuth = true; }
|
|
if (strcasecmp($h, 'Content-Type: application/json') === 0) { $hasJson = true; }
|
|
}
|
|
if ($hasAuth) { ok('Authorization header includes AccessKey token'); } else { fail('Missing or invalid Authorization header'); }
|
|
if ($hasJson) { ok('Content-Type header is application/json'); } else { fail('Missing Content-Type: application/json header'); }
|
|
|
|
$expectedBody = json_encode($payload);
|
|
if (($bird->last['body'] ?? '') === $expectedBody) { ok('Request body is JSON-encoded as expected'); } else { fail('Request body JSON mismatch'); }
|
|
|
|
echo "\nBirdHttpHeadersTest completed.\n";
|