Example #1
0
def test_filter_cookie_with_unicode_domain(loop) -> None:
    jar = CookieJar(loop=loop)
    jar.update_cookies(SimpleCookie(
        "idna-domain-first=first; Domain=xn--9caa.com; Path=/; "
    ))
    assert len(jar.filter_cookies(URL("http://éé.com"))) == 1
    assert len(jar.filter_cookies(URL("http://xn--9caa.com"))) == 1
Example #2
0
def test_filter_cookie_with_unicode_domain(loop):
    jar = CookieJar(loop=loop)
    jar.update_cookies(SimpleCookie(
        "idna-domain-first=first; Domain=xn--9caa.com; Path=/; "
    ))
    assert len(jar.filter_cookies(URL("http://éé.com"))) == 1
    assert len(jar.filter_cookies(URL("http://xn--9caa.com"))) == 1
Example #3
0
async def test_ignore_domain_ending_with_dot(loop: Any) -> None:
    jar = CookieJar(unsafe=True)
    jar.update_cookies(SimpleCookie("cookie=val; Domain=example.com.;"),
                       URL("http://www.example.com"))
    cookies_sent = jar.filter_cookies(URL("http://www.example.com/"))
    assert cookies_sent.output(header="Cookie:") == "Cookie: cookie=val"
    cookies_sent = jar.filter_cookies(URL("http://example.com/"))
    assert cookies_sent.output(header="Cookie:") == ""
def function573(arg2211):
    var3415 = CookieJar(loop=arg2211, unsafe=True)
    var3415.update_cookies(SimpleCookie('cookie=val; Domain=example.com.;'),
                           URL('http://www.example.com'))
    var1963 = var3415.filter_cookies(URL('http://www.example.com/'))
    assert (var1963.output(header='Cookie:') == 'Cookie: cookie=val')
    var1963 = var3415.filter_cookies(URL('http://example.com/'))
    assert (var1963.output(header='Cookie:') == '')
Example #5
0
def test_ignore_domain_ending_with_dot(loop):
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie("cookie=val; Domain=example.com.;"),
                       "http://www.example.com")
    cookies_sent = jar.filter_cookies("http://www.example.com/")
    assert cookies_sent.output(header='Cookie:') == "Cookie: cookie=val"
    cookies_sent = jar.filter_cookies("http://example.com/")
    assert cookies_sent.output(header='Cookie:') == ""
Example #6
0
def test_ignore_domain_ending_with_dot(loop) -> None:
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie("cookie=val; Domain=example.com.;"),
                       URL("http://www.example.com"))
    cookies_sent = jar.filter_cookies(URL("http://www.example.com/"))
    assert cookies_sent.output(header='Cookie:') == "Cookie: cookie=val"
    cookies_sent = jar.filter_cookies(URL("http://example.com/"))
    assert cookies_sent.output(header='Cookie:') == ""
Example #7
0
def _build_cookie_header(session, cookies, cookie_header, url):
    url, _ = strip_auth_from_url(url)
    all_cookies = session._cookie_jar.filter_cookies(url)
    if cookies is not None:
        tmp_cookie_jar = CookieJar()
        tmp_cookie_jar.update_cookies(cookies)
        req_cookies = tmp_cookie_jar.filter_cookies(url)
        if req_cookies:
            all_cookies.load(req_cookies)

    if not all_cookies and not cookie_header:
        return None

    c = SimpleCookie()
    if cookie_header:
        c.load(cookie_header)
    for name, value in all_cookies.items():
        if isinstance(value, Morsel):
            mrsl_val = value.get(value.key, Morsel())
            mrsl_val.set(value.key, value.value, value.coded_value)
            c[name] = mrsl_val
        else:
            c[name] = value

    return c.output(header="", sep=";").strip()
Example #8
0
def test_domain_filter_ip_cookie_send(loop):
    jar = CookieJar(loop=loop)
    cookies = SimpleCookie(
        "shared-cookie=first; "
        "domain-cookie=second; Domain=example.com; "
        "subdomain1-cookie=third; Domain=test1.example.com; "
        "subdomain2-cookie=fourth; Domain=test2.example.com; "
        "dotted-domain-cookie=fifth; Domain=.example.com; "
        "different-domain-cookie=sixth; Domain=different.org; "
        "secure-cookie=seventh; Domain=secure.com; Secure; "
        "no-path-cookie=eighth; Domain=pathtest.com; "
        "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
        "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
        "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
        "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
        "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
        " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
        "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
        " Max-Age=60; "
        "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
        " Max-Age=string; "
        "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
        " Expires=string;"
    )

    jar.update_cookies(cookies)
    cookies_sent = jar.filter_cookies("http://1.2.3.4/").output(
        header='Cookie:')
    assert cookies_sent == 'Cookie: shared-cookie=first'
Example #9
0
async def test_quotes_correctly_based_on_input(loop, cookies, expected,
                                               quote_bool) -> None:
    jar = CookieJar(unsafe=True, quote_cookie=quote_bool)
    jar.update_cookies(SimpleCookie(cookies))
    cookies_sent = jar.filter_cookies(
        URL("http://127.0.0.1/")).output(header='Cookie:')
    assert cookies_sent == expected
