| 1 | import pickle |
| 2 | import importlib |
| 3 | |
| 4 | import mechanize |
| 5 | import mechanize._response |
| 6 | import mechanize._testcase |
| 7 | from mechanize.polyglot import is_py2 |
| 8 | |
| 9 | pickle_modules = [pickle] |
| 10 | if is_py2: |
| 11 | pickle_modules.append(importlib.import_module('cPickle')) |
| 12 | |
| 13 | |
| 14 | def pickle_and_unpickle(obj, implementation): |
| 15 | return implementation.loads(implementation.dumps(obj)) |
| 16 | |
| 17 | |
| 18 | def test_pickling(obj, check=lambda unpickled: None): |
| 19 | for pm in pickle_modules: |
| 20 | check(pickle_and_unpickle(obj, pm)) |
| 21 | |
| 22 | |
| 23 | class PickleTest(mechanize._testcase.TestCase): |
| 24 | |
| 25 | def test_pickle_cookie(self): |
| 26 | from mechanize._clientcookie import cookies_equal |
| 27 | cookiejar = mechanize.CookieJar() |
| 28 | url = "http://example.com/" |
| 29 | request = mechanize.Request(url) |
| 30 | response = mechanize._response.test_response( |
| 31 | headers=[("Set-Cookie", "spam=eggs")], url=url) |
| 32 | [cookie] = cookiejar.make_cookies(response, request) |
| 33 | |
| 34 | def check_equality(b): |
| 35 | self.assertTrue(cookies_equal(cookie, b)) |
| 36 | |
| 37 | test_pickling(cookie, check_equality) |
| 38 | |
| 39 | def test_pickle_cookiejar(self): |
| 40 | test_pickling(mechanize.CookieJar()) |
| 41 | |
| 42 | |
| 43 | if __name__ == "__main__": |
| 44 | mechanize._testcase.main() |