Skip to content

Commit

Permalink
Merge pull request #25 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix to Small bug + Improvements
  • Loading branch information
usernane authored Jun 25, 2022
2 parents f1d18c2 + 84c2e8e commit 932c478
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 73 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright 2019 Ibrahim BinAlshikh, restEasy library.
Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ It includes inputs feltering and data validation in addion to creating user-defi
<a href="https://sonarcloud.io/dashboard?id=WebFiori_http">
<img src="https://sonarcloud.io/api/project_badges/measure?project=WebFiori_http&metric=alert_status" />
</a>
<a href="https://github.com/WebFiori/restEasy/releases">
<a href="https://github.com/WebFiori/http/releases">
<img src="https://img.shields.io/github/release/WebFiori/http.svg?label=latest" />
</a>
<a href="https://packagist.org/packages/webfiori/http">
Expand Down
27 changes: 24 additions & 3 deletions tests/webfiori/tests/http/HttpCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testConstructor01() {
$this->assertEquals('/b/a/m', $cookie->getPath());
$this->assertEquals('', $cookie->getLifetime());
$cookie->setExpires(10);
$expires = date(DATE_COOKIE, time() + 10);
$expires = date(DATE_COOKIE, time() + 10*60);
$this->assertTrue($cookie->isPersistent());
$this->assertEquals($expires, $cookie->getLifetime());
$this->assertEquals('Lax', $cookie->getSameSite());
Expand All @@ -52,8 +52,8 @@ public function testConstructor01() {
$this->assertTrue($cookie->isHttpOnly());
$this->assertTrue($cookie->isSecure());
$cookie->setExpires(-1);
$expires = date(DATE_COOKIE, time() - 1);
$this->assertEquals(time() - 1, $cookie->getExpires());
$expires = date(DATE_COOKIE, time() - 1*60);
$this->assertEquals(time() - 1*60, $cookie->getExpires());
$this->assertEquals($expires, $cookie->getLifetime());
$this->assertEquals('super=cool; expires='.$expires.'; path=/b/a/m; Secure; HttpOnly; SameSite=None', $cookie->getHeaderString());
$cookie->setExpires(0);
Expand Down Expand Up @@ -87,4 +87,25 @@ public function testConstructor03() {
$cookie->kill();
$this->assertEquals(date(DATE_COOKIE, time() - 60*60*24), $cookie->getLifetime());
}
/**
* @test
*/
public function testRemainingTime00() {
$cookie = new HttpCookie();
$this->assertEquals(0, $cookie->getRemainingTime());
$cookie->setExpires(1);
$this->assertEquals(60, $cookie->getRemainingTime());
sleep(3);
$this->assertEquals(57, $cookie->getRemainingTime());
}
/**
* @test
*/
public function testRemainingTime01() {
$cookie = new HttpCookie();
$cookie->setExpires(0.1);
$this->assertEquals(6, $cookie->getRemainingTime());
sleep(8);
$this->assertEquals(0, $cookie->getRemainingTime());
}
}
3 changes: 2 additions & 1 deletion tests/webfiori/tests/http/RequestParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPUnit\Framework\TestCase;
use webfiori\http\RequestParameter;
use webfiori\http\APIFilter;
/**
* Description of RequestParameterTest
*
Expand Down Expand Up @@ -36,7 +37,7 @@ public function testCreateParameter02() {
'min' => 33,
'max' => 100,
'custom-filter' => function ($original, $basicFilterResult, $param) {
if ($basicFilterResult != \restEasy\APIFilter::INVALID) {
if ($basicFilterResult != APIFilter::INVALID) {
return $basicFilterResult * 100;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/webfiori/tests/http/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function testParams01() {
$uri->setParameterValue('first-var', '1009');
$this->assertEquals(['first-var'], $uri->getParametersNames());
$this->assertEquals('1009', $uri->getParameter('first-var')->getValue());
$this->assertFalse($uri->setParameterValue('not-exist', 'hello'));
}
/**
* @test
Expand Down
1 change: 1 addition & 0 deletions tests/webfiori/tests/http/WebServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function testGetAuthHeaders00() {
'scheme' => '',
'credentials' => ''
],$service->getAuthHeader());
$this->assertNull($service->isAuthorized());
}
/**
*
Expand Down
2 changes: 1 addition & 1 deletion tests/webfiori/tests/http/testServices/TestServiceObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct($name) {
}
//put your code here
public function isAuthorized() {

return parent::isAuthorized();
}

public function processRequest() {
Expand Down
2 changes: 1 addition & 1 deletion webfiori/http/APIFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* The MIT License
*
* Copyright 2019 Ibrahim BinAlshikh, restEasy library.
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion webfiori/http/AbstractWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* The MIT License
*
* Copyright 2019 Ibrahim BinAlshikh, restEasy library.
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
34 changes: 33 additions & 1 deletion webfiori/http/HttpCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ class HttpCookie {
private $val;
/**
* Creates new instance of the class with default properties.
*
* A newly created cookie will have following properties by default:
* <ul>
* <li>name: new-cookie</li>
* <li>path: /</li>
* <li>secure: true</li>
* <li>http only: true</li>
* <li>domain: The domain at which the library is operating from.</li>
* <li>same site: Lax</li>
* <li>expires: 0</li>
* <li>value: sha256 hash</li>
* </ul>
*/
public function __construct() {
$this->httpOnly = true;
Expand Down Expand Up @@ -133,6 +145,26 @@ public function getName() : string {
public function getPath() : string {
return $this->path;
}
/**
* Returns number of seconds before the cookie expires.
*
* @return int If the cookie is non-persistent or the cookie has expired,
* the method will always return 0. Other than that, the method will return
* number of seconds remaining before the cookie dies.
*/
public function getRemainingTime() : int {
$expiresAt = $this->getExpires();

if ($expiresAt == 0) {
return 0;
}
$remaining = $expiresAt - time();

if ($remaining < 0) {
return 0;
}
return $remaining;
}
/**
* Returns the value of the attribute 'SameSite'.
*
Expand Down Expand Up @@ -231,7 +263,7 @@ public function setExpires(float $expireAfter) {

return;
}
$this->expires = time() + $expireAfter;
$this->expires = time() + $expireAfter*60;
}
/**
* Sets the attribute 'HttpOnly'.
Expand Down
2 changes: 1 addition & 1 deletion webfiori/http/ManagerInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* The MIT License
*
* Copyright 2020 Ibrahim BinAlshikh, restEasy library.
* Copyright 2020 Ibrahim BinAlshikh, WebFiori HTTP.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion webfiori/http/RequestParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* The MIT License
*
* Copyright 2019 Ibrahim BinAlshikh, restEasy library.
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
121 changes: 61 additions & 60 deletions webfiori/http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(string $requestedUri) {
if (gettype($this->uriBroken) != 'array') {
throw new InvalidArgumentException('Invalid URI given.');
}

if (!$this->checkOptionalParamsOrder()) {
throw new InvalidArgumentException('Inncorrect parameters order.');
}
Expand All @@ -82,19 +82,6 @@ public function __construct(string $requestedUri) {
$this->uriBroken['vars-possible-values'][$varName] = [];
}
}
private function checkOptionalParamsOrder() {
$hasOptional = false;

foreach ($this->getParameters() as $obj) {
$hasOptional = $hasOptional || $obj->isOptional();
if ($hasOptional && !$obj->isOptional()) {

return false;
}
}

return true;
}

/**
* Adds a request method to the allowed set of methods at which the URI can
Expand Down Expand Up @@ -340,6 +327,50 @@ public function getParametersNames() : array {

return $retVal;
}
/**
* Returns the value of URI parameter given its name.
*
* A URI parameter is a string which is defined while creating the route.
* it is name is included between '{}'.
*
* @param string $varName The name of the parameter. Note that this value
* must not include braces.
*
* @return string|null The method will return the value of the
* parameter if found. If the parameter is not set or the parameter
* does not exist, the method will return null.
*
* @since 1.0
*/
public function getParameterValue(string $varName) {
$param = $this->getParameter($varName);

if ($param !== null) {
return $param->getValue();
}

return null;
}
/**
* Returns an array that contains possible values for a URI parameter.
*
* @param string $varName The name of the parameter.
*
* @return array The method will return an array that contains all possible
* values for the parameter which was added using the method Router::addUriVarValue().
* If the parameter does not exist, the array will be empty.
*
* @since 1.3.6
*/
public function getParameterValues(string $varName) {
$trimmed = trim($varName);

if (isset($this->uriBroken['vars-possible-values'][$trimmed])) {
return $this->uriBroken['vars-possible-values'][$trimmed];
}

return [];
}
/**
* Returns the path part of the URI.
*
Expand Down Expand Up @@ -472,49 +503,6 @@ public function getUri(bool $incQueryStr = false, bool $incFragment = false) {

return $retVal;
}
/**
* Returns the value of URI parameter given its name.
*
* A URI parameter is a string which is defined while creating the route.
* it is name is included between '{}'.
*
* @param string $varName The name of the parameter. Note that this value
* must not include braces.
*
* @return string|null The method will return the value of the
* parameter if found. If the parameter is not set or the parameter
* does not exist, the method will return null.
*
* @since 1.0
*/
public function getParameterValue(string $varName) {
$param = $this->getParameter($varName);
if ($param !== null) {
return $param->getValue();
}

return null;
}
/**
* Returns an array that contains possible values for a URI parameter.
*
* @param string $varName The name of the parameter.
*
* @return array The method will return an array that contains all possible
* values for the parameter which was added using the method Router::addUriVarValue().
* If the parameter does not exist, the array will be empty.
*
* @since 1.3.6
*/
public function getParameterValues(string $varName) {
$trimmed = trim($varName);

if (isset($this->uriBroken['vars-possible-values'][$trimmed])) {
return $this->uriBroken['vars-possible-values'][$trimmed];
}

return [];
}
/**
* Checks if the URI has a parameter or not given its name.
*
Expand Down Expand Up @@ -562,7 +550,7 @@ public function isAllParametersSet() : bool {

return $canRoute;
}

/**
* Checks if URI is fetched using allowed request method or not.
*
Expand All @@ -578,7 +566,7 @@ public function isRequestMethodAllowed() : bool {

return count($methods) == 0 || in_array(Request::getMethod(), $this->getRequestMethods());
}

/**
* Sets the value of a URI parameter.
*
Expand Down Expand Up @@ -726,7 +714,7 @@ public static function splitURI(string $uri) {

return $retVal;
}

/**
* Splits a string based on character mask.
*
Expand Down Expand Up @@ -764,4 +752,17 @@ private static function _queryOrFragment($split, $char, $encoded) {

return $split2;
}
private function checkOptionalParamsOrder() {
$hasOptional = false;

foreach ($this->getParameters() as $obj) {
$hasOptional = $hasOptional || $obj->isOptional();

if ($hasOptional && !$obj->isOptional()) {
return false;
}
}

return true;
}
}
2 changes: 1 addition & 1 deletion webfiori/http/WebServicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* The MIT License
*
* Copyright 2019 Ibrahim BinAlshikh, restEasy library.
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit 932c478

Please sign in to comment.