def test_split_accept_header_multiple_values_and_quality():
    """Assert split_accept_header behavior with multiple Accept values and
    options

    Input: 'text/html,application/xml;q=0.8'
    Output: ['text/html', 'application/xml;q=0.8']

    Spaces are ignored.

    """
    accept_header = 'text/html,application/xml;q=0.8'
    result = split_accept_header(accept_header)

    assert next(result) == 'text/html'
    assert next(result) == 'application/xml;q=0.8'
    with raises(StopIteration):
        next(result)

    # Same with space
    accept_header = 'text/html, application/xml; q=0.8'
    result = split_accept_header(accept_header)

    assert next(result) == 'text/html'
    assert next(result) == 'application/xml;q=0.8'
    with raises(StopIteration):
        next(result)
def test_split_accept_header():
    """Assert split_accept_header basic behavior

    Input: 'text/html'
    Output: ['text/html']

    """
    accept_header = 'text/html'
    result = split_accept_header(accept_header)

    assert next(result) == 'text/html'
    with raises(StopIteration):
        next(result)
def test_split_accept_header_with_q():
    """Assert split_accept_header behavior with quality option

    Input: 'text/html;q=1.0'
    Output: ['text/html;q=1.0']

    """
    accept_header = 'text/html;q=1.0'
    result = split_accept_header(accept_header)

    assert next(result) == 'text/html;q=1.0'
    with raises(StopIteration):
        next(result)
Exemplo n.º 4
0
from __future__ import print_function, unicode_literals

from argparse import ArgumentParser

from http_accept import parse_accept_value, split_accept_header
from http_accept import HeaderAcceptList, HeaderAcceptValue


if __name__ == '__main__':
    parser = ArgumentParser(
        description='Show info of an Accept HTTP Header'
    )
    parser.add_argument('header')
    arguments = parser.parse_args()

    accepts = HeaderAcceptList(
        HeaderAcceptValue(
            info.get('mimetype'), **info.get('options', {})
        )
        for info in (
        parse_accept_value(value)
        for value in split_accept_header(arguments.header)
    ))

    for accept in accepts:
        print(
            '%s %s %s' % (accept.mimetype, accept.quality, ';'.join(
                '='.join((key, value)) for key, value in accept.options.items()
            ))
        )