# Path: impala/util.py
# def cookie_matches_path(c, path):
#     if 'path' not in c or not c['path']:
#         return True
#     cookie_path = c['path'].strip()
#     if not cookie_path.startswith('/'):
#         cookie_path = '/' + cookie_path
#     cookie_path = os.path.normpath(cookie_path)
#     if cookie_path == '/':
#         return True
#     if not path.startswith('/'):
#         path = '/' + path
#     path = os.path.normpath(path)
#     return path == cookie_path or path.startswith(cookie_path + '/')
# 
# def get_cookie_expiry(c):
#     if 'max-age' in c and c['max-age']:
#         try:
#             max_age_sec = int(c['max-age'])
#             return datetime.datetime.now() + datetime.timedelta(seconds=max_age_sec)
#         except:
#             pass
#     # TODO: implement support for 'expires' cookie attribute as well.
#     return None
# 
# def get_all_matching_cookies(cookie_names, path, resp_headers):
#     if 'Set-Cookie' not in resp_headers:
#         return None
# 
#     cookies = http_cookies.SimpleCookie()
#     try:
#         cookies.load(resp_headers['Set-Cookie'])
#     except:
#         return None
# 
#     matching_cookies = []
#     for cn in cookie_names:
#         if cn in cookies:
#             c = cookies[cn]
#             if c and cookie_matches_path(c, path):
#                 matching_cookies.append(c)
#     return matching_cookies


import sys
    import unittest2 as unittest
    import unittest
import pytest
from datetime import datetime, timedelta
from impala.util import cookie_matches_path, get_cookie_expiry, get_all_matching_cookies
        assert cookie_matches_path({'path': '/cliservice/'}, '/cliservice/')
        assert cookie_matches_path({'path': '/cliservice'}, 'cliservice/abc/def/ghi')
        assert cookie_matches_path({'path': '/cliservice/'}, 'cliservice/?q=abcd')

        assert not cookie_matches_path({'path': '/a/b/c//'}, '/a')
        assert not cookie_matches_path({'path': '/a/b/c//'}, '/a/b')
        assert cookie_matches_path({'path': '/a/b/c//'}, 'a/b/c')
        assert cookie_matches_path({'path': '/a/b/c//'}, '/a/b/c')
        assert cookie_matches_path({'path': '/a/b/c//'}, 'a/b/c/d')
        assert cookie_matches_path({'path': '/a/b/c//'}, '/a/b/c/d')
        assert cookie_matches_path({'path': '/a/b/c//'}, '/a/b/c/d/e/f/g/h')

    def test_get_cookie_expiry(self):
        assert get_cookie_expiry({}) is None
        assert get_cookie_expiry({'max-age': ''}) is None

        hour = timedelta(hours=1)
        sec = timedelta(seconds=1)
        now = datetime.now()
        assert now <= get_cookie_expiry({'max-age': '0'}) <= now + sec
        now = datetime.now()
        assert now - sec <= get_cookie_expiry({'max-age': '-1'}) <= now

        now = datetime.now()
        assert now - hour <= get_cookie_expiry({'max-age': '-3600'}) <= now - hour + sec
        now = datetime.now()
        assert now + hour <= get_cookie_expiry({'max-age': '3600'}) <= now + hour + sec

        now = datetime.now()
        days2k = timedelta(days=2000)

# Path: impala/util.py
# def cookie_matches_path(c, path):
#     if 'path' not in c or not c['path']:
#         return True
#     cookie_path = c['path'].strip()
#     if not cookie_path.startswith('/'):
#         cookie_path = '/' + cookie_path
#     cookie_path = os.path.normpath(cookie_path)
#     if cookie_path == '/':
#         return True
#     if not path.startswith('/'):
#         path = '/' + path
#     path = os.path.normpath(path)
#     return path == cookie_path or path.startswith(cookie_path + '/')
# 
# def get_cookie_expiry(c):
#     if 'max-age' in c and c['max-age']:
#         try:
#             max_age_sec = int(c['max-age'])
#             return datetime.datetime.now() + datetime.timedelta(seconds=max_age_sec)
#         except:
#             pass
#     # TODO: implement support for 'expires' cookie attribute as well.
#     return None
# 
# def get_all_matching_cookies(cookie_names, path, resp_headers):
#     if 'Set-Cookie' not in resp_headers:
#         return None
# 
#     cookies = http_cookies.SimpleCookie()
#     try:
#         cookies.load(resp_headers['Set-Cookie'])
#     except:
#         return None
# 
#     matching_cookies = []
#     for cn in cookie_names:
#         if cn in cookies:
#             c = cookies[cn]
#             if c and cookie_matches_path(c, path):
#                 matching_cookies.append(c)
#     return matching_cookies


# Path: impala/tests/test_util.py
import sys
    import unittest2 as unittest
    import unittest
import pytest
from datetime import datetime, timedelta
from impala.util import cookie_matches_path, get_cookie_expiry, get_all_matching_cookies

        assert cookie_matches_path({'path': '/cliservice/'}, '/cliservice/')
        assert cookie_matches_path({'path': '/cliservice'}, 'cliservice/abc/def/ghi')
        assert cookie_matches_path({'path': '/cliservice/'}, 'cliservice/?q=abcd')

        assert not cookie_matches_path({'path': '/a/b/c//'}, '/a')
        assert not cookie_matches_path({'path': '/a/b/c//'}, '/a/b')
        assert cookie_matches_path({'path': '/a/b/c//'}, 'a/b/c')
        assert cookie_matches_path({'path': '/a/b/c//'}, '/a/b/c')
        assert cookie_matches_path({'path': '/a/b/c//'}, 'a/b/c/d')
        assert cookie_matches_path({'path': '/a/b/c//'}, '/a/b/c/d')
        assert cookie_matches_path({'path': '/a/b/c//'}, '/a/b/c/d/e/f/g/h')

    def test_get_cookie_expiry(self):
        assert get_cookie_expiry({}) is None
        assert get_cookie_expiry({'max-age': ''}) is None

        hour = timedelta(hours=1)
        sec = timedelta(seconds=1)
        now = datetime.now()
        assert now <= get_cookie_expiry({'max-age': '0'}) <= now + sec
        now = datetime.now()
        assert now - sec <= get_cookie_expiry({'max-age': '-1'}) <= now

        now = datetime.now()
        assert now - hour <= get_cookie_expiry({'max-age': '-3600'}) <= now - hour + sec
        now = datetime.now()
        assert now + hour <= get_cookie_expiry({'max-age': '3600'}) <= now + hour + sec

        now = datetime.now()
        days2k = timedelta(days=2000)

        assert now - days2k <= get_cookie_expiry({'max-age': '-172800000'}) <= now - days2k + sec