summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Lebedev <andrey@lebedev.lt>2013-03-07 16:25:47 +0200
committerAndrey Lebedev <andrey@lebedev.lt>2013-03-08 12:58:54 +0200
commit2bbbdb020817c5f0f90124f75480388f96292863 (patch)
tree88df7e69112d1b5ee5a19fac1be325976170c5c5
parent03e7be584c5b4e739e4fdce68360f2bc77376372 (diff)
downloadwebtest-2bbbdb020817c5f0f90124f75480388f96292863.tar.gz
Support secure cookies
Add missing methods to _RequestCookieAdapter to comply with requirements of urllib2.Request interfaces (specified at http://docs.python.org/2/library/cookielib.html#cookielib.CookieJar.add_cookie_header)
-rw-r--r--CHANGELOG.rst2
-rw-r--r--tests/test_app.py32
-rw-r--r--webtest/utils.py15
3 files changed, 49 insertions, 0 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index fbd0323..226dee9 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -6,6 +6,8 @@ News
* Allow TestResponse.click() to match HTML content again.
+* Support secure cookies [Andrey Lebedev]
+
2.0.1 (2013-03-05)
------------------
diff --git a/tests/test_app.py b/tests/test_app.py
index 30e217b..95d8d56 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -179,6 +179,38 @@ class TestCookies(unittest.TestCase):
app.reset()
self.assertFalse(bool(app.cookies))
+ def test_secure_cookies(self):
+ def cookie_app(environ, start_response):
+ req = Request(environ)
+ status = "200 OK"
+ body = '<html><body><a href="/go/">go</a></body></html>'
+ headers = [
+ ('Content-Type', 'text/html'),
+ ('Content-Length', str(len(body))),
+ ]
+ if req.path_info != '/go/':
+ headers.extend([
+ ('Set-Cookie', 'spam=eggs; secure'),
+ ('Set-Cookie', 'foo=bar;baz; secure'),
+ ])
+ else:
+ self.assertEquals(dict(req.cookies),
+ {'spam': 'eggs', 'foo': 'bar'})
+ self.assertIn('foo=bar', environ['HTTP_COOKIE'])
+ self.assertIn('spam=eggs', environ['HTTP_COOKIE'])
+ start_response(status, headers)
+ return [to_bytes(body)]
+
+ app = webtest.TestApp(cookie_app)
+
+ self.assertFalse(app.cookies)
+ res = app.get('https://localhost/')
+ self.assertEqual(app.cookies['spam'], 'eggs')
+ self.assertEqual(app.cookies['foo'], 'bar')
+ res = res.click('go')
+ self.assertEqual(app.cookies['spam'], 'eggs')
+ self.assertEqual(app.cookies['foo'], 'bar')
+
def test_cookies_readonly(self):
app = webtest.TestApp(debug_app)
try:
diff --git a/webtest/utils.py b/webtest/utils.py
index 34bf2b3..419767d 100644
--- a/webtest/utils.py
+++ b/webtest/utils.py
@@ -123,6 +123,21 @@ class _RequestCookieAdapter(object):
def has_header(self, key):
return key in self._request.headers
+ def get_host(self):
+ return self._request.host
+
+ def get_type(self):
+ return self._request.scheme
+
+ @property
+ def type(self): # NOQA
+ # This is undocumented method that Python 3 cookielib uses
+ return self.get_type()
+
+ def header_items(self):
+ return self._request.headers.items()
+
+
class _ResponseCookieAdapter(object):
"""