From 66b48b2a5320393cff1e8cf0f2f7e21f98ec68a0 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Sun, 1 Sep 2024 17:18:51 +0300 Subject: [PATCH 1/2] refactor: Parse Output as Json or Return a String --- webfiori/http/APITestCase.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/webfiori/http/APITestCase.php b/webfiori/http/APITestCase.php index de9defc..5b39f24 100644 --- a/webfiori/http/APITestCase.php +++ b/webfiori/http/APITestCase.php @@ -10,6 +10,8 @@ namespace webfiori\http; use PHPUnit\Framework\TestCase; +use webfiori\json\Json; +use webfiori\json\JsonException; /** * A helper class which is used to implement test cases for API calls. * @@ -101,8 +103,15 @@ public function callEndpoint(WebServicesManager $manager, string $requestMethod, $retVal = $manager->readOutputStream(); unlink(self::OUTPUT_STREAM); - - return $retVal; + + try { + $json = Json::decode($retVal); + $json->setIsFormatted(true); + return $json.''; + } catch (JsonException $ex) { + return $retVal; + } + } private function parseVal($val) { $type = gettype($val); From 30e1ef959429fa6381bcdf69e1f14c35c42c3f14 Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Sun, 1 Sep 2024 17:28:25 +0300 Subject: [PATCH 2/2] test: Fixed Test Cases --- .../tests/http/WebServicesManagerTest.php | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/tests/webfiori/tests/http/WebServicesManagerTest.php b/tests/webfiori/tests/http/WebServicesManagerTest.php index 97189af..9c22e0c 100644 --- a/tests/webfiori/tests/http/WebServicesManagerTest.php +++ b/tests/webfiori/tests/http/WebServicesManagerTest.php @@ -20,7 +20,10 @@ class WebServicesManagerTest extends APITestCase { public function test00() { $manager = new WebServicesManager(); $manager->addService(new NoAuthService()); - $this->assertEquals('{"message":"You are authorized.","http-code":200}', $this->getRequest($manager, 'ok-service')); + $this->assertEquals('{'.self::NL + . ' "message":"You are authorized.",'.self::NL + . ' "http-code":200'.self::NL + . '}', $this->getRequest($manager, 'ok-service')); return $manager; } /** @@ -30,7 +33,11 @@ public function test00() { public function test01() { $manager = new WebServicesManager(); $manager->addService(new NotImplService()); - $this->assertEquals('{"message":"Service not implemented.","type":"error","http-code":404}', $this->postRequest($manager, 'not-implemented')); + $this->assertEquals('{'.self::NL + . ' "message":"Service not implemented.",'.self::NL + . ' "type":"error",'.self::NL + . ' "http-code":404'.self::NL + . '}', $this->postRequest($manager, 'not-implemented')); return $manager; } /** @@ -166,7 +173,11 @@ public function testConstructor00() { */ public function testDoNothing00() { $api = new SampleServicesManager(); - $this->assertEquals('{"message":"Service not supported.","type":"error","http-code":404}', $this->getRequest($api, 'do-nothen')); + $this->assertEquals('{'.self::NL + . ' "message":"Service not supported.",'.self::NL + . ' "type":"error",'.self::NL + . ' "http-code":404'.self::NL + . '}', $this->getRequest($api, 'do-nothen')); } /** * @depends testSumTwoIntegers05 @@ -185,7 +196,11 @@ public function testGetNonFiltered00($api) { */ public function testGetUser00() { $api = new SampleServicesManager(); - $this->assertEquals('{"message":"Method Not Allowed.","type":"error","http-code":405}', $this->getRequest($api, 'get-user-profile', [ + $this->assertEquals('{'.self::NL + . ' "message":"Method Not Allowed.",'.self::NL + . ' "type":"error",'.self::NL + . ' "http-code":405'.self::NL + . '}', $this->getRequest($api, 'get-user-profile', [ 'user-id' => -9, 'pass' => '123' ])); @@ -196,7 +211,11 @@ public function testGetUser00() { public function testGetUser01() { $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'; $api = new SampleServicesManager(); - $this->assertEquals('{"message":"Database Error.","type":"error","http-code":500}', $this->postRequest($api, 'get-user-profile', [ + $this->assertEquals('{'.self::NL + . ' "message":"Database Error.",'.self::NL + . ' "type":"error",'.self::NL + . ' "http-code":500'.self::NL + . '}', $this->postRequest($api, 'get-user-profile', [ 'user-id' => -9, 'pass' => '123' ])); @@ -208,7 +227,10 @@ public function testGetUser01() { public function testGetUser02() { $api = new SampleServicesManager(); - $this->assertEquals('{"user-name":"Ibrahim","bio":"A software engineer who is ready to help anyone in need."}', $this->postRequest($api, 'get-user-profile', [ + $this->assertEquals('{'.self::NL + . ' "user-name":"Ibrahim",'.self::NL + . ' "bio":"A software engineer who is ready to help anyone in need."'.self::NL + . '}', $this->postRequest($api, 'get-user-profile', [ 'user-id' => '99', 'pass' => '123' ]));