diff options
author | Gael Pasgrimaud <gael@gawel.org> | 2014-04-17 18:31:39 +0200 |
---|---|---|
committer | Gael Pasgrimaud <gael@gawel.org> | 2014-04-17 18:31:39 +0200 |
commit | d055b1fca818cc270e729f0e8f58bc2902392321 (patch) | |
tree | 7a3dbf9b340a7135f0c5597c0dfc183d65326a40 | |
parent | cd44e874224879a78dd0209b77f8bfd7222ac5da (diff) | |
download | webtest-d055b1fca818cc270e729f0e8f58bc2902392321.tar.gz |
Fixed #84 Application cookies for localhost are no longer ignored
-rw-r--r-- | CHANGELOG.rst | 3 | ||||
-rw-r--r-- | tests/test_app.py | 24 | ||||
-rw-r--r-- | webtest/app.py | 20 |
3 files changed, 46 insertions, 1 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0d478c5..53b856d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,9 @@ News - Fixed #73. Python < 2.6.5 does not support unicode as keyword arguments names. [Stepan Kolesnik] +- Fixed #84 Application cookies for localhost are no longer ignored + [gawel] + - Fixed #89 remove WSGIWarning: You are not supposed to send a body in a DELETE request because we now have a good reason for that. See http://bit.ly/1tb3yxW [gawel] diff --git a/tests/test_app.py b/tests/test_app.py index ce0cfeb..474499f 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -293,6 +293,30 @@ class TestCookies(unittest.TestCase): self.assertEqual(res.request.environ['HTTP_COOKIE'], 'spam=eggs') self.assertEqual(dict(res.request.cookies), {'spam': 'eggs'}) + def test_http_localhost_cookie(self): + def cookie_app(environ, start_response): + status = to_bytes("200 OK") + body = 'Cookie.' + headers = [ + ('Content-Type', 'text/html'), + ('Content-Length', str(len(body))), + ('Set-Cookie', + 'spam=eggs; Domain=localhost;'), + ] + start_response(status, headers) + return [to_bytes(body)] + + app = webtest.TestApp(cookie_app) + self.assertTrue(not app.cookies, + 'App should initially contain no cookies') + + res = app.get('/') + res = app.get('/') + self.assertTrue(app.cookies, + 'Response should not have set cookies') + self.assertEqual(res.request.environ['HTTP_COOKIE'], 'spam=eggs') + self.assertEqual(dict(res.request.cookies), {'spam': 'eggs'}) + class TestEnviron(unittest.TestCase): diff --git a/webtest/app.py b/webtest/app.py index d0b8abb..b57814a 100644 --- a/webtest/app.py +++ b/webtest/app.py @@ -63,6 +63,23 @@ class AppError(Exception): Exception.__init__(self, message) +class CookiePolicy(http_cookiejar.DefaultCookiePolicy): + """A subclass of DefaultCookiePolicy to allow cookie set for + Domain=localhost.""" + + def return_ok_domain(self, cookie, request): + if cookie.domain == '.localhost': + return True + return http_cookiejar.DefaultCookiePolicy.return_ok_domain( + self, cookie, request) + + def set_ok_domain(self, cookie, request): + if cookie.domain == '.localhost': + return True + return http_cookiejar.DefaultCookiePolicy.set_ok_domain( + self, cookie, request) + + class TestRequest(webob.BaseRequest): """A subclass of webob.Request""" ResponseClass = TestResponse @@ -153,7 +170,8 @@ class TestApp(object): extra_environ = {} self.extra_environ = extra_environ self.use_unicode = use_unicode - self.cookiejar = cookiejar or http_cookiejar.CookieJar() + self.cookiejar = cookiejar or http_cookiejar.CookieJar( + policy=CookiePolicy()) if parser_features is None: parser_features = 'html.parser' self.RequestClass.ResponseClass.parser_features = parser_features |