Skip to content

Commit 932c478

Browse files
authored
Merge pull request #25 from WebFiori/dev
Fix to Small bug + Improvements
2 parents f1d18c2 + 84c2e8e commit 932c478

14 files changed

Lines changed: 130 additions & 73 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright 2019 Ibrahim BinAlshikh, restEasy library.
3+
Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It includes inputs feltering and data validation in addion to creating user-defi
1212
<a href="https://sonarcloud.io/dashboard?id=WebFiori_http">
1313
<img src="https://sonarcloud.io/api/project_badges/measure?project=WebFiori_http&metric=alert_status" />
1414
</a>
15-
<a href="https://github.com/WebFiori/restEasy/releases">
15+
<a href="https://github.com/WebFiori/http/releases">
1616
<img src="https://img.shields.io/github/release/WebFiori/http.svg?label=latest" />
1717
</a>
1818
<a href="https://packagist.org/packages/webfiori/http">

tests/webfiori/tests/http/HttpCookieTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testConstructor01() {
3939
$this->assertEquals('/b/a/m', $cookie->getPath());
4040
$this->assertEquals('', $cookie->getLifetime());
4141
$cookie->setExpires(10);
42-
$expires = date(DATE_COOKIE, time() + 10);
42+
$expires = date(DATE_COOKIE, time() + 10*60);
4343
$this->assertTrue($cookie->isPersistent());
4444
$this->assertEquals($expires, $cookie->getLifetime());
4545
$this->assertEquals('Lax', $cookie->getSameSite());
@@ -52,8 +52,8 @@ public function testConstructor01() {
5252
$this->assertTrue($cookie->isHttpOnly());
5353
$this->assertTrue($cookie->isSecure());
5454
$cookie->setExpires(-1);
55-
$expires = date(DATE_COOKIE, time() - 1);
56-
$this->assertEquals(time() - 1, $cookie->getExpires());
55+
$expires = date(DATE_COOKIE, time() - 1*60);
56+
$this->assertEquals(time() - 1*60, $cookie->getExpires());
5757
$this->assertEquals($expires, $cookie->getLifetime());
5858
$this->assertEquals('super=cool; expires='.$expires.'; path=/b/a/m; Secure; HttpOnly; SameSite=None', $cookie->getHeaderString());
5959
$cookie->setExpires(0);
@@ -87,4 +87,25 @@ public function testConstructor03() {
8787
$cookie->kill();
8888
$this->assertEquals(date(DATE_COOKIE, time() - 60*60*24), $cookie->getLifetime());
8989
}
90+
/**
91+
* @test
92+
*/
93+
public function testRemainingTime00() {
94+
$cookie = new HttpCookie();
95+
$this->assertEquals(0, $cookie->getRemainingTime());
96+
$cookie->setExpires(1);
97+
$this->assertEquals(60, $cookie->getRemainingTime());
98+
sleep(3);
99+
$this->assertEquals(57, $cookie->getRemainingTime());
100+
}
101+
/**
102+
* @test
103+
*/
104+
public function testRemainingTime01() {
105+
$cookie = new HttpCookie();
106+
$cookie->setExpires(0.1);
107+
$this->assertEquals(6, $cookie->getRemainingTime());
108+
sleep(8);
109+
$this->assertEquals(0, $cookie->getRemainingTime());
110+
}
90111
}

tests/webfiori/tests/http/RequestParameterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPUnit\Framework\TestCase;
55
use webfiori\http\RequestParameter;
6+
use webfiori\http\APIFilter;
67
/**
78
* Description of RequestParameterTest
89
*
@@ -36,7 +37,7 @@ public function testCreateParameter02() {
3637
'min' => 33,
3738
'max' => 100,
3839
'custom-filter' => function ($original, $basicFilterResult, $param) {
39-
if ($basicFilterResult != \restEasy\APIFilter::INVALID) {
40+
if ($basicFilterResult != APIFilter::INVALID) {
4041
return $basicFilterResult * 100;
4142
}
4243
}

tests/webfiori/tests/http/UriTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function testParams01() {
8383
$uri->setParameterValue('first-var', '1009');
8484
$this->assertEquals(['first-var'], $uri->getParametersNames());
8585
$this->assertEquals('1009', $uri->getParameter('first-var')->getValue());
86+
$this->assertFalse($uri->setParameterValue('not-exist', 'hello'));
8687
}
8788
/**
8889
* @test

tests/webfiori/tests/http/WebServiceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testGetAuthHeaders00() {
1616
'scheme' => '',
1717
'credentials' => ''
1818
],$service->getAuthHeader());
19+
$this->assertNull($service->isAuthorized());
1920
}
2021
/**
2122
*

tests/webfiori/tests/http/testServices/TestServiceObj.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct($name) {
1414
}
1515
//put your code here
1616
public function isAuthorized() {
17-
17+
return parent::isAuthorized();
1818
}
1919

2020
public function processRequest() {

webfiori/http/APIFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* The MIT License
55
*
6-
* Copyright 2019 Ibrahim BinAlshikh, restEasy library.
6+
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

webfiori/http/AbstractWebService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* The MIT License
55
*
6-
* Copyright 2019 Ibrahim BinAlshikh, restEasy library.
6+
* Copyright 2019 Ibrahim BinAlshikh, WebFiori HTTP.
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

webfiori/http/HttpCookie.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ class HttpCookie {
2525
private $val;
2626
/**
2727
* Creates new instance of the class with default properties.
28+
*
29+
* A newly created cookie will have following properties by default:
30+
* <ul>
31+
* <li>name: new-cookie</li>
32+
* <li>path: /</li>
33+
* <li>secure: true</li>
34+
* <li>http only: true</li>
35+
* <li>domain: The domain at which the library is operating from.</li>
36+
* <li>same site: Lax</li>
37+
* <li>expires: 0</li>
38+
* <li>value: sha256 hash</li>
39+
* </ul>
2840
*/
2941
public function __construct() {
3042
$this->httpOnly = true;
@@ -133,6 +145,26 @@ public function getName() : string {
133145
public function getPath() : string {
134146
return $this->path;
135147
}
148+
/**
149+
* Returns number of seconds before the cookie expires.
150+
*
151+
* @return int If the cookie is non-persistent or the cookie has expired,
152+
* the method will always return 0. Other than that, the method will return
153+
* number of seconds remaining before the cookie dies.
154+
*/
155+
public function getRemainingTime() : int {
156+
$expiresAt = $this->getExpires();
157+
158+
if ($expiresAt == 0) {
159+
return 0;
160+
}
161+
$remaining = $expiresAt - time();
162+
163+
if ($remaining < 0) {
164+
return 0;
165+
}
166+
return $remaining;
167+
}
136168
/**
137169
* Returns the value of the attribute 'SameSite'.
138170
*
@@ -231,7 +263,7 @@ public function setExpires(float $expireAfter) {
231263

232264
return;
233265
}
234-
$this->expires = time() + $expireAfter;
266+
$this->expires = time() + $expireAfter*60;
235267
}
236268
/**
237269
* Sets the attribute 'HttpOnly'.

0 commit comments

Comments
 (0)