Example #10
0
class TestCookieJarBase(unittest.TestCase):

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        # N.B. those need to be overriden in child test cases
        self.jar = CookieJar(loop=self.loop)

    def tearDown(self):
        self.loop.close()

    def request_reply_with_same_url(self, url):
        self.jar.update_cookies(self.cookies_to_send)
        cookies_sent = self.jar.filter_cookies(url)

        self.jar.clear()

        self.jar.update_cookies(self.cookies_to_receive, url)
        cookies_received = SimpleCookie()
        for cookie in self.jar:
            dict.__setitem__(cookies_received, cookie.key, cookie)

        self.jar.clear()

        return cookies_sent, cookies_received
Example #11
0
async def test_secure_cookie_not_filtered_from_unsafe_cookiejar_when_given_unsecured_endpoint(
) -> None:
    """Secure SimpleCookie should not be filtered from unsafe CookieJar when given an unsecured endpoint.

    There are times when sending a secure cookie to an unsecured endpoint is desireable.  Such an
    occasion is during testing. RFC 6265 section-4.1.2.5 states that this behaviour is a decision
    based on the trust of a network by the user agent.
    """
    endpoint = 'http://127.0.0.1/'

    secure_cookie = SimpleCookie(
        "cookie-key=cookie-value; HttpOnly; Path=/; Secure", )

    jar = CookieJar(unsafe=True)

    # Confirm the jar is empty
    assert len(jar) == 0

    jar.update_cookies(
        secure_cookie,
        URL(endpoint),
    )

    # Confirm the jar contains the cookie
    assert len(jar) == 1

    filtered_cookies = jar.filter_cookies(request_url=endpoint)

    # Confirm the filtered results contain the cookie
    assert len(filtered_cookies) == 1
Example #12
0
def test_domain_filter_ip_cookie_send(loop) -> None:
    jar = CookieJar(loop=loop)
    cookies = SimpleCookie(
        "shared-cookie=first; "
        "domain-cookie=second; Domain=example.com; "
        "subdomain1-cookie=third; Domain=test1.example.com; "
        "subdomain2-cookie=fourth; Domain=test2.example.com; "
        "dotted-domain-cookie=fifth; Domain=.example.com; "
        "different-domain-cookie=sixth; Domain=different.org; "
        "secure-cookie=seventh; Domain=secure.com; Secure; "
        "no-path-cookie=eighth; Domain=pathtest.com; "
        "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
        "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
        "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
        "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
        "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
        " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
        "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
        " Max-Age=60; "
        "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
        " Max-Age=string; "
        "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
        " Expires=string;"
    )

    jar.update_cookies(cookies)
    cookies_sent = jar.filter_cookies(URL("http://1.2.3.4/")).output(
        header='Cookie:')
    assert cookies_sent == 'Cookie: shared-cookie=first'
def function2368(arg2126):
    var1128 = CookieJar(loop=arg2126, unsafe=True)
    var1128.update_cookies(
        SimpleCookie('ip-cookie="second"; Domain=127.0.0.1;'))
    var3020 = var1128.filter_cookies(
        URL('http://127.0.0.1/')).output(header='Cookie:')
    assert (var3020 == 'Cookie: ip-cookie="second"')
Example #14
0
class TestCookieJarBase(unittest.TestCase):

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        # N.B. those need to be overridden in child test cases
        self.jar = CookieJar(loop=self.loop)

    def tearDown(self):
        self.loop.close()

    def request_reply_with_same_url(self, url):
        self.jar.update_cookies(self.cookies_to_send)
        cookies_sent = self.jar.filter_cookies(URL(url))

        self.jar.clear()

        self.jar.update_cookies(self.cookies_to_receive, URL(url))
        cookies_received = SimpleCookie()
        for cookie in self.jar:
            dict.__setitem__(cookies_received, cookie.key, cookie)

        self.jar.clear()

        return cookies_sent, cookies_received
Example #15
0
def test_preserving_quoted_cookies(loop):
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie(
        "ip-cookie=\"second\"; Domain=127.0.0.1;"
    ))
    cookies_sent = jar.filter_cookies(URL("http://127.0.0.1/")).output(
        header='Cookie:')
    assert cookies_sent == 'Cookie: ip-cookie=\"second\"'
Example #16
0
def test_preserving_quoted_cookies(loop) -> None:
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie(
        "ip-cookie=\"second\"; Domain=127.0.0.1;"
    ))
    cookies_sent = jar.filter_cookies(URL("http://127.0.0.1/")).output(
        header='Cookie:')
    assert cookies_sent == 'Cookie: ip-cookie=\"second\"'
