Repository: httpie/httpie
File: httpie/utils.py (Python)

Code before:

class ExplicitNullAuth(requests.auth.AuthBase):
    """Forces requests to ignore the ``.netrc``.
    <https://github.com/psf/requests/issues/2773#issuecomment-174312831>
    """

    def __call__(self, r):
        return r


def get_content_type(filename):
    """
    Return the content type for ``filename`` in format appropriate
    for Content-Type headers, or ``None`` if the file type is unknown
    to ``mimetypes``.

    """
    mime, encoding = mimetypes.guess_type(filename, strict=False)
    if mime:
        content_type = mime
        if encoding:
            content_type = '%s; charset=%s' % (mime, encoding)
        return content_type


def get_expired_cookies(response):
    original = response.raw._original_response
    expired_cookies = []
    cookie_headers = []
    curr_timestamp = time.time()

    for header_name, content in original.msg._headers:
        if header_name == 'Set-Cookie':
            cookie_headers.append(content)

    extracted_cookies = parse_ns_headers(cookie_headers)

    for cookie in extracted_cookies:
        cookie_header = dict((key, val) for key, val in cookie)
        if "expires" in cookie_header and cookie_header['expires'] <= curr_timestamp:
            cookie_name = cookie[0][0]
            cookie_path = cookie_header.get('path', '/')
            expired_cookies.append({'name': cookie_name, 'path': cookie_path})

    return expired_cookies


Review comment (bug):
Since this function only looks at the headers, it doesn’t need access to the entire response. It would be good to refactor so that it only takes the headers. Or even just the value of the `Set-Cookie` header. 

This change will also make it easier to write tests for the function. There should be a few unit tests: e.g., passing in a mix of expired / unexpired cookies and looking for edge cases. You will probably have to allow passing the current timestamp as an optional argument for the test.

Also, could you please add parameter and return type annotations?


Code after:


class ExplicitNullAuth(requests.auth.AuthBase):
    """Forces requests to ignore the ``.netrc``.
    <https://github.com/psf/requests/issues/2773#issuecomment-174312831>
    """

    def __call__(self, r):
        return r


def get_content_type(filename):
    """
    Return the content type for ``filename`` in format appropriate
    for Content-Type headers, or ``None`` if the file type is unknown
    to ``mimetypes``.

    """
    mime, encoding = mimetypes.guess_type(filename, strict=False)
    if mime:
        content_type = mime
        if encoding:
            content_type = '%s; charset=%s' % (mime, encoding)
        return content_type


def get_expired_cookies(headers: List[Tuple[str, str]], curr_timestamp: float = None) -> List[dict]:
    expired_cookies = []
    cookie_headers = []
    curr_timestamp = curr_timestamp or time.time()

    for header_name, content in headers:
        if header_name == 'Set-Cookie':
            cookie_headers.append(content)

    extracted_cookies = [
        dict(cookie, name=cookie[0][0])
        for cookie in parse_ns_headers(cookie_headers)
    ]

    for cookie in extracted_cookies:
        if "expires" in cookie and cookie['expires'] <= curr_timestamp:
            expired_cookies.append({
                'name': cookie['name'],
                'path': cookie.get('path', '/')
            })

    return expired_cookies