def test_get_cookies_for_url_ignores_secure_cookies_for_http():
    jar = CookieJar()

    jar.add(URL(b"https://foo.org"), Cookie(b"hello", b"world", secure=True))

    cookies = list(jar.get_cookies_for_url(URL(b"http://foo.org/hello-world")))
    assert len(cookies) == 0
Beispiel #2
0
 def base_url(self, value):
     url = None
     if value and not isinstance(value, URL):
         if isinstance(value, str):
             url = URL(value.encode())
         else:
             url = URL(value)
     self._base_url = url
def test_check_permanent_redirects():
    client = ClientSession()
    client._permanent_redirects_urls._cache[b"/foo"] = URL(b"https://somewhere.org")

    request = Request("GET", b"/foo", None)
    assert request.url == URL(b"/foo")

    client.check_permanent_redirects(request)
    assert request.url == URL(b"https://somewhere.org")
Beispiel #4
0
def test_get_cookies_for_url():
    jar = CookieJar()

    jar.add(URL(b"https://foo.org"), Cookie("hello", "world"))

    cookies = list(jar.get_cookies_for_url(URL(b"https://foo.org/hello-world")))

    assert len(cookies) == 1
    assert cookies[0].name == "hello"
    assert cookies[0].value == "world"
Beispiel #5
0
def test_get_cookies_for_url():
    jar = CookieJar()

    jar.add(URL(b'https://foo.org'), Cookie(b'hello', b'world'))

    cookies = list(jar.get_cookies_for_url(
        URL(b'https://foo.org/hello-world')))

    assert len(cookies) == 1
    assert cookies[0].name == b'hello'
    assert cookies[0].value == b'world'
def test_cookiejar_ignores_cookie_domain_set_as_ipaddress():
    jar = CookieJar()

    assert (jar.get_domain(
        URL(b"https://foo.org/hello-world"),
        Cookie(b"foo", b"foo", domain=b"192.168.1.5"),
    ) == b"foo.org")
Beispiel #7
0
async def test_client_session_validate_url_for_relative_urls_with_base_url():
    async with ClientSession(base_url=b"https://foo.org") as client:
        request = Request("GET", b"/home", None)

        client._validate_request_url(request)

        assert request.url == URL(b"https://foo.org/home")
Beispiel #8
0
    async def get_connection(self, url: URL) -> ClientConnection:
        pool = self.pools.get_pool(url.schema, url.host, url.port, self.ssl)

        try:
            return await asyncio.wait_for(pool.get_connection(),
                                          self.connection_timeout)
        except TimeoutError:
            raise ConnectionTimeout(url.base_url(), self.connection_timeout)
Beispiel #9
0
 def _validate_request_url(self, request: Request):
     if not request.url.is_absolute:
         if self.base_url:
             request.url = URL(self._get_url_without_params(request.url))
         else:
             raise ValueError(
                 'request.url must be a complete, absolute URL. Either provide a base_url '
                 'for the client, or specify a full URL for the request.')
Beispiel #10
0
 def _validate_request_url(self, request: Request):
     if not request.url.is_absolute:
         if self.base_url:
             request.url = URL(self.get_url_value(request.url))
         else:
             raise ValueError(
                 "request.url must be a complete, absolute URL. "
                 "Either provide a base_url "
                 "for the client, or specify a full URL for the request.")
def test_cookie_jar_does_not_override_http_only_cookie_with_non_http_only_cookie(
):
    jar = CookieJar()

    jar.add(
        URL(b"https://foo.org"),
        Cookie(
            b"hello",
            b"world",
            expires=datetime_to_cookie_format(datetime.utcnow() +
                                              timedelta(days=2)),
            http_only=True,
        ),
    )

    jar.add(
        URL(b"https://foo.org"),
        Cookie(
            b"hello",
            b"world2",
            expires=datetime_to_cookie_format(datetime.utcnow() +
                                              timedelta(days=2)),
            http_only=True,
        ),
    )

    cookie = jar.get(b"foo.org", b"/", b"hello")
    assert cookie is not None
    assert cookie.cookie.value == b"world2"

    jar.add(
        URL(b"https://foo.org"),
        Cookie(
            b"hello",
            b"world modified",
            expires=datetime_to_cookie_format(datetime.utcnow() +
                                              timedelta(days=2)),
            http_only=False,
        ),
    )

    cookie = jar.get(b"foo.org", b"/", b"hello")
    assert cookie is not None
    assert cookie.cookie.value == b"world2"
Beispiel #12
0
    def __init__(self,
                 loop=None,
                 url=None,
                 ssl=None,
                 pools=None,
                 default_headers: Optional[List[Header]] = None,
                 follow_redirects: bool = True,
                 connection_timeout: float = 5.0,
                 request_timeout: float = 60.0,
                 maximum_redirects: int = 20,
                 redirects_cache_type: Union[Type[RedirectsCache], Any] = None,
                 cookie_jar: CookieJar = None,
                 middlewares: Optional[List[Callable]] = None):
        if loop is None:
            loop = asyncio.get_event_loop()

        if url and not isinstance(url, URL):
            if isinstance(url, str):
                url = url.encode()
            url = URL(url)

        if not pools:
            pools = ClientConnectionPools(loop)

        if redirects_cache_type is None and follow_redirects:
            redirects_cache_type = RedirectsCache

        if middlewares is None:
            middlewares = []

        if cookie_jar is None:
            cookie_jar = CookieJar()

        if cookie_jar:
            middlewares.insert(0, cookies_middleware)

        self.loop = loop
        self.base_url = url
        self.ssl = ssl
        self.default_headers = Headers(default_headers)
        self.pools = pools
        self.connection_timeout = connection_timeout
        self.request_timeout = request_timeout
        self.follow_redirects = follow_redirects
        self.cookie_jar = cookie_jar
        self._permanent_redirects_urls = redirects_cache_type(
        ) if follow_redirects else None
        self.non_standard_handling_of_301_302_redirect_method = True
        self.maximum_redirects = maximum_redirects
        self._handler = None
        self._middlewares = middlewares
        if middlewares:
            self._build_middlewares_chain()
        self.delay_before_retry = 0.5
