code tidy-up

This commit is contained in:
Aleksandar Fabijanic
2013-06-17 21:56:53 -05:00
parent d5ec46b9fc
commit 0c50f6241c
3 changed files with 58 additions and 29 deletions

View File

@@ -115,7 +115,7 @@ HTTPCookie::HTTPCookie(const NameValueCollection& nvc):
int tzd; int tzd;
DateTime exp = DateTimeParser::parse(value, tzd); DateTime exp = DateTimeParser::parse(value, tzd);
Timestamp now; Timestamp now;
setMaxAge((int) ((exp.timestamp() - now)/Timestamp::resolution())); setMaxAge((int) ((exp.timestamp() - now) / Timestamp::resolution()));
} }
else if (icompare(name, "version") == 0) else if (icompare(name, "version") == 0)
{ {
@@ -259,7 +259,7 @@ std::string HTTPCookie::toString() const
if (_maxAge != -1) if (_maxAge != -1)
{ {
Timestamp ts; Timestamp ts;
ts += _maxAge*Timestamp::resolution(); ts += _maxAge * Timestamp::resolution();
result.append("; expires="); result.append("; expires=");
DateTimeFormatter::append(result, ts, DateTimeFormat::HTTP_FORMAT); DateTimeFormatter::append(result, ts, DateTimeFormat::HTTP_FORMAT);
} }

View File

@@ -110,55 +110,82 @@ void HTTPCookieTest::testUnescape()
assert (unescaped == "\n\t@,;\"'"); assert (unescaped == "\n\t@,;\"'");
} }
void HTTPCookieTest::testExpiry() void HTTPCookieTest::testExpiryFuture()
{ {
NameValueCollection nvc; NameValueCollection nvc;
nvc.add("name", "value"); nvc.add("name", "value");
//----Test expiry time in the future----
Timestamp before; Timestamp before;
DateTime future; DateTime future;
//now + 1 year //now + 1 year
future.assign(future.year()+1, future.month(),future.day(),future.hour(),future.minute(),future.second(), future.millisecond(), future.microsecond()); future.assign(future.year() + 1,
std::string futureExpiryString = DateTimeFormatter::format(future.timestamp(),DateTimeFormat::HTTP_FORMAT); future.month(),
future.day(),
future.hour(),
future.minute(),
future.second(),
future.millisecond(),
future.microsecond());
std::string futureExpiryString = DateTimeFormatter::format(future.timestamp(), DateTimeFormat::HTTP_FORMAT);
nvc.add("expires", futureExpiryString); nvc.add("expires", futureExpiryString);
HTTPCookie cookie(nvc); HTTPCookie cookie(nvc);
//take one second off the future time since HTTPCookie is always one second off
future.assign(future.year(), future.month(),future.day(),future.hour(),future.minute(),future.second()-1, future.millisecond(), future.microsecond());
futureExpiryString = DateTimeFormatter::format(future.timestamp(),DateTimeFormat::HTTP_FORMAT);
// assert that the cookie bears the same expiry time as the string // one second off to account for expiry of time (this test is not reliable)
// its constructor was passed. future.assign(future.year(),
assert (cookie.toString() == "name=value; expires=" + futureExpiryString); 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); cookie.setVersion(1);
Timestamp now; Timestamp now;
int futureAge = (int) ((future.timestamp() - now)/Timestamp::resolution()); int futureAge = (int) ((future.timestamp() - now) / Timestamp::resolution());
Timestamp after; Timestamp after;
Timespan diff = after - before; //time taken between creation of 'future' and 'now' Timespan diff = after - before; //time taken between creation of 'future' and 'now'
int margin = diff.seconds() + 1; int margin = diff.seconds() + 1;
// assert that the cookie's max age is the number of seconds between now // 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 // and the time indicated in the 'expires' value passed to its constructor, within a margin of error
// constructor, within a margin of error
assert (abs(cookie.getMaxAge() - futureAge) <= margin); assert (abs(cookie.getMaxAge() - futureAge) <= margin);
}
//-----Test expiry time in the past----
before = Timestamp(); void HTTPCookieTest::testExpiryPast()
{
NameValueCollection nvc;
nvc.add("name", "value");
Timestamp before = Timestamp();
DateTime past; DateTime past;
//now - 1 year // 1 year ago
past.assign(past.year()-1, past.month(),past.day(),past.hour(),past.minute(),past.second(), past.millisecond(), past.microsecond()); past.assign(past.year() - 1,
std::string pastExpiryString = DateTimeFormatter::format(past.timestamp(),DateTimeFormat::HTTP_FORMAT); past.month(),
past.day(),
past.hour(),
past.minute(),
past.second(),
past.millisecond(),
past.microsecond());
std::string pastExpiryString = DateTimeFormatter::format(past.timestamp(), DateTimeFormat::HTTP_FORMAT);
nvc.erase("expires"); nvc.erase("expires");
nvc.add("expires", pastExpiryString); nvc.add("expires", pastExpiryString);
cookie = HTTPCookie(nvc); HTTPCookie cookie = HTTPCookie(nvc);
assert (cookie.toString() == "name=value; expires=" + pastExpiryString); assert (cookie.toString() == "name=value; expires=" + pastExpiryString);
cookie.setVersion(1); cookie.setVersion(1);
now = Timestamp(); Timestamp now = Timestamp();
int pastAge = (int) ((past.timestamp() - now)/Timestamp::resolution()); int pastAge = (int) ((past.timestamp() - now) / Timestamp::resolution());
after = Timestamp(); Timestamp after = Timestamp();
diff = after - before; Timespan diff = after - before;
margin = diff.seconds(); int margin = diff.seconds();
assert (abs(cookie.getMaxAge() - pastAge) <= margin); assert (abs(cookie.getMaxAge() - pastAge) <= margin);
} }
@@ -180,7 +207,8 @@ CppUnit::Test* HTTPCookieTest::suite()
CppUnit_addTest(pSuite, HTTPCookieTest, testCookie); CppUnit_addTest(pSuite, HTTPCookieTest, testCookie);
CppUnit_addTest(pSuite, HTTPCookieTest, testEscape); CppUnit_addTest(pSuite, HTTPCookieTest, testEscape);
CppUnit_addTest(pSuite, HTTPCookieTest, testUnescape); CppUnit_addTest(pSuite, HTTPCookieTest, testUnescape);
CppUnit_addTest(pSuite, HTTPCookieTest, testExpiry); CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryFuture);
CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryPast);
return pSuite; return pSuite;
} }

View File

@@ -49,7 +49,8 @@ public:
void testCookie(); void testCookie();
void testEscape(); void testEscape();
void testUnescape(); void testUnescape();
void testExpiry(); void testExpiryFuture();
void testExpiryPast();
void setUp(); void setUp();
void tearDown(); void tearDown();