web/test/unittest_request.py
changeset 7164 93a19c1831aa
child 8309 48ef505aa9f9
equal deleted inserted replaced
7162:62561ea082d2 7164:93a19c1831aa
       
     1 """misc. unittests for utility functions
       
     2 """
       
     3 
       
     4 from logilab.common.testlib import TestCase, unittest_main
       
     5 
       
     6 from functools import partial
       
     7 
       
     8 from cubicweb.web.request import (_parse_accept_header,
       
     9                                   _mimetype_sort_key, _mimetype_parser, _charset_sort_key)
       
    10 
       
    11 
       
    12 
       
    13 class AcceptParserTC(TestCase):
       
    14 
       
    15     def test_parse_accept(self):
       
    16         parse_accept_header = partial(_parse_accept_header,
       
    17                                       value_parser=_mimetype_parser,
       
    18                                       value_sort_key=_mimetype_sort_key)
       
    19         # compare scores
       
    20         self.assertEqual(parse_accept_header("audio/*;q=0.2, audio/basic"),
       
    21                          [( ('audio/basic', ('audio', 'basic', {}), 1.0 ) ),
       
    22                           ( ('audio/*', ('audio', '*', {}), 0.2 ) )])
       
    23         self.assertEqual(parse_accept_header("text/plain;q=0.5, text/html, text/x-dvi;q=0.8, text/x-c"),
       
    24                          [( ('text/html', ('text', 'html', {}), 1.0 ) ),
       
    25                           ( ('text/x-c', ('text', 'x-c', {}), 1.0 ) ),
       
    26                           ( ('text/x-dvi', ('text', 'x-dvi', {}), 0.8 ) ),
       
    27                           ( ('text/plain', ('text', 'plain', {}), 0.5 ) )])
       
    28         # compare mimetype precedence for a same given score
       
    29         self.assertEqual(parse_accept_header("audio/*, audio/basic"),
       
    30                          [( ('audio/basic', ('audio', 'basic', {}), 1.0 ) ),
       
    31                           ( ('audio/*', ('audio', '*', {}), 1.0 ) )])
       
    32         self.assertEqual(parse_accept_header("text/*, text/html, text/html;level=1, */*"),
       
    33                          [( ('text/html', ('text', 'html', {'level': '1'}), 1.0 ) ),
       
    34                           ( ('text/html', ('text', 'html', {}), 1.0 ) ),
       
    35                           ( ('text/*', ('text', '*', {}), 1.0 ) ),
       
    36                           ( ('*/*', ('*', '*', {}), 1.0 ) )])
       
    37         # free party
       
    38         self.assertEqual(parse_accept_header("text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"),
       
    39                          [( ('text/html', ('text', 'html', {'level': '1'}), 1.0 ) ),
       
    40                           ( ('text/html', ('text', 'html', {}), 0.7 ) ),
       
    41                           ( ('*/*', ('*', '*', {}), 0.5 ) ),
       
    42                           ( ('text/html', ('text', 'html', {'level': '2'}), 0.4 ) ),
       
    43                           ( ('text/*', ('text', '*', {}), 0.3 ) )
       
    44                           ])
       
    45         # chrome sample header
       
    46         self.assertEqual(parse_accept_header("application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"),
       
    47                          [( ('application/xhtml+xml', ('application', 'xhtml+xml', {}), 1.0 ) ),
       
    48                           ( ('application/xml', ('application', 'xml', {}), 1.0 ) ),
       
    49                           ( ('image/png', ('image', 'png', {}), 1.0 ) ),
       
    50                           ( ('text/html', ('text', 'html', {}), 0.9 ) ),
       
    51                           ( ('text/plain', ('text', 'plain', {}), 0.8 ) ),
       
    52                           ( ('*/*', ('*', '*', {}), 0.5 ) ),
       
    53                           ])
       
    54 
       
    55     def test_parse_accept_language(self):
       
    56         self.assertEqual(_parse_accept_header('fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'),
       
    57                          [('fr', 'fr', 1.0), ('fr-fr', 'fr-fr', 0.8),
       
    58                           ('en-us', 'en-us', 0.5), ('en', 'en', 0.3)])
       
    59 
       
    60     def test_parse_accept_charset(self):
       
    61         parse_accept_header = partial(_parse_accept_header,
       
    62                                       value_sort_key=_charset_sort_key)
       
    63         self.assertEqual(parse_accept_header('ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
       
    64                          [('ISO-8859-1', 'ISO-8859-1', 1.0),
       
    65                           ('utf-8', 'utf-8', 0.7),
       
    66                           ('*', '*', 0.7)])
       
    67 
       
    68 if __name__ == '__main__':
       
    69     unittest_main()