Merge pull request #582 from slingamn/cookietests.release
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Tests for persistent and secure cookies
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 1adaba3, 8fedec7 · Browse files · 2 files·+44 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+44 added−2 removed
- tests:+44 added−2 removed
Provenance
- Commit
8bd6515- Tree
03760be- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
1adaba3- Parent 2
8fedec7
2 files changed+44 added−2 removed
modified:tests/test_cookies.py+41 added
84 unmodified lines85 """Regression test: don't send a Cookie header with a string value of 'None'!"""86 page = json.loads(requests.get(httpbin('headers')).text)87 self.assertTrue('Cookie' not in page['headers'])88 89+ def test_secure_cookies(self):90+ """Test that secure cookies can only be sent via https."""91+ header = "Set-Cookie: ThisIsA=SecureCookie; Path=/; Secure; HttpOnly"92+ url = 'https://httpbin.org/response-headers?%s' % (requests.utils.quote(header),)93+ cookies = requests.get(url, verify=False).cookies94+ self.assertEqual(len(cookies), 1)95+ self.assertEqual(list(cookies)[0].secure, True)96+97+ secure_resp = requests.get('https://httpbin.org/cookies', cookies=cookies, verify=False)98+ secure_cookies_sent = json.loads(secure_resp.text)['cookies']99+ self.assertEqual(secure_cookies_sent, {'ThisIsA': 'SecureCookie'})100+101+ insecure_resp = requests.get('http://httpbin.org/cookies', cookies=cookies)102+ insecure_cookies_sent = json.loads(insecure_resp.text)['cookies']103+ self.assertEqual(insecure_cookies_sent, {})104+105 class LWPCookieJarTest(TestBaseMixin, unittest.TestCase):106 """Check store/load of cookies to FileCookieJar's, specifically LWPCookieJar's."""107 108 COOKIEJAR_CLASS = cookielib.LWPCookieJar63 unmodified lines172 self.assertEqual(len(cookiejar_2), num_total_cookies)173 r = requests.get(httpbin('cookies'), cookies=cookiejar_2)174 self.assertEqual(json.loads(r.text)['cookies'], {'key': 'value'})175 176+ def test_persistent_cookies(self):177+ """Test that we correctly interpret persistent cookies."""178+ # httpbin's normal cookie methods don't send persistent cookies,179+ # so cook up the appropriate header and force it to send180+ header = "Set-Cookie: Persistent=CookiesAreScary; expires=Sun, 04-May-2032 04:56:50 GMT; path=/"181+ url = httpbin('response-headers?%s' % (requests.utils.quote(header),))182+ cookiejar = self.COOKIEJAR_CLASS(self.cookiejar_filename)183+184+ requests.get(url, cookies=cookiejar)185+ self.assertEqual(len(cookiejar), 1)186+ self.assertCookieHas(list(cookiejar)[0], name='Persistent', value='CookiesAreScary')187+188+ requests.get(httpbin('cookies', 'set', 'ThisCookieIs', 'SessionOnly'), cookies=cookiejar)189+ self.assertEqual(len(cookiejar), 2)190+ self.assertEqual(len([c for c in cookiejar if c.name == 'Persistent']), 1)191+ self.assertEqual(len([c for c in cookiejar if c.name == 'ThisCookieIs']), 1)192+193+ # save and load194+ cookiejar.save()195+ cookiejar_2 = self.COOKIEJAR_CLASS(self.cookiejar_filename)196+ cookiejar_2.load()197+ # we should only load the persistent cookie198+ self.assertEqual(len(cookiejar_2), 1)199+ self.assertCookieHas(list(cookiejar_2)[0], name='Persistent', value='CookiesAreScary')200+201 class MozCookieJarTest(LWPCookieJarTest):202 """Same test, but substitute MozillaCookieJar."""203 204 COOKIEJAR_CLASS = cookielib.MozillaCookieJar3 unmodified linesmodified:tests/test_requests.py+3 added-2 removed
54 unmodified lines55 56 def assertCookieHas(self, cookie, **kwargs):57 """Assert that a cookie has various specified properties."""58 for attr, expected_value in kwargs.items():59+ cookie_attr = getattr(cookie, attr)59- message = 'Failed comparison for %s' % (attr,)60- self.assertEqual(getattr(cookie, attr), expected_value, message)60+ message = 'Failed comparison for %s: %s != %s' % (attr, cookie_attr, expected_value)61+ self.assertEqual(cookie_attr, expected_value, message)62 63 class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase):64 """Requests test cases."""65 811 unmodified lines