From 00c330342d14360941872e5b30317e6882a2d277 Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 13 Aug 2024 18:54:24 +0000 Subject: [PATCH] Added test for HMAC and cURL. --- src/AiwassMsgPack.php | 3 ++- src/RpcClient.php | 1 + tests/CurlHttpTest.php | 49 ++++++++++++++++++++++++++++++++++ tests/HmacVerificationTest.php | 27 +++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/CurlHttpTest.php create mode 100644 tests/HmacVerificationTest.php diff --git a/src/AiwassMsgPack.php b/src/AiwassMsgPack.php index 0d18b72..0f13189 100644 --- a/src/AiwassMsgPack.php +++ b/src/AiwassMsgPack.php @@ -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 { diff --git a/src/RpcClient.php b/src/RpcClient.php index 0bc5efd..6ce3160 100644 --- a/src/RpcClient.php +++ b/src/RpcClient.php @@ -6,6 +6,7 @@ namespace Aiwass; use InvalidArgumentException; + /** * Implemens an RPC client. */ diff --git a/tests/CurlHttpTest.php b/tests/CurlHttpTest.php new file mode 100644 index 0000000..8997a6e --- /dev/null +++ b/tests/CurlHttpTest.php @@ -0,0 +1,49 @@ +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']); + } +} diff --git a/tests/HmacVerificationTest.php b/tests/HmacVerificationTest.php new file mode 100644 index 0000000..e1b1ef5 --- /dev/null +++ b/tests/HmacVerificationTest.php @@ -0,0 +1,27 @@ + '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)); + } +}