-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add equality methods to mechanize.Cookie
Also add tests for pickling / unpickling Cookie & CookieJar. Closes #29
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |