sls/tests/Unit/MeteredIceCandidateProviderTest.php
kushal.saha 3aca6d90f3 feat(webrtc): implement IceCandidateProviderInterface with Metered and ConvexSol drivers
- Add Metered and ConvexSol (CoTURN) provider drivers with response validation and caching
- Add IceCandidateManager with automatic failover and structured logging
- Add IceCandidateProviderInterface and DTOs conforming to RTCIceServer format
2026-08-26 12:44:41 +00:00

80 lines
2.3 KiB
PHP

<?php
namespace Tests\Unit;
use App\Services\IceCandidate\Providers\MeteredIceCandidateProvider;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use RuntimeException;
use Tests\TestCase;
class MeteredIceCandidateProviderTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Cache::flush();
}
public function test_fetches_and_parses_metered_ice_servers_successfully(): void
{
$mockUrl = 'https://mock.metered.live/api/v1/turn/credentials';
$apiKey = 'test-metered-key';
Http::fake([
$mockUrl.'*' => Http::response([
[
'urls' => 'stun:stun.relay.metered.ca:80',
],
[
'urls' => 'turn:standard.relay.metered.ca:80',
'username' => 'metered_usr',
'credential' => 'metered_pwd',
],
], 200),
]);
$provider = new MeteredIceCandidateProvider([
'url' => $mockUrl,
'key' => $apiKey,
'timeout' => 3,
'cache_ttl' => 60,
]);
$candidates = $provider->getIceCandidates();
$this->assertCount(2, $candidates);
$array = $candidates->toArray();
$this->assertEquals('stun:stun.relay.metered.ca:80', $array[0]['urls']);
$this->assertEquals('metered_usr', $array[1]['username']);
$this->assertEquals('metered_pwd', $array[1]['credential']);
// Verify result is cached (subsequent call does not hit HTTP)
Http::fake([
$mockUrl.'*' => Http::response([], 500),
]);
$cachedCandidates = $provider->getIceCandidates();
$this->assertCount(2, $cachedCandidates);
}
public function test_throws_exception_on_metered_http_error(): void
{
$mockUrl = 'https://mock.metered.live/api/v1/turn/credentials';
Http::fake([
$mockUrl.'*' => Http::response('Server Error', 500),
]);
$provider = new MeteredIceCandidateProvider([
'url' => $mockUrl,
'key' => 'key',
]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Metered API Error: 500');
$provider->getIceCandidates();
}
}