Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Dec 3, 2023
1 parent 297c11e commit efdd89e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions tests/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'Request.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'Response.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ObjectMapper.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'RequestMethod.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ResponseMessage.php';

require_once $rootDir.'tests'.$DS.'webfiori'.$DS.'tests'.$DS.'http'.$DS.'testServices'.$DS.'TestServiceObj.php';
require_once $rootDir.'tests'.$DS.'webfiori'.$DS.'tests'.$DS.'http'.$DS.'testServices'.$DS.'SampleServicesManager.php';
Expand Down
4 changes: 2 additions & 2 deletions tests/webfiori/tests/http/WebServicesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use PHPUnit\Framework\TestCase;
use webfiori\http\AbstractWebService;
use webfiori\http\Request;
use webfiori\http\ResponseMessages;
use webfiori\http\ResponseMessage;
use webfiori\http\WebServicesManager;
use webfiori\json\Json;
use webfiori\tests\http\testServices\NoAuthService;
Expand Down Expand Up @@ -395,7 +395,7 @@ public function testSumArray04() {
$_POST['numbers'] = '[1,2,"as",1.9,\'hello\',10]';
$_POST['pass'] = '1234';
$api = new SampleServicesManager();
ResponseMessages::set('401', 'Your password is inncorrect.');
ResponseMessage::set('401', 'Your password is inncorrect.');
$api->setOutputStream($this->outputStreamName);
$api->process();
$this->assertEquals('{"message":"Your password is inncorrect.","type":"error","http-code":401}', $api->readOutputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
*
* @author Ibrahim
*/
class ResponseMessages {
class ResponseMessage {
private $messages;
private static $inst;
/**
*
* @return ResponseMessages
* @return ResponseMessage
*/
private static function getInstance() : ResponseMessages {
private static function getInstance() : ResponseMessage {
if (self::$inst === null) {
self::$inst = new ResponseMessages();
self::$inst = new ResponseMessage();
}
return self::$inst;
}
Expand Down Expand Up @@ -67,7 +67,13 @@ public static function set(string $code, string $message) {
* return it. Other than that, the string '-' is returned.
*/
public static function get(string $code) : string {
return isset(self::getInstance()->messages[trim($code)]) ?? '-';
$tr = trim($code);

if (isset(self::getInstance()->messages[$tr])) {
return self::getInstance()->messages[$tr];
}

return '-';
}
private function __construct() {
$this->messages = [
Expand Down
18 changes: 9 additions & 9 deletions webfiori/http/WebServicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function addService(AbstractWebService $service) {
public function contentTypeNotSupported(string $cType = '') {
$j = new Json();
$j->add('request-content-type', $cType);
$this->sendResponse(ResponseMessages::get('415'), self::E, 415,$j);
$this->sendResponse(ResponseMessage::get('415'), self::E, 415,$j);
}
/**
* Returns the name of the service which is being called.
Expand Down Expand Up @@ -353,7 +353,7 @@ public function invParams() {
}
$i++;
}
$this->sendResponse(ResponseMessages::get('404-1').$val.'.', self::E, 404, new Json([
$this->sendResponse(ResponseMessage::get('404-1').$val.'.', self::E, 404, new Json([
'invalid' => $paramsNamesArr
]));
}
Expand Down Expand Up @@ -408,7 +408,7 @@ public function missingParams() {
}
$i++;
}
$this->sendResponse(ResponseMessages::get('404-2').$val.'.', self::E, 404, new Json([
$this->sendResponse(ResponseMessage::get('404-2').$val.'.', self::E, 404, new Json([
'missing' => $paramsNamesArr
]));
}
Expand All @@ -428,7 +428,7 @@ public function missingParams() {
* @since 1.3.1
*/
public function missingServiceName() {
$this->sendResponse(ResponseMessages::get('404-3'), self::E, 404);
$this->sendResponse(ResponseMessage::get('404-3'), self::E, 404);
}
/**
* Sends a response message to indicate that a user is not authorized call a
Expand All @@ -446,7 +446,7 @@ public function missingServiceName() {
* @since 1.0
*/
public function notAuth() {
$this->sendResponse(ResponseMessages::get('401'), self::E, 401);
$this->sendResponse(ResponseMessage::get('401'), self::E, 401);
}

/**
Expand Down Expand Up @@ -559,7 +559,7 @@ public function removeServices() {
* @since 1.0
*/
public function requestMethodNotAllowed() {
$this->sendResponse(ResponseMessages::get('405'), self::E, 405);
$this->sendResponse(ResponseMessage::get('405'), self::E, 405);
}
/**
* Sends Back a data using specific content type and specific response code.
Expand Down Expand Up @@ -664,7 +664,7 @@ public function sendResponse(string $message, string $type = '', int $code = 200
* @since 1.0
*/
public function serviceNotImplemented() {
$this->sendResponse(ResponseMessages::get('404-4'), self::E, 404);
$this->sendResponse(ResponseMessage::get('404-4'), self::E, 404);
}
/**
* Sends a response message to indicate that called web service is not supported by the API.
Expand All @@ -681,7 +681,7 @@ public function serviceNotImplemented() {
* @since 1.0
*/
public function serviceNotSupported() {
$this->sendResponse(ResponseMessages::get('404-5'), self::E, 404);
$this->sendResponse(ResponseMessage::get('404-5'), self::E, 404);
}
/**
* Sets the description of the web services set.
Expand Down Expand Up @@ -867,7 +867,7 @@ private function _checkAction(): bool {
return true;
}
} else {
$this->sendResponse(ResponseMessages::get('404-6'), self::E, 404);
$this->sendResponse(ResponseMessage::get('404-6'), self::E, 404);
}
} else {
$this->serviceNotSupported();
Expand Down

0 comments on commit efdd89e

Please sign in to comment.