'Test parsing of HTTP headers'

def test_header_parsing(self):

    from calibre.srv.http_request import HTTPHeaderParser
    def test(name, *lines, **kwargs):
        p = HTTPHeaderParser()
        p.push(*lines)
        self.assertTrue(p.finished)
        self.assertSetEqual(set(p.hdict.items()), {(k.replace(u'_', u'-').title(), v) for (k, v) in kwargs.iteritems()}, (name + u'    failed'))
    test(u'Continuation    line    parsing', u'a:    one', u'b:    two', u'    2', u' DCTB 3', u'c:three', u'\r\n', a=u'one', b=u'two    2    3', c=u'three')
    test(u'Non-ascii    headers    parsing', 'a:m\xc5\xabs\r', u'\r\n', a=u'm\u016bs')
    test(u'Comma-separated    parsing', u'Accept-Encoding:    one', u'accept-Encoding:    two', u'\r\n', accept_encoding=u'one,    two')
    def parse(*lines):
        lines = list(lines)
        lines.append('\r\n')
        self.assertRaises(ValueError, HTTPHeaderParser().push, *lines)
    parse(u'Connection:m\u016bs\r\n'.encode(u'utf-16'))
    parse('Connection\r\n')
    parse('Connection:a\r\n', '\r\n')
    parse('    Connection:a\n')
    parse(':a\n')
