Ticket #387 (closed defect: fixed)

Opened 2 years ago

Last modified 17 months ago

Webtest doesn't parse quoted cookies

Reported by: mgood Owned by: ianb
Priority: normal Milestone: 1.4.1
Component: paste Version: svn-trunk
Severity: normal Keywords:
Cc:

Description

Included test case & fix:

  • (a) /dev/null vs. (b) b/tests/test_cookies.py

    diff --git a/tests/test_cookies.py b/tests/test_cookies.py
    new file mode 100644
    index 0000000..8611a2a
    a b  
     1import webtest 
     2from nose.tools import eq_ 
     3 
     4 
     5def cookie_app(environ, start_response): 
     6    status = '200 OK' 
     7    body = '' 
     8    headers = [ 
     9        ('Content-Type', 'text/html'), 
     10        ('Content-Length', str(len(body))), 
     11        ('Set-Cookie', 'spam=eggs'), 
     12        ('Set-Cookie', 'foo="bar;baz"'), 
     13    ] 
     14    start_response(status, headers) 
     15    return [body] 
     16 
     17 
     18def test_cookies(): 
     19    app = webtest.TestApp(cookie_app) 
     20    assert not app.cookies, 'App should initially contain no cookies' 
     21 
     22    res = app.get('/') 
     23    assert app.cookies, 'Response should have set cookies' 
     24    eq_(app.cookies['spam'], 'eggs') 
     25    eq_(app.cookies['foo'], 'bar;baz') 
  • webtest/__init__.py

    diff --git a/webtest/__init__.py b/webtest/__init__.py
    index 1e5682a..ec2beee 100644
    a b  
    1414import time 
    1515import cgi 
    1616import os 
    17 from Cookie import BaseCookie, CookieError 
     17from Cookie import SimpleCookie, CookieError 
    1818from Cookie import _quote as cookie_quote 
    1919try: 
    2020    from cStringIO import StringIO 
     
    374374        res.cookies_set = {} 
    375375        for header in res.headers.getall('set-cookie'): 
    376376            try: 
    377                 c = BaseCookie(header) 
     377                c = SimpleCookie(header) 
    378378            except CookieError, e: 
    379379                raise CookieError( 
    380380                    "Could not parse cookie header %r: %s" % (header, e)) 

Change History

Changed 2 years ago by bbangert

  • status changed from new to closed
  • resolution set to fixed

Changed 21 months ago by dound

  • status changed from closed to reopened
  • resolution fixed deleted

Line 342 also needs to be updated, or the value ends up being double quoted when sent back to the server:

@@ -339,7 +339,7 @@
         req.environ['wsgi.errors'] = errors
         if self.cookies:
             cookie_header = ''.join([
-                '%s="%s"; ' % (name, cookie_quote(value))
+                '%s=%s; ' % (name, cookie_quote(value))
                 for name, value in self.cookies.items()])
             req.environ['HTTP_COOKIE'] = cookie_header
         req.environ['paste.testing'] = True

Changed 18 months ago by peter_v

I can confirm that this is a bug. It seems that Cookie._quote imported as cookie_quote already adds quotes if needed so hard coding additional quotes here seems wrong.

Changed 17 months ago by ianb

  • status changed from reopened to closed
  • resolution set to fixed

Applied in 107:ded87c86cd00

Note: See TracTickets for help on using tickets.