def function761(arg2357):
    var1898 = CookieJar(loop=arg2357)
    var1347 = SimpleCookie(
        'shared-cookie=first; domain-cookie=second; Domain=example.com; subdomain1-cookie=third; Domain=test1.example.com; subdomain2-cookie=fourth; Domain=test2.example.com; dotted-domain-cookie=fifth; Domain=.example.com; different-domain-cookie=sixth; Domain=different.org; secure-cookie=seventh; Domain=secure.com; Secure; no-path-cookie=eighth; Domain=pathtest.com; path1-cookie=nineth; Domain=pathtest.com; Path=/; path2-cookie=tenth; Domain=pathtest.com; Path=/one; path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; expires-cookie=thirteenth; Domain=expirestest.com; Path=/; Expires=Tue, 1 Jan 1980 12:00:00 GMT; max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/; Max-Age=60; invalid-max-age-cookie=fifteenth; Domain=invalid-values.com;  Max-Age=string; invalid-expires-cookie=sixteenth; Domain=invalid-values.com;  Expires=string;'
    )
    var1898.update_cookies(var1347)
    var25 = var1898.filter_cookies(
        URL('http://1.2.3.4/')).output(header='Cookie:')
    assert (var25 == 'Cookie: shared-cookie=first')
def function895(arg1205):
    var807 = CookieJar(loop=arg1205, unsafe=True)
    var807.update_cookies(
        SimpleCookie(
            'shared-cookie=first; ip-cookie=second; Domain=127.0.0.1;'))
    var1203 = var807.filter_cookies(
        URL('http://127.0.0.1/')).output(header='Cookie:')
    assert (
        var1203 == 'Cookie: ip-cookie=second\r\nCookie: shared-cookie=first')
Example #19
0
async def test_preserving_ip_domain_cookies(loop) -> None:
    jar = CookieJar(unsafe=True)
    jar.update_cookies(
        SimpleCookie("shared-cookie=first; "
                     "ip-cookie=second; Domain=127.0.0.1;"))
    cookies_sent = jar.filter_cookies(
        URL("http://127.0.0.1/")).output(header='Cookie:')
    assert cookies_sent == ('Cookie: ip-cookie=second\r\n'
                            'Cookie: shared-cookie=first')
Example #20
0
def test_preserving_ip_domain_cookies(loop) -> None:
    jar = CookieJar(loop=loop, unsafe=True)
    jar.update_cookies(SimpleCookie(
        "shared-cookie=first; "
        "ip-cookie=second; Domain=127.0.0.1;"
    ))
    cookies_sent = jar.filter_cookies(URL("http://127.0.0.1/")).output(
        header='Cookie:')
    assert cookies_sent == ('Cookie: ip-cookie=second\r\n'
                            'Cookie: shared-cookie=first')
Example #21
0
class Cookies(Serializable):
    jar: CookieJar

    def __init__(self) -> None:
        self.jar = CookieJar()

    def serialize(self) -> JSON:
        return {
            morsel.key: {
                **{k: v
                   for k, v in morsel.items() if v},
                "value": morsel.value,
            }
            for morsel in self.jar
        }

    @classmethod
    def deserialize(cls, raw: JSON) -> Cookies:
        cookie = SimpleCookie()
        for key, data in raw.items():
            cookie[key] = data.pop("value")
            cookie[key].update(data)
        cookies = cls()
        cookies.jar.update_cookies(cookie, ig_url)
        return cookies

    @property
    def csrf_token(self) -> str:
        try:
            return self["csrftoken"]
        except IGCookieNotFoundError:
            return "missing"

    @property
    def user_id(self) -> str:
        return self["ds_user_id"]

    @property
    def username(self) -> str:
        return self["ds_username"]

    def get(self, key: str) -> Morsel:
        filtered = self.jar.filter_cookies(ig_url)
        return filtered.get(key)

    def get_value(self, key: str) -> str | None:
        cookie = self.get(key)
        return cookie.value if cookie else None

    def __getitem__(self, key: str) -> str:
        cookie = self.get(key)
        if not cookie:
            raise IGCookieNotFoundError(key)
        return cookie.value
Example #22
0
async def test_treat_as_secure_origin() -> None:
    endpoint = URL("http://127.0.0.1/")

    jar = CookieJar(unsafe=True, treat_as_secure_origin=[endpoint])
    secure_cookie = SimpleCookie(
        "cookie-key=cookie-value; HttpOnly; Path=/; Secure", )

    jar.update_cookies(
        secure_cookie,
        endpoint,
    )

    assert len(jar) == 1
    filtered_cookies = jar.filter_cookies(request_url=endpoint)
    assert len(filtered_cookies) == 1
class Class134(unittest.TestCase):
    def function1020(self):
        self.attribute702 = asyncio.new_event_loop()
        asyncio.set_event_loop(None)
        self.attribute762 = CookieJar(loop=self.attribute702)

    def function249(self):
        self.attribute702.close()

    def function1281(self, arg284):
        self.attribute762.update_cookies(self.function674)
        var1595 = self.attribute762.filter_cookies(URL(arg284))
        self.attribute762.clear()
        self.attribute762.update_cookies(self.function1086, URL(arg284))
        var3164 = SimpleCookie()
        for var3580 in self.attribute762:
            dict.__setitem__(var3164, var3580.key, var3580)
        self.attribute762.clear()
        return (var1595, var3164)
