mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-06 00:31:10 +01:00
Merge pull request #225 from karlr42/develop
Improved test case for HTTPCookie expiry functions
This commit is contained in:
commit
7cf83c1b6f
@ -112,12 +112,8 @@ void HTTPCookieTest::testUnescape()
|
|||||||
|
|
||||||
void HTTPCookieTest::testExpiryFuture()
|
void HTTPCookieTest::testExpiryFuture()
|
||||||
{
|
{
|
||||||
NameValueCollection nvc;
|
|
||||||
nvc.add("name", "value");
|
|
||||||
|
|
||||||
Timestamp before;
|
|
||||||
DateTime future;
|
DateTime future;
|
||||||
//now + 1 year
|
//1 year from now
|
||||||
future.assign(future.year() + 1,
|
future.assign(future.year() + 1,
|
||||||
future.month(),
|
future.month(),
|
||||||
future.day(),
|
future.day(),
|
||||||
@ -126,43 +122,11 @@ void HTTPCookieTest::testExpiryFuture()
|
|||||||
future.second(),
|
future.second(),
|
||||||
future.millisecond(),
|
future.millisecond(),
|
||||||
future.microsecond());
|
future.microsecond());
|
||||||
|
testCookieExpiry(future);
|
||||||
std::string futureExpiryString = DateTimeFormatter::format(future.timestamp(), DateTimeFormat::HTTP_FORMAT);
|
|
||||||
nvc.add("expires", futureExpiryString);
|
|
||||||
HTTPCookie cookie(nvc);
|
|
||||||
|
|
||||||
// one second off to account for expiry of time (this test is not reliable)
|
|
||||||
future.assign(future.year(),
|
|
||||||
future.month(),
|
|
||||||
future.day(),
|
|
||||||
future.hour(),
|
|
||||||
future.minute(),
|
|
||||||
future.second() - 1,
|
|
||||||
future.millisecond(),
|
|
||||||
future.microsecond());
|
|
||||||
|
|
||||||
futureExpiryString = "name=value; expires=";
|
|
||||||
DateTimeFormatter::append(futureExpiryString, future.timestamp(), DateTimeFormat::HTTP_FORMAT);
|
|
||||||
assert (cookie.toString() == futureExpiryString);
|
|
||||||
|
|
||||||
cookie.setVersion(1);
|
|
||||||
Timestamp now;
|
|
||||||
int futureAge = (int) ((future.timestamp() - now) / Timestamp::resolution());
|
|
||||||
Timestamp after;
|
|
||||||
Timespan diff = after - before; //time taken between creation of 'future' and 'now'
|
|
||||||
int margin = diff.seconds() + 1;
|
|
||||||
// assert that the cookie's max age is the number of seconds between now
|
|
||||||
// and the time indicated in the 'expires' value passed to its constructor, within a margin of error
|
|
||||||
assert (abs(cookie.getMaxAge() - futureAge) <= margin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void HTTPCookieTest::testExpiryPast()
|
void HTTPCookieTest::testExpiryPast()
|
||||||
{
|
{
|
||||||
NameValueCollection nvc;
|
|
||||||
nvc.add("name", "value");
|
|
||||||
|
|
||||||
Timestamp before = Timestamp();
|
|
||||||
DateTime past;
|
DateTime past;
|
||||||
// 1 year ago
|
// 1 year ago
|
||||||
past.assign(past.year() - 1,
|
past.assign(past.year() - 1,
|
||||||
@ -173,20 +137,48 @@ void HTTPCookieTest::testExpiryPast()
|
|||||||
past.second(),
|
past.second(),
|
||||||
past.millisecond(),
|
past.millisecond(),
|
||||||
past.microsecond());
|
past.microsecond());
|
||||||
|
testCookieExpiry(past);
|
||||||
|
}
|
||||||
|
|
||||||
std::string pastExpiryString = DateTimeFormatter::format(past.timestamp(), DateTimeFormat::HTTP_FORMAT);
|
void HTTPCookieTest::testCookieExpiry(DateTime expiryTime){
|
||||||
nvc.erase("expires");
|
NameValueCollection nvc;
|
||||||
nvc.add("expires", pastExpiryString);
|
nvc.add("name", "value");
|
||||||
HTTPCookie cookie = HTTPCookie(nvc);
|
std::string expiryString = DateTimeFormatter::format(expiryTime.timestamp(),DateTimeFormat::HTTP_FORMAT);
|
||||||
assert (cookie.toString() == "name=value; expires=" + pastExpiryString);
|
nvc.add("expires", expiryString);
|
||||||
|
|
||||||
|
Timestamp before; //start of cookie lifetime
|
||||||
|
HTTPCookie cookie(nvc); //cookie created
|
||||||
|
std::string cookieStringV0 = cookie.toString();
|
||||||
cookie.setVersion(1);
|
cookie.setVersion(1);
|
||||||
Timestamp now = Timestamp();
|
std::string cookieStringV1 = cookie.toString();
|
||||||
int pastAge = (int) ((past.timestamp() - now) / Timestamp::resolution());
|
Timestamp now;
|
||||||
Timestamp after = Timestamp();
|
//expected number of seconds until expiryTime - should be close to cookie._maxAge
|
||||||
Timespan diff = after - before;
|
int expectedMaxAge = (int) ((expiryTime.timestamp() - now) / Timestamp::resolution()); //expected number of seconds until expiryTime
|
||||||
int margin = diff.seconds();
|
Timestamp after; //end of cookie lifetime
|
||||||
assert (abs(cookie.getMaxAge() - pastAge) <= margin);
|
|
||||||
|
//length of lifetime of the cookie
|
||||||
|
Timespan delta = after - before;
|
||||||
|
|
||||||
|
//pull out cookie expire time string
|
||||||
|
size_t startPos = cookieStringV0.find("expires=") + 8;
|
||||||
|
std::string cookieExpireTimeStr = cookieStringV0.substr(startPos, cookieStringV0.find(";", startPos));
|
||||||
|
//convert to a DateTime
|
||||||
|
int tzd;
|
||||||
|
DateTime cookieExpireTime = DateTimeParser::parse(cookieExpireTimeStr, tzd);
|
||||||
|
//pull out cookie max age
|
||||||
|
int cookieMaxAge;
|
||||||
|
startPos = cookieStringV1.find("Max-Age=\"") + 9;
|
||||||
|
std::string cookieMaxAgeStr = cookieStringV1.substr(startPos, cookieStringV1.find("\"", startPos));
|
||||||
|
//convert to integer
|
||||||
|
std::istringstream(cookieMaxAgeStr) >> cookieMaxAge;
|
||||||
|
|
||||||
|
//assert that the cookie's expiry time reflects the time passed to
|
||||||
|
//its constructor, within a delta of the lifetime of the cookie
|
||||||
|
assert (cookieExpireTime - expiryTime <= delta);
|
||||||
|
//assert that the cookie's max age is the number of seconds between
|
||||||
|
//the creation of the cookie and the expiry time passed to its
|
||||||
|
//constuctor, within a delta of the lifetime of the cookie
|
||||||
|
assert (abs(cookieMaxAge - expectedMaxAge) <= delta.seconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Poco/Net/Net.h"
|
#include "Poco/Net/Net.h"
|
||||||
|
#include "Poco/DateTime.h"
|
||||||
#include "CppUnit/TestCase.h"
|
#include "CppUnit/TestCase.h"
|
||||||
|
|
||||||
|
|
||||||
@ -51,6 +52,7 @@ public:
|
|||||||
void testUnescape();
|
void testUnescape();
|
||||||
void testExpiryFuture();
|
void testExpiryFuture();
|
||||||
void testExpiryPast();
|
void testExpiryPast();
|
||||||
|
void testCookieExpiry(Poco::DateTime expiryTime);
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
Loading…
Reference in New Issue
Block a user