Added test for HMAC and cURL.

This commit is contained in:
flash 2024-08-13 18:54:24 +00:00
parent 05e707be51
commit 00c330342d
4 changed files with 79 additions and 1 deletions

View file

@ -6,6 +6,7 @@
namespace Aiwass;
use MessagePack;
/**
* Internal container for MessagePack class.
*
@ -20,7 +21,7 @@ final class AiwassMsgPack {
}
public static function decode(string $value): mixed {
return @self::$msgpack->unpack($value, null);
return @self::$msgpack->unpack($value); // @phpstan-ignore-line: PHPstan claims second arg is required, it is not.
}
public static function init(): void {

View file

@ -6,6 +6,7 @@
namespace Aiwass;
use InvalidArgumentException;
/**
* Implemens an RPC client.
*/

49
tests/CurlHttpTest.php Normal file
View file

@ -0,0 +1,49 @@
<?php
// CurlHttpTest.php
// Created: 2024-08-13
// Updated: 2024-08-13
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Aiwass\CurlHttpRequest;
#[CoversClass(CurlHttpRequest::class)]
final class CurlHttpTest extends TestCase {
public function testGetRequest(): void {
$request = new CurlHttpRequest;
$request->setPost(false);
$request->setUrl('https://httpbin.org/get?alreadyhere=true');
$request->setHeader('X-Test', 'teste');
$request->setParams('soap=beans');
$response = json_decode($request->execute(), true);
$this->assertTrue(array_key_exists('X-Test', $response['headers']));
$this->assertEquals('teste', $response['headers']['X-Test']);
$this->assertTrue(array_key_exists('alreadyhere', $response['args']));
$this->assertEquals('true', $response['args']['alreadyhere']);
$this->assertTrue(array_key_exists('soap', $response['args']));
$this->assertEquals('beans', $response['args']['soap']);
}
public function testPostRequest(): void {
$request = new CurlHttpRequest;
$request->setPost(true);
$request->setUrl('https://httpbin.org/post');
$request->setHeader('X-Meow', 'soap');
$request->setParams('windows=xp&macos=leopard');
$response = json_decode($request->execute(), true);
$this->assertTrue(array_key_exists('X-Meow', $response['headers']));
$this->assertEquals('soap', $response['headers']['X-Meow']);
$this->assertTrue(array_key_exists('windows', $response['form']));
$this->assertEquals('xp', $response['form']['windows']);
$this->assertTrue(array_key_exists('macos', $response['form']));
$this->assertEquals('leopard', $response['form']['macos']);
}
}

View file

@ -0,0 +1,27 @@
<?php
// HmacVerificationTest.php
// Created: 2024-08-13
// Updated: 2024-08-13
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Aiwass\HmacVerificationProvider;
#[CoversClass(HmacVerificationProvider::class)]
final class HmacVerificationTest extends TestCase {
public function testProcedureEnforce(): void {
$provider = new HmacVerificationProvider(fn() => 'meow');
$action = 'test';
$params = 'meow=cool&the=bean';
$queryToken = $provider->sign(false, $action, $params);
$this->assertTrue($provider->verify($queryToken, false, $action, $params));
$this->assertFalse($provider->verify($queryToken, true, $action, $params));
$procedureToken = $provider->sign(true, $action, $params);
$this->assertTrue($provider->verify($procedureToken, true, $action, $params));
$this->assertFalse($provider->verify($procedureToken, false, $action, $params));
}
}