Example #24
0
class TestCookieJarSafe(TestCookieJarBase):

    def setUp(self):
        super().setUp()

        self.cookies_to_send = SimpleCookie(
            "shared-cookie=first; "
            "domain-cookie=second; Domain=example.com; "
            "subdomain1-cookie=third; Domain=test1.example.com; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; "
            "dotted-domain-cookie=fifth; Domain=.example.com; "
            "different-domain-cookie=sixth; Domain=different.org; "
            "secure-cookie=seventh; Domain=secure.com; Secure; "
            "no-path-cookie=eighth; Domain=pathtest.com; "
            "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
            "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
            "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
            "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
            "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
            " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
            "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
            " Max-Age=60; "
            "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
            " Max-Age=string; "
            "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
            " Expires=string;"
        )

        self.cookies_to_receive = SimpleCookie(
            "unconstrained-cookie=first; Path=/; "
            "domain-cookie=second; Domain=example.com; Path=/; "
            "subdomain1-cookie=third; Domain=test1.example.com; Path=/; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; "
            "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; "
            "different-domain-cookie=sixth; Domain=different.org; Path=/; "
            "no-path-cookie=seventh; Domain=pathtest.com; "
            "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; "
            "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;"
        )

        self.jar = CookieJar(loop=self.loop)

    def timed_request(self, url, update_time, send_time):
        with mock.patch.object(self.loop, 'time', return_value=update_time):
            self.jar.update_cookies(self.cookies_to_send)

        with mock.patch.object(self.loop, 'time', return_value=send_time):
            cookies_sent = self.jar.filter_cookies(url)

        self.jar.clear()

        return cookies_sent

    def test_domain_filter_same_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_and_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://test1.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_diff_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_diff_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.org/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "different-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "different-domain-cookie"
        })

    def test_domain_filter_host_only(self):
        self.jar.update_cookies(self.cookies_to_receive, "http://example.com/")

        cookies_sent = self.jar.filter_cookies("http://example.com/")
        self.assertIn("unconstrained-cookie", set(cookies_sent.keys()))

        cookies_sent = self.jar.filter_cookies("http://different.org/")
        self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys()))

    def test_secure_filter(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

        cookies_sent, _ = (
            self.request_reply_with_same_url("https://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "secure-cookie"
        })

    def test_path_filter_root(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_filter_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/one/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie"
        })

    def test_path_filter_file(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie"
        })

    def test_path_filter_subfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_subsubfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/three/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_different_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/hundred/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_value(self):
        _, cookies_received = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "no-path-cookie",
            "path-cookie",
            "wrong-path-cookie"
        })

        self.assertEqual(cookies_received["no-path-cookie"]["path"], "/")
        self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath")
        self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/")

    def test_expires(self):
        ts_before = datetime.datetime(
            1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        ts_after = datetime.datetime(
            2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_before)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "expires-cookie"
        })

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_after)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_max_age(self):
        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 1000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "max-age-cookie"
        })

        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 2000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_invalid_values(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://invalid-values.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "invalid-max-age-cookie",
            "invalid-expires-cookie"
        })

        cookie = cookies_sent["invalid-max-age-cookie"]
        self.assertEqual(cookie["max-age"], "")

        cookie = cookies_sent["invalid-expires-cookie"]
        self.assertEqual(cookie["expires"], "")
Example #25
0
 async def _build_client_session(self, url: Union[str, URL]):
     ck = CookieJar(unsafe=True)
     ck.filter_cookies(url)
     self._client_session = ClientSession(cookie_jar=ck)
Example #26
0
async def test_filter_cookies_str_deprecated(loop) -> None:
    jar = CookieJar()
    with pytest.warns(DeprecationWarning):
        jar.filter_cookies("http://éé.com")
