diff --git a/Net/src/HTTPCookie.cpp b/Net/src/HTTPCookie.cpp index 3ad0de857..c984b34c9 100644 --- a/Net/src/HTTPCookie.cpp +++ b/Net/src/HTTPCookie.cpp @@ -115,7 +115,7 @@ HTTPCookie::HTTPCookie(const NameValueCollection& nvc): int tzd; DateTime exp = DateTimeParser::parse(value, tzd); Timestamp now; - setMaxAge((int) ((exp.timestamp() - now)/Timestamp::resolution())); + setMaxAge((int) ((exp.timestamp() - now) / Timestamp::resolution())); } else if (icompare(name, "version") == 0) { @@ -259,7 +259,7 @@ std::string HTTPCookie::toString() const if (_maxAge != -1) { Timestamp ts; - ts += _maxAge*Timestamp::resolution(); + ts += _maxAge * Timestamp::resolution(); result.append("; expires="); DateTimeFormatter::append(result, ts, DateTimeFormat::HTTP_FORMAT); } diff --git a/Net/testsuite/src/HTTPCookieTest.cpp b/Net/testsuite/src/HTTPCookieTest.cpp index 18ad9beb5..ee5b31059 100644 --- a/Net/testsuite/src/HTTPCookieTest.cpp +++ b/Net/testsuite/src/HTTPCookieTest.cpp @@ -110,55 +110,82 @@ void HTTPCookieTest::testUnescape() assert (unescaped == "\n\t@,;\"'"); } -void HTTPCookieTest::testExpiry() - { +void HTTPCookieTest::testExpiryFuture() +{ NameValueCollection nvc; nvc.add("name", "value"); - //----Test expiry time in the future---- Timestamp before; DateTime future; //now + 1 year - future.assign(future.year()+1, future.month(),future.day(),future.hour(),future.minute(),future.second(), future.millisecond(), future.microsecond()); - std::string futureExpiryString = DateTimeFormatter::format(future.timestamp(),DateTimeFormat::HTTP_FORMAT); + future.assign(future.year() + 1, + 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); 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 - // its constructor was passed. - assert (cookie.toString() == "name=value; expires=" + futureExpiryString); + // 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()); + 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 + // and the time indicated in the 'expires' value passed to its constructor, within a margin of error 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; - //now - 1 year - past.assign(past.year()-1, past.month(),past.day(),past.hour(),past.minute(),past.second(), past.millisecond(), past.microsecond()); - std::string pastExpiryString = DateTimeFormatter::format(past.timestamp(),DateTimeFormat::HTTP_FORMAT); + // 1 year ago + past.assign(past.year() - 1, + 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.add("expires", pastExpiryString); - cookie = HTTPCookie(nvc); + HTTPCookie cookie = HTTPCookie(nvc); assert (cookie.toString() == "name=value; expires=" + pastExpiryString); cookie.setVersion(1); - now = Timestamp(); - int pastAge = (int) ((past.timestamp() - now)/Timestamp::resolution()); - after = Timestamp(); - diff = after - before; - margin = diff.seconds(); + Timestamp now = Timestamp(); + int pastAge = (int) ((past.timestamp() - now) / Timestamp::resolution()); + Timestamp after = Timestamp(); + Timespan diff = after - before; + int margin = diff.seconds(); assert (abs(cookie.getMaxAge() - pastAge) <= margin); } @@ -180,7 +207,8 @@ CppUnit::Test* HTTPCookieTest::suite() CppUnit_addTest(pSuite, HTTPCookieTest, testCookie); CppUnit_addTest(pSuite, HTTPCookieTest, testEscape); CppUnit_addTest(pSuite, HTTPCookieTest, testUnescape); - CppUnit_addTest(pSuite, HTTPCookieTest, testExpiry); + CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryFuture); + CppUnit_addTest(pSuite, HTTPCookieTest, testExpiryPast); return pSuite; } diff --git a/Net/testsuite/src/HTTPCookieTest.h b/Net/testsuite/src/HTTPCookieTest.h index 4c4d19f25..864d9d323 100644 --- a/Net/testsuite/src/HTTPCookieTest.h +++ b/Net/testsuite/src/HTTPCookieTest.h @@ -49,7 +49,8 @@ public: void testCookie(); void testEscape(); void testUnescape(); - void testExpiry(); + void testExpiryFuture(); + void testExpiryPast(); void setUp(); void tearDown();