Beispiel #13
0
 def extract_redirect_location(response: Response):
     location = response.headers[b'Location']
     if not location:
         raise MissingLocationForRedirect(response)
     # if the server returned more than one value, use the last header in order
     # if the location cannot be parsed as URL, let exception happen: this might be a redirect to a URN!!
     # simply don't follows the redirect, and returns the response to the caller
     try:
         return URL(location[-1].value)
     except InvalidURL:
         raise UnsupportedRedirect()
Beispiel #14
0
    def get_url_value(self, url: Union[AnyStr, URL]) -> bytes:
        if isinstance(url, str):
            url = url.encode()

        if not isinstance(url, URL):
            url = URL(url)

        if url.is_absolute:
            return url.value

        if self.base_url:
            return self.base_url.join(url).value
        return url.value
Beispiel #15
0
    def _get_url_without_params(self, url) -> bytes:
        if isinstance(url, str):
            url = url.encode()

        if not isinstance(url, URL):
            url = URL(url)

        if url.is_absolute:
            return url.value

        if self.base_url:
            return self.base_url.join(url).value
        return url.value
Beispiel #16
0
    def extract_redirect_location(response: Response) -> URL:
        # if the server returned more than one value, use
        # the first header in order
        location = response.get_first_header(b"Location")
        if not location:
            raise MissingLocationForRedirect(response)

        # if the location cannot be parsed as URL, let exception happen:
        # this might be a redirect to a URN!
        # simply don't follows the redirect, and returns the response to
        # the caller
        try:
            return URL(location)
        except InvalidURL:
            raise UnsupportedRedirect(location)
def test_cookie_jar_get_cookie_default_path(value, expected_result):
    assert CookieJar.get_cookie_default_path(URL(value)) == expected_result
Beispiel #18
0
def test_redirects_cache(source, destination):
    cache = RedirectsCache()
    cache[source] = URL(destination)
    assert cache[source] == URL(destination)
Beispiel #19
0
import pytest
from datetime import datetime, timedelta
from blacksheep import (Request, Response, Headers, Header, Cookie, URL,
                        TextContent, datetime_to_cookie_format)
from blacksheep.client import ClientSession, CircularRedirectError, MaximumRedirectsExceededError
from blacksheep.client.cookies import CookieJar, InvalidCookie, InvalidCookieDomain, StoredCookie
from blacksheep.scribe import write_response_cookie
from . import FakePools


@pytest.mark.parametrize('request_url,cookie_domain,expected_domain', [
    [URL(b'https://bezkitu.org'), None, b'bezkitu.org'],
    [URL(b'https://foo.bezkitu.org'), b'foo.bezkitu.org', b'foo.bezkitu.org'],
    [URL(b'https://foo.bezkitu.org'), b'bezkitu.org', b'bezkitu.org'],
    [URL(b'https://foo.bezkitu.org'), b'bezkitu.org.', b'foo.bezkitu.org'],
])
def test_cookiejar_get_domain(request_url, cookie_domain, expected_domain):
    jar = CookieJar()
    cookie = Cookie(b'Name', b'Value', domain=cookie_domain)
    domain = jar.get_domain(request_url, cookie)
    assert domain == expected_domain


@pytest.mark.parametrize(
    'request_url,cookie_domain',
    [[URL(b'https://bezkitu.org'), b'example.com'],
     [URL(b'https://foo.bezkitu.org'), b'baz.foo.bezkitu.org'],
     [URL(b'https://foo.bezkitu.org'), b'foo.org']])
def test_cookiejar_invalid_domain(request_url, cookie_domain):
    jar = CookieJar()
    cookie = Cookie(b'Name', b'Value', domain=cookie_domain)
)
from blacksheep.client import ClientSession
from blacksheep.client.cookies import (
    CookieJar,
    InvalidCookieDomain,
    MissingSchemeInURL,
    StoredCookie,
)
from blacksheep.scribe import write_response_cookie
from . import FakePools


@pytest.mark.parametrize(
    "request_url,cookie_domain,expected_domain",
    [
        [URL(b"https://bezkitu.org"), None, b"bezkitu.org"],
        [
            URL(b"https://foo.bezkitu.org"), b"foo.bezkitu.org",
            b"foo.bezkitu.org"
        ],
        [URL(b"https://foo.bezkitu.org"), b"bezkitu.org", b"bezkitu.org"],
        [URL(b"https://foo.bezkitu.org"), b"bezkitu.org.", b"foo.bezkitu.org"],
    ],
)
def test_cookiejar_get_domain(request_url, cookie_domain, expected_domain):
    jar = CookieJar()
    cookie = Cookie(b"Name", b"Value", domain=cookie_domain)
    domain = jar.get_domain(request_url, cookie)
    assert domain == expected_domain

Beispiel #21
0
 def base_url(self, value: Union[str, URL]):
     if value and not isinstance(value, URL):
         if isinstance(value, str):
             value = value.encode()
         value = URL(value)
     self._base_url = value
def test_cookie_jar_throws_for_url_without_host():
    jar = CookieJar()

    with pytest.raises(MissingSchemeInURL):
        jar.get_cookies_for_url(URL(b"/"))