Example #27
0
class TestCookieJarSafe(TestCookieJarBase):

    def setUp(self):
        super().setUp()

        self.cookies_to_send = SimpleCookie(
            "shared-cookie=first; "
            "domain-cookie=second; Domain=example.com; "
            "subdomain1-cookie=third; Domain=test1.example.com; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; "
            "dotted-domain-cookie=fifth; Domain=.example.com; "
            "different-domain-cookie=sixth; Domain=different.org; "
            "secure-cookie=seventh; Domain=secure.com; Secure; "
            "no-path-cookie=eighth; Domain=pathtest.com; "
            "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
            "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
            "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
            "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
            "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
            " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
            "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
            " Max-Age=60; "
            "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
            " Max-Age=string; "
            "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
            " Expires=string;"
        )

        self.cookies_to_receive = SimpleCookie(
            "unconstrained-cookie=first; Path=/; "
            "domain-cookie=second; Domain=example.com; Path=/; "
            "subdomain1-cookie=third; Domain=test1.example.com; Path=/; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; "
            "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; "
            "different-domain-cookie=sixth; Domain=different.org; Path=/; "
            "no-path-cookie=seventh; Domain=pathtest.com; "
            "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; "
            "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;"
        )

        self.jar = CookieJar(loop=self.loop)

    def timed_request(self, url, update_time, send_time):
        with mock.patch.object(self.loop, 'time', return_value=update_time):
            self.jar.update_cookies(self.cookies_to_send)

        with mock.patch.object(self.loop, 'time', return_value=send_time):
            cookies_sent = self.jar.filter_cookies(URL(url))

        self.jar.clear()

        return cookies_sent

    def test_domain_filter_same_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_and_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://test1.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_diff_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_diff_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.org/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "different-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "different-domain-cookie"
        })

    def test_domain_filter_host_only(self):
        self.jar.update_cookies(self.cookies_to_receive,
                                URL("http://example.com/"))

        cookies_sent = self.jar.filter_cookies(URL("http://example.com/"))
        self.assertIn("unconstrained-cookie", set(cookies_sent.keys()))

        cookies_sent = self.jar.filter_cookies(URL("http://different.org/"))
        self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys()))

    def test_secure_filter(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

        cookies_sent, _ = (
            self.request_reply_with_same_url("https://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "secure-cookie"
        })

    def test_path_filter_root(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_filter_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/one/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie"
        })

    def test_path_filter_file(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie"
        })

    def test_path_filter_subfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_subsubfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/three/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_different_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/hundred/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_value(self):
        _, cookies_received = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "no-path-cookie",
            "path-cookie",
            "wrong-path-cookie"
        })

        self.assertEqual(cookies_received["no-path-cookie"]["path"], "/")
        self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath")
        self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/")

    def test_expires(self):
        ts_before = datetime.datetime(
            1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        ts_after = datetime.datetime(
            2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_before)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "expires-cookie"
        })

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_after)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_max_age(self):
        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 1000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "max-age-cookie"
        })

        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 2000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_invalid_values(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://invalid-values.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "invalid-max-age-cookie",
            "invalid-expires-cookie"
        })

        cookie = cookies_sent["invalid-max-age-cookie"]
        self.assertEqual(cookie["max-age"], "")

        cookie = cookies_sent["invalid-expires-cookie"]
        self.assertEqual(cookie["expires"], "")
Example #28
0
class TestCookieJarSafe(TestCookieJarBase):

    def setUp(self):
        super().setUp()

        self.cookies_to_send = SimpleCookie(
            "shared-cookie=first; "
            "domain-cookie=second; Domain=example.com; "
            "subdomain1-cookie=third; Domain=test1.example.com; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; "
            "dotted-domain-cookie=fifth; Domain=.example.com; "
            "different-domain-cookie=sixth; Domain=different.org; "
            "secure-cookie=seventh; Domain=secure.com; Secure; "
            "no-path-cookie=eighth; Domain=pathtest.com; "
            "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
            "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
            "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
            "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
            "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
            " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
            "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
            " Max-Age=60; "
            "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
            " Max-Age=string; "
            "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
            " Expires=string;"
        )

        self.cookies_to_receive = SimpleCookie(
            "unconstrained-cookie=first; Path=/; "
            "domain-cookie=second; Domain=example.com; Path=/; "
            "subdomain1-cookie=third; Domain=test1.example.com; Path=/; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; "
            "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; "
            "different-domain-cookie=sixth; Domain=different.org; Path=/; "
            "no-path-cookie=seventh; Domain=pathtest.com; "
            "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; "
            "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;"
        )

        self.jar = CookieJar(loop=self.loop)

    def timed_request(self, url, update_time, send_time):
        with mock.patch.object(self.loop, 'time', return_value=update_time):
            self.jar.update_cookies(self.cookies_to_send)

        with mock.patch.object(self.loop, 'time', return_value=send_time):
            cookies_sent = self.jar.filter_cookies(URL(url))

        self.jar.clear()

        return cookies_sent

    def test_domain_filter_same_host(self) -> None:
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_and_subdomain(self) -> None:
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://test1.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_diff_subdomain(self) -> None:
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_diff_host(self) -> None:
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.org/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "different-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "different-domain-cookie"
        })

    def test_domain_filter_host_only(self) -> None:
        self.jar.update_cookies(self.cookies_to_receive,
                                URL("http://example.com/"))

        cookies_sent = self.jar.filter_cookies(URL("http://example.com/"))
        self.assertIn("unconstrained-cookie", set(cookies_sent.keys()))

        cookies_sent = self.jar.filter_cookies(URL("http://different.org/"))
        self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys()))

    def test_secure_filter(self) -> None:
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

        cookies_sent, _ = (
            self.request_reply_with_same_url("https://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "secure-cookie"
        })

    def test_path_filter_root(self) -> None:
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_filter_folder(self) -> None:

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/one/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie"
        })

    def test_path_filter_file(self) -> None:

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie"
        })

    def test_path_filter_subfolder(self) -> None:

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_subsubfolder(self) -> None:

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/three/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_different_folder(self) -> None:

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/hundred/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_value(self) -> None:
        _, cookies_received = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "no-path-cookie",
            "path-cookie",
            "wrong-path-cookie"
        })

        self.assertEqual(cookies_received["no-path-cookie"]["path"], "/")
        self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath")
        self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/")

    def test_expires(self) -> None:
        ts_before = datetime.datetime(
            1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        ts_after = datetime.datetime(
            2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_before)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "expires-cookie"
        })

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_after)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_max_age(self) -> None:
        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 1000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "max-age-cookie"
        })

        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 2000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_invalid_values(self) -> None:
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://invalid-values.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "invalid-max-age-cookie",
            "invalid-expires-cookie"
        })

        cookie = cookies_sent["invalid-max-age-cookie"]
        self.assertEqual(cookie["max-age"], "")

        cookie = cookies_sent["invalid-expires-cookie"]
        self.assertEqual(cookie["expires"], "")

    def test_cookie_not_expired_when_added_after_removal(self) -> None:
        """Test case for https://github.com/aio-libs/aiohttp/issues/2084"""
        timestamps = [533588.993, 533588.993, 533588.993,
                      533588.993, 533589.093, 533589.093]

        loop = mock.Mock()
        loop.time.side_effect = itertools.chain(
            timestamps, itertools.cycle([timestamps[-1]]))

        jar = CookieJar(unsafe=True, loop=loop)
        # Remove `foo` cookie.
        jar.update_cookies(SimpleCookie('foo=""; Max-Age=0'))
        # Set `foo` cookie to `bar`.
        jar.update_cookies(SimpleCookie('foo="bar"'))

        # Assert that there is a cookie.
        assert len(jar) == 1
