Skip to content

Commit

Permalink
Merge pull request #57 from WebFiori/dev
Browse files Browse the repository at this point in the history
refactor: Parse Output as Json or Return a String
  • Loading branch information
usernane authored Sep 1, 2024
2 parents df496d7 + 30e1ef9 commit 8e3fa63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
34 changes: 28 additions & 6 deletions tests/webfiori/tests/http/WebServicesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
Expand All @@ -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;
}
/**
Expand Down Expand Up @@ -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
Expand All @@ -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'
]));
Expand All @@ -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'
]));
Expand All @@ -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'
]));
Expand Down
13 changes: 11 additions & 2 deletions webfiori/http/APITestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8e3fa63

Please sign in to comment.