'Test parsing of Accept-Language'

def test_accept_language(self):

    from calibre.srv.http_response import preferred_lang
    from calibre.utils.localization import get_translator
    def test(name, val, ans):
        self.ae(preferred_lang(val, (lambda x: (True, x, None))), ans, (name + u'    failed'))
    test(u'Empty    field', u'', u'en')
    test(u'Simple', u'de', u'de')
    test(u'Case    insensitive', u'Es', u'es')
    test(u'Multiple', u'fr,    es', u'fr')
    test(u'Priority', u'en;q=0.1,    de;q=0.7,    fr;q=0.5', u'de')
    def handler(data):
        return (data.lang_code + data._(u'Unknown'))
    with TestServer(handler, timeout=0.3) as server:
        conn = server.connect()
        def test(al, q):
            conn.request(u'GET', u'/', headers={u'Accept-Language': al})
            r = conn.getresponse()
            self.ae(r.status, httplib.OK)
            q += get_translator(q)[(-1)].ugettext(u'Unknown')
            self.ae(r.read(), q)
        test(u'en', u'en')
        test(u'eng', u'en')
        test(u'es', u'es')