Example #29
0
async def test_filter_cookies_str_deprecated(loop) -> None:
    jar = CookieJar()
    with pytest.warns(DeprecationWarning):
        jar.filter_cookies("http://éé.com")
class Class109(Class134):
    def function829(self):
        super().function829()
        self.function674 = SimpleCookie(
            'shared-cookie=first; domain-cookie=second; Domain=example.com; subdomain1-cookie=third; Domain=test1.example.com; subdomain2-cookie=fourth; Domain=test2.example.com; dotted-domain-cookie=fifth; Domain=.example.com; different-domain-cookie=sixth; Domain=different.org; secure-cookie=seventh; Domain=secure.com; Secure; no-path-cookie=eighth; Domain=pathtest.com; path1-cookie=nineth; Domain=pathtest.com; Path=/; path2-cookie=tenth; Domain=pathtest.com; Path=/one; path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; expires-cookie=thirteenth; Domain=expirestest.com; Path=/; Expires=Tue, 1 Jan 1980 12:00:00 GMT; max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/; Max-Age=60; invalid-max-age-cookie=fifteenth; Domain=invalid-values.com;  Max-Age=string; invalid-expires-cookie=sixteenth; Domain=invalid-values.com;  Expires=string;'
        )
        self.function1086 = SimpleCookie(
            'unconstrained-cookie=first; Path=/; domain-cookie=second; Domain=example.com; Path=/; subdomain1-cookie=third; Domain=test1.example.com; Path=/; subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; dotted-domain-cookie=fifth; Domain=.example.com; Path=/; different-domain-cookie=sixth; Domain=different.org; Path=/; no-path-cookie=seventh; Domain=pathtest.com; path-cookie=eighth; Domain=pathtest.com; Path=/somepath; wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;'
        )
        self.attribute684 = CookieJar(loop=self.attribute702)

    def function2308(self, arg1432, arg554, arg2376):
        with mock.patch.object(self.attribute702, 'time', return_value=arg554):
            self.attribute684.update_cookies(self.function674)
        with mock.patch.object(self.attribute702, 'time',
                               return_value=arg2376):
            var1016 = self.attribute684.filter_cookies(URL(arg1432))
        self.attribute684.clear()
        return var1016

    def function1079(self):
        (var2086, var746) = self.function1281('http://example.com/')
        self.assertEqual(
            set(var2086.keys()),
            {'shared-cookie', 'domain-cookie', 'dotted-domain-cookie'})
        self.assertEqual(
            set(var746.keys()),
            {'unconstrained-cookie', 'domain-cookie', 'dotted-domain-cookie'})

    def function1904(self):
        (var2722, var4440) = self.function1281('http://test1.example.com/')
        self.assertEqual(
            set(var2722.keys()), {
                'shared-cookie', 'domain-cookie', 'subdomain1-cookie',
                'dotted-domain-cookie'
            })
        self.assertEqual(
            set(var4440.keys()), {
                'unconstrained-cookie', 'domain-cookie', 'subdomain1-cookie',
                'dotted-domain-cookie'
            })

    def function1154(self):
        (var1758, var4586) = self.function1281('http://different.example.com/')
        self.assertEqual(
            set(var1758.keys()),
            {'shared-cookie', 'domain-cookie', 'dotted-domain-cookie'})
        self.assertEqual(
            set(var4586.keys()),
            {'unconstrained-cookie', 'domain-cookie', 'dotted-domain-cookie'})

    def function41(self):
        (var1751, var2799) = self.function1281('http://different.org/')
        self.assertEqual(set(var1751.keys()),
                         {'shared-cookie', 'different-domain-cookie'})
        self.assertEqual(set(var2799.keys()),
                         {'unconstrained-cookie', 'different-domain-cookie'})

    def function1701(self):
        self.attribute684.update_cookies(self.function1086,
                                         URL('http://example.com/'))
        var4242 = self.attribute684.filter_cookies(URL('http://example.com/'))
        self.assertIn('unconstrained-cookie', set(var4242.keys()))
        var4242 = self.attribute684.filter_cookies(
            URL('http://different.org/'))
        self.assertNotIn('unconstrained-cookie', set(var4242.keys()))

    def function40(self):
        (var594, var1244) = self.function1281('http://secure.com/')
        self.assertEqual(set(var594.keys()), {'shared-cookie'})
        (var594, var1244) = self.function1281('https://secure.com/')
        self.assertEqual(set(var594.keys()),
                         {'shared-cookie', 'secure-cookie'})

    def function1512(self):
        (var3624, var3414) = self.function1281('http://pathtest.com/')
        self.assertEqual(set(var3624.keys()),
                         {'shared-cookie', 'no-path-cookie', 'path1-cookie'})

    def function2599(self):
        (var4506, var3655) = self.function1281('http://pathtest.com/one/')
        self.assertEqual(set(var4506.keys()), {
            'shared-cookie', 'no-path-cookie', 'path1-cookie', 'path2-cookie'
        })

    def function2738(self):
        (var3435, var2363) = self.function1281('http://pathtest.com/one/two')
        self.assertEqual(
            set(var3435.keys()), {
                'shared-cookie', 'no-path-cookie', 'path1-cookie',
                'path2-cookie', 'path3-cookie'
            })

    def function422(self):
        (var705, var3858) = self.function1281('http://pathtest.com/one/two/')
        self.assertEqual(
            set(var705.keys()), {
                'shared-cookie', 'no-path-cookie', 'path1-cookie',
                'path2-cookie', 'path3-cookie', 'path4-cookie'
            })

    def function2737(self):
        (var1973,
         var47) = self.function1281('http://pathtest.com/one/two/three/')
        self.assertEqual(
            set(var1973.keys()), {
                'shared-cookie', 'no-path-cookie', 'path1-cookie',
                'path2-cookie', 'path3-cookie', 'path4-cookie'
            })

    def function2630(self):
        (var2559, var4088) = self.function1281('http://pathtest.com/hundred/')
        self.assertEqual(set(var2559.keys()),
                         {'shared-cookie', 'no-path-cookie', 'path1-cookie'})

    def function1078(self):
        (var1919, var1124) = self.function1281('http://pathtest.com/')
        self.assertEqual(
            set(var1124.keys()), {
                'unconstrained-cookie', 'no-path-cookie', 'path-cookie',
                'wrong-path-cookie'
            })
        self.assertEqual(var1124['no-path-cookie']['path'], '/')
        self.assertEqual(var1124['path-cookie']['path'], '/somepath')
        self.assertEqual(var1124['wrong-path-cookie']['path'], '/')

    def function571(self):
        var1472 = datetime.datetime(1975, 1, 1,
                                    tzinfo=datetime.timezone.utc).timestamp()
        var4091 = datetime.datetime(2115, 1, 1,
                                    tzinfo=datetime.timezone.utc).timestamp()
        var2731 = self.function2308('http://expirestest.com/', var1472,
                                    var1472)
        self.assertEqual(set(var2731.keys()),
                         {'shared-cookie', 'expires-cookie'})
        var2731 = self.function2308('http://expirestest.com/', var1472,
                                    var4091)
        self.assertEqual(set(var2731.keys()), {'shared-cookie'})

    def function1280(self):
        var2264 = self.function2308('http://maxagetest.com/', 1000, 1000)
        self.assertEqual(set(var2264.keys()),
                         {'shared-cookie', 'max-age-cookie'})
        var2264 = self.function2308('http://maxagetest.com/', 1000, 2000)
        self.assertEqual(set(var2264.keys()), {'shared-cookie'})

    def function1453(self):
        (var3134, var2649) = self.function1281('http://invalid-values.com/')
        self.assertEqual(set(var3134.keys()), {
            'shared-cookie', 'invalid-max-age-cookie', 'invalid-expires-cookie'
        })
        var706 = var3134['invalid-max-age-cookie']
        self.assertEqual(var706['max-age'], '')
        var706 = var3134['invalid-expires-cookie']
        self.assertEqual(var706['expires'], '')
Example #31
0
class TestCookieJarSafe(TestCookieJarBase):

    def setUp(self):
        super().setUp()

        self.cookies_to_send = SimpleCookie(
            "shared-cookie=first; "
            "domain-cookie=second; Domain=example.com; "
            "subdomain1-cookie=third; Domain=test1.example.com; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; "
            "dotted-domain-cookie=fifth; Domain=.example.com; "
            "different-domain-cookie=sixth; Domain=different.org; "
            "secure-cookie=seventh; Domain=secure.com; Secure; "
            "no-path-cookie=eighth; Domain=pathtest.com; "
            "path1-cookie=nineth; Domain=pathtest.com; Path=/; "
            "path2-cookie=tenth; Domain=pathtest.com; Path=/one; "
            "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; "
            "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; "
            "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;"
            " Expires=Tue, 1 Jan 1980 12:00:00 GMT; "
            "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;"
            " Max-Age=60; "
            "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; "
            " Max-Age=string; "
            "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; "
            " Expires=string;"
        )

        self.cookies_to_receive = SimpleCookie(
            "unconstrained-cookie=first; Path=/; "
            "domain-cookie=second; Domain=example.com; Path=/; "
            "subdomain1-cookie=third; Domain=test1.example.com; Path=/; "
            "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; "
            "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; "
            "different-domain-cookie=sixth; Domain=different.org; Path=/; "
            "no-path-cookie=seventh; Domain=pathtest.com; "
            "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; "
            "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;"
        )

        self.jar = CookieJar(loop=self.loop)

    def timed_request(self, url, update_time, send_time):
        with mock.patch.object(self.loop, 'time', return_value=update_time):
            self.jar.update_cookies(self.cookies_to_send)

        with mock.patch.object(self.loop, 'time', return_value=send_time):
            cookies_sent = self.jar.filter_cookies(URL(url))

        self.jar.clear()

        return cookies_sent

    def test_domain_filter_same_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_and_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://test1.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "subdomain1-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_same_host_diff_subdomain(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.example.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "domain-cookie",
            "dotted-domain-cookie"
        })

    def test_domain_filter_diff_host(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://different.org/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "different-domain-cookie"
        })

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "different-domain-cookie"
        })

    def test_domain_filter_host_only(self):
        self.jar.update_cookies(self.cookies_to_receive,
                                URL("http://example.com/"))

        cookies_sent = self.jar.filter_cookies(URL("http://example.com/"))
        self.assertIn("unconstrained-cookie", set(cookies_sent.keys()))

        cookies_sent = self.jar.filter_cookies(URL("http://different.org/"))
        self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys()))

    def test_secure_filter(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

        cookies_sent, _ = (
            self.request_reply_with_same_url("https://secure.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "secure-cookie"
        })

    def test_path_filter_root(self):
        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_filter_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/one/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie"
        })

    def test_path_filter_file(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie"
        })

    def test_path_filter_subfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_subsubfolder(self):

        cookies_sent, _ = self.request_reply_with_same_url(
            "http://pathtest.com/one/two/three/")

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie",
            "path2-cookie",
            "path3-cookie",
            "path4-cookie"
        })

    def test_path_filter_different_folder(self):

        cookies_sent, _ = (
            self.request_reply_with_same_url("http://pathtest.com/hundred/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "no-path-cookie",
            "path1-cookie"
        })

    def test_path_value(self):
        _, cookies_received = (
            self.request_reply_with_same_url("http://pathtest.com/"))

        self.assertEqual(set(cookies_received.keys()), {
            "unconstrained-cookie",
            "no-path-cookie",
            "path-cookie",
            "wrong-path-cookie"
        })

        self.assertEqual(cookies_received["no-path-cookie"]["path"], "/")
        self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath")
        self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/")

    def test_expires(self):
        ts_before = datetime.datetime(
            1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        ts_after = datetime.datetime(
            2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp()

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_before)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "expires-cookie"
        })

        cookies_sent = self.timed_request(
            "http://expirestest.com/", ts_before, ts_after)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_max_age(self):
        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 1000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "max-age-cookie"
        })

        cookies_sent = self.timed_request(
            "http://maxagetest.com/", 1000, 2000)

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie"
        })

    def test_invalid_values(self):
        cookies_sent, cookies_received = (
            self.request_reply_with_same_url("http://invalid-values.com/"))

        self.assertEqual(set(cookies_sent.keys()), {
            "shared-cookie",
            "invalid-max-age-cookie",
            "invalid-expires-cookie"
        })

        cookie = cookies_sent["invalid-max-age-cookie"]
        self.assertEqual(cookie["max-age"], "")

        cookie = cookies_sent["invalid-expires-cookie"]
        self.assertEqual(cookie["expires"], "")

    def test_cookie_not_expired_when_added_after_removal(self):
        """Test case for https://github.com/aio-libs/aiohttp/issues/2084"""
        timestamps = [533588.993, 533588.993, 533588.993,
                      533588.993, 533589.093, 533589.093]

        loop = mock.Mock()
        loop.time.side_effect = itertools.chain(
            timestamps, itertools.cycle([timestamps[-1]]))

        jar = CookieJar(unsafe=True, loop=loop)
        # Remove `foo` cookie.
        jar.update_cookies(SimpleCookie('foo=""; Max-Age=0'))
        # Set `foo` cookie to `bar`.
        jar.update_cookies(SimpleCookie('foo="bar"'))

        # Assert that there is a cookie.
        assert len(jar) == 1
def function1288(arg118):
    var1555 = CookieJar(loop=arg118)
    var1555.update_cookies(
        SimpleCookie('idna-domain-first=first; Domain=xn--9caa.com; Path=/; '))
    assert (len(var1555.filter_cookies(URL('http://éé.com'))) == 1)
    assert (len(var1555.filter_cookies(URL('http://xn--9caa.com'))) == 1)