当前位置: 首页>>代码示例>>Python>>正文


Python Cookie.parse方法代码示例

本文整理汇总了Python中ipapython.cookie.Cookie.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Cookie.parse方法的具体用法?Python Cookie.parse怎么用?Python Cookie.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ipapython.cookie.Cookie的用法示例。


在下文中一共展示了Cookie.parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_invalid

# 需要导入模块: from ipapython.cookie import Cookie [as 别名]
# 或者: from ipapython.cookie.Cookie import parse [as 别名]
    def test_invalid(self):
        # Invalid Max-Age
        s = 'color=blue; Max-Age=over-the-hill'
        with self.assertRaises(ValueError):
            cookies = Cookie.parse(s)

        cookie = Cookie('color', 'blue')
        with self.assertRaises(ValueError):
            cookie.max_age = 'over-the-hill'

        # Invalid Expires
        s = 'color=blue; Expires=Sun, 06 Xxx 1994 08:49:37 GMT'
        with self.assertRaises(ValueError):
            cookies = Cookie.parse(s)

        cookie = Cookie('color', 'blue')
        with self.assertRaises(ValueError):
            cookie.expires = 'Sun, 06 Xxx 1994 08:49:37 GMT'
开发者ID:guanwei,项目名称:freeipa,代码行数:20,代码来源:test_cookie.py

示例2: test_invalid

# 需要导入模块: from ipapython.cookie import Cookie [as 别名]
# 或者: from ipapython.cookie.Cookie import parse [as 别名]
    def test_invalid(self):
        # Invalid Max-Age
        s = "color=blue; Max-Age=over-the-hill"
        with self.assertRaises(ValueError):
            Cookie.parse(s)

        cookie = Cookie("color", "blue")
        with self.assertRaises(ValueError):
            cookie.max_age = "over-the-hill"

        # Invalid Expires
        s = "color=blue; Expires=Sun, 06 Xxx 1994 08:49:37 GMT"
        with self.assertRaises(ValueError):
            Cookie.parse(s)

        cookie = Cookie("color", "blue")
        with self.assertRaises(ValueError):
            cookie.expires = "Sun, 06 Xxx 1994 08:49:37 GMT"
开发者ID:apophys,项目名称:freeipa,代码行数:20,代码来源:test_cookie.py

示例3: test_parse

# 需要导入模块: from ipapython.cookie import Cookie [as 别名]
# 或者: from ipapython.cookie.Cookie import parse [as 别名]
    def test_parse(self):
        # Empty string
        s = ''
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 0)

        # Invalid single token
        s = 'color'
        with self.assertRaises(ValueError):
            cookies = Cookie.parse(s)

        # Invalid single token that's keyword
        s = 'HttpOnly'
        with self.assertRaises(ValueError):
            cookies = Cookie.parse(s)

        # Invalid key/value pair whose key is a keyword
        s = 'domain=example.com'
        with self.assertRaises(ValueError):
            cookies = Cookie.parse(s)

        # 1 cookie with empty value
        s = 'color='
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, '')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=")
        self.assertEqual(cookie.http_cookie(), "color=;")

        # 1 cookie with name/value
        s = 'color=blue'
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue")
        self.assertEqual(cookie.http_cookie(), "color=blue;")

        # 1 cookie with whose value is quoted
        # Use "get by name" utility to extract specific cookie
        s = 'color="blue"'
        cookie = Cookie.get_named_cookie_from_string(s, 'color')
        self.assertIsNotNone(cookie)
        self.assertIsNotNone(cookie, Cookie)
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue")
        self.assertEqual(cookie.http_cookie(), "color=blue;")

        # 1 cookie with name/value and domain, path attributes.
        # Change up the whitespace a bit.
        s = 'color =blue; domain= example.com ; path = /toplevel '
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, 'example.com')
        self.assertEqual(cookie.path, '/toplevel')
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue; Domain=example.com; Path=/toplevel")
        self.assertEqual(cookie.http_cookie(), "color=blue;")

        # 2 cookies, various attributes
        s = 'color=blue; Max-Age=3600; temperature=hot; HttpOnly'
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 2)
        cookie = cookies[0]
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, 3600)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
#.........这里部分代码省略.........
开发者ID:guanwei,项目名称:freeipa,代码行数:103,代码来源:test_cookie.py

示例4: test_expires

# 需要导入模块: from ipapython.cookie import Cookie [as 别名]
# 或者: from ipapython.cookie.Cookie import parse [as 别名]
    def test_expires(self):
        # 1 cookie with name/value and no Max-Age and no Expires
        s = 'color=blue;'
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        # Force timestamp to known value
        cookie.timestamp = self.now
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue")
        self.assertEqual(cookie.get_expiration(), None)
        # Normalize
        self.assertEqual(cookie.normalize_expiration(), None)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(str(cookie), "color=blue")

        # 1 cookie with name/value and Max-Age
        s = 'color=blue; max-age=%d' % (self.max_age)
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        # Force timestamp to known value
        cookie.timestamp = self.now
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, self.max_age)
        self.assertEqual(cookie.expires, None)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue; Max-Age=%d" % (self.max_age))
        self.assertEqual(cookie.get_expiration(), self.age_expiration)
        # Normalize
        self.assertEqual(cookie.normalize_expiration(), self.age_expiration)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, self.age_expiration)
        self.assertEqual(str(cookie), "color=blue; Expires=%s" % (self.age_string))


        # 1 cookie with name/value and Expires
        s = 'color=blue; Expires=%s' % (self.expires_string)
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        # Force timestamp to known value
        cookie.timestamp = self.now
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, self.expires)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue; Expires=%s" % (self.expires_string))
        self.assertEqual(cookie.get_expiration(), self.expires)
        # Normalize
        self.assertEqual(cookie.normalize_expiration(), self.expires)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, self.expires)
        self.assertEqual(str(cookie), "color=blue; Expires=%s" % (self.expires_string))

        # 1 cookie with name/value witht both Max-Age and Expires, Max-Age takes precedence
        s = 'color=blue; Expires=%s; max-age=%d' % (self.expires_string, self.max_age)
        cookies = Cookie.parse(s)
        self.assertEqual(len(cookies), 1)
        cookie = cookies[0]
        # Force timestamp to known value
        cookie.timestamp = self.now
        self.assertEqual(cookie.key, 'color')
        self.assertEqual(cookie.value, 'blue')
        self.assertEqual(cookie.domain, None)
        self.assertEqual(cookie.path, None)
        self.assertEqual(cookie.max_age, self.max_age)
        self.assertEqual(cookie.expires, self.expires)
        self.assertEqual(cookie.secure, None)
        self.assertEqual(cookie.httponly, None)
        self.assertEqual(str(cookie), "color=blue; Max-Age=%d; Expires=%s" % (self.max_age, self.expires_string))
        self.assertEqual(cookie.get_expiration(), self.age_expiration)
        # Normalize
        self.assertEqual(cookie.normalize_expiration(), self.age_expiration)
        self.assertEqual(cookie.max_age, None)
        self.assertEqual(cookie.expires, self.age_expiration)
        self.assertEqual(str(cookie), "color=blue; Expires=%s" % (self.age_string))

        # Verify different types can be assigned to the timestamp and
        # expires attribute.

        cookie = Cookie('color', 'blue')
        cookie.timestamp = self.now
        self.assertEqual(cookie.timestamp, self.now)
#.........这里部分代码省略.........
开发者ID:guanwei,项目名称:freeipa,代码行数:103,代码来源:test_cookie.py


注:本文中的ipapython.cookie.Cookie.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。