Skip to content

Commit

Permalink
Add equality methods to mechanize.Cookie
Browse files Browse the repository at this point in the history
Also add tests for pickling / unpickling Cookie & CookieJar.

Closes #29
  • Loading branch information
jjlee committed Oct 16, 2010
1 parent fd1495c commit 0cd23f9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mechanize/_clientcookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ class Cookie:
"""


_attrs = ("version", "name", "value",
"port", "port_specified",
"domain", "domain_specified", "domain_initial_dot",
"path", "path_specified",
"secure", "expires", "discard", "comment", "comment_url",
"rfc2109", "_rest")

def __init__(self, version, name, value,
port, port_specified,
domain, domain_specified, domain_initial_dot,
Expand Down Expand Up @@ -382,6 +390,12 @@ def is_expired(self, now=None):
if now is None: now = time.time()
return (self.expires is not None) and (self.expires <= now)

def __eq__(self, other):
return all(getattr(self, a) == getattr(other, a) for a in self._attrs)

def __ne__(self, other):
return not (self == other)

def __str__(self):
if self.port is None: p = ""
else: p = ":"+self.port
Expand Down
60 changes: 60 additions & 0 deletions test/test_cookie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import mechanize._clientcookie
import mechanize._testcase


def cookie_args(
version=1, name="spam", value="eggs",
port="80", port_specified=True,
domain="example.com", domain_specified=False, domain_initial_dot=False,
path="/", path_specified=False,
secure=False,
expires=0,
discard=True,
comment=None,
comment_url=None,
rest={},
rfc2109=False,
):
return locals()


def make_cookie(*args, **kwds):
return mechanize._clientcookie.Cookie(**cookie_args(*args, **kwds))


class Test(mechanize._testcase.TestCase):


def test_equality(self):
# not using assertNotEqual here since operator used varies across
# Python versions
self.assertEqual(make_cookie(), make_cookie())
self.assertFalse(make_cookie(name="ham") == make_cookie())

def test_inequality(self):
# not using assertNotEqual here since operator used varies across
# Python versions
self.assertTrue(make_cookie(name="ham") != make_cookie())
self.assertFalse(make_cookie() != make_cookie())

def test_all_state_included(self):
def non_equal_value(value):
if value is None:
new_value = "80"
elif isinstance(value, basestring):
new_value = value + "1"
elif isinstance(value, bool):
new_value = not value
elif isinstance(value, dict):
new_value = dict(value)
new_value["spam"] = "eggs"
elif isinstance(value, int):
new_value = value + 1
else:
assert False, value
assert new_value != value, value
return new_value
cookie = make_cookie()
for arg, default_value in cookie_args().iteritems():
new_value = non_equal_value(default_value)
self.assertNotEqual(make_cookie(**{arg: new_value}), cookie)
37 changes: 37 additions & 0 deletions test/test_pickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cPickle
import cStringIO as StringIO
import pickle

import mechanize
import mechanize._response
import mechanize._testcase


def pickle_and_unpickle(obj, implementation):
return implementation.loads(implementation.dumps(obj))


def test_pickling(obj, check=lambda unpickled: None):
check(pickle_and_unpickle(obj, cPickle))
check(pickle_and_unpickle(obj, pickle))


class PickleTest(mechanize._testcase.TestCase):

def test_pickle_cookie(self):
cookiejar = mechanize.CookieJar()
url = "http://example.com/"
request = mechanize.Request(url)
response = mechanize._response.test_response(
headers=[("Set-Cookie", "spam=eggs")],
url=url)
[cookie] = cookiejar.make_cookies(response, request)
check_equality = lambda unpickled: self.assertEqual(unpickled, cookie)
test_pickling(cookie, check_equality)

def test_pickle_cookiejar(self):
test_pickling(mechanize.CookieJar())


if __name__ == "__main__":
mechanize._testcase.main()

0 comments on commit 0cd23f9

Please sign in to comment.