browser2: Add easy cookie building
This commit is contained in:
parent
ec64a52643
commit
83e08c103d
2 changed files with 91 additions and 7 deletions
|
|
@ -1,8 +1,9 @@
|
|||
# TODO declare __all__
|
||||
# TODO support logging
|
||||
|
||||
import urlparse
|
||||
from urlparse import urlparse
|
||||
from datetime import datetime, timedelta
|
||||
import posixpath
|
||||
|
||||
from .cookies import Cookie, Cookies, strip_spaces_and_quotes, Definitions
|
||||
|
||||
|
|
@ -130,7 +131,7 @@ class CookieJar(object):
|
|||
|
||||
:rtype: bool
|
||||
"""
|
||||
url = urlparse.urlparse(url)
|
||||
url = urlparse(url)
|
||||
domain = url.hostname
|
||||
|
||||
# Accept/reject overrides
|
||||
|
|
@ -174,7 +175,7 @@ class CookieJar(object):
|
|||
:type url: str
|
||||
:type now: datetime
|
||||
"""
|
||||
url = urlparse.urlparse(url)
|
||||
url = urlparse(url)
|
||||
if cookie.domain is None:
|
||||
cookie.domain = url.hostname
|
||||
if cookie.path is None:
|
||||
|
|
@ -208,7 +209,7 @@ class CookieJar(object):
|
|||
:type now: datetime
|
||||
:rtype: dict
|
||||
"""
|
||||
url = urlparse.urlparse(url)
|
||||
url = urlparse(url)
|
||||
if now is None:
|
||||
now = datetime.now()
|
||||
# we want insecure cookies in https too!
|
||||
|
|
@ -359,3 +360,50 @@ class CookieJar(object):
|
|||
Remove all cookies.
|
||||
"""
|
||||
self.cookies.clear()
|
||||
|
||||
def build(self, name, value, url, path=None, wildcard=False):
|
||||
"""
|
||||
Build a Cookie object for the current URL.
|
||||
|
||||
The domain and path are guessed. If you want to set for the whole domain,
|
||||
take care of what you put in URL!
|
||||
for_url('http://example.com/hello/world') will only set cookie for the
|
||||
/hello/ path.
|
||||
|
||||
`name` and `value` are required parameters of Cookie.__init__()
|
||||
|
||||
You can force the `path` if you want.
|
||||
|
||||
The `wildcard` parameter will add a period before the domain.
|
||||
|
||||
Typical usage would be, inside a DomainBrowser:
|
||||
cookie = self.cookies.for_url(k, v, self.url)
|
||||
cookie = self.cookies.for_url(k, v, self.absurl('/'))
|
||||
cookie = self.cookies.for_url(k, v, self.BASEURL)
|
||||
|
||||
And then:
|
||||
self.cookies.set(cookie)
|
||||
|
||||
For more advanced usage, create a Cookie object manually, or
|
||||
alter the returned Cookie object before set().
|
||||
|
||||
:type name: basestring
|
||||
:type value: basestring
|
||||
:type url: str
|
||||
:type path: str
|
||||
:type wildcard: bool
|
||||
:rtype cookie: :class:`cookies.Cookie`
|
||||
"""
|
||||
cookie = Cookie(name, value)
|
||||
url = urlparse(url)
|
||||
if wildcard:
|
||||
cookie.domain = '.' + url.hostname
|
||||
else:
|
||||
cookie.domain = url.hostname
|
||||
if path is None:
|
||||
cookie.path = posixpath.join(posixpath.dirname(url.path), '')
|
||||
else:
|
||||
cookie.path = path
|
||||
if url.scheme == 'https':
|
||||
cookie.secure = True
|
||||
return cookie
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ from requests import HTTPError
|
|||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from .browser import BaseBrowser, DomainBrowser, Weboob
|
||||
from . import cookiejar
|
||||
from .cookiejar import CookieJar
|
||||
from .cookies import Cookies
|
||||
|
||||
from weboob.tools.json import json
|
||||
|
|
@ -234,7 +234,7 @@ def test_referrer():
|
|||
|
||||
|
||||
def test_cookieparse():
|
||||
cj = cookiejar.CookieJar()
|
||||
cj = CookieJar()
|
||||
|
||||
def bc(data):
|
||||
"""
|
||||
|
|
@ -300,7 +300,7 @@ def test_cookiejar():
|
|||
cookie3 = bc('k=v3; domain=www.example.com; path=/lol/cat/')
|
||||
cookie4 = bc('k=v4; domain=www.example.com; path=/lol/')
|
||||
|
||||
cj = cookiejar.CookieJar()
|
||||
cj = CookieJar()
|
||||
cj.set(cookie0)
|
||||
cj.set(cookie1)
|
||||
cj.set(cookie2)
|
||||
|
|
@ -369,6 +369,42 @@ def test_cookiejar():
|
|||
assert len(cj.all()) == 1
|
||||
|
||||
|
||||
def test_buildcookie():
|
||||
cj = CookieJar()
|
||||
"""
|
||||
Test cookie building
|
||||
"""
|
||||
c = cj.build('kk', 'vv', 'http://example.com/')
|
||||
assert c.domain == 'example.com'
|
||||
assert not c.secure
|
||||
assert c.path == '/'
|
||||
|
||||
c = cj.build('kk', 'vv', 'http://example.com/', path='/plop/', wildcard=True)
|
||||
assert c.domain == '.example.com'
|
||||
|
||||
assert c.path == '/plop/'
|
||||
c = cj.build('kk', 'vv', 'http://example.com/plop/')
|
||||
assert c.path == '/plop/'
|
||||
c = cj.build('kk', 'vv', 'http://example.com/plop/plap')
|
||||
assert c.path == '/plop/'
|
||||
c = cj.build('kk', 'vv', 'http://example.com/plop/?http://example.net/plip/')
|
||||
assert c.path == '/plop/'
|
||||
assert c.domain == 'example.com'
|
||||
c = cj.build('kk', 'vv', 'http://example.com/plop/plap', path='/')
|
||||
assert c.path == '/'
|
||||
|
||||
c = cj.build('kk', 'vv', 'https://example.com/')
|
||||
assert c.domain == 'example.com'
|
||||
assert c.secure
|
||||
|
||||
# check the cookie works
|
||||
c.name = 'k'
|
||||
c.value = 'v'
|
||||
cj.set(c)
|
||||
assert cj.for_request('https://example.com/') == {'k': 'v'}
|
||||
assert cj.for_request('http://example.com/') == {}
|
||||
|
||||
|
||||
def test_cookienav():
|
||||
"""
|
||||
Test browsing while getting new cookies
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue