cubicweb/web/test/unittest_request.py
changeset 11057 0b59724cb3f2
parent 8309 48ef505aa9f9
child 11794 d8830e2bd2e0
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     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.devtools.fake import FakeConfig
       
     9 
       
    10 from cubicweb.web.request import (CubicWebRequestBase, _parse_accept_header,
       
    11                                   _mimetype_sort_key, _mimetype_parser, _charset_sort_key)
       
    12 
       
    13 
       
    14 
       
    15 class AcceptParserTC(TestCase):
       
    16 
       
    17     def test_parse_accept(self):
       
    18         parse_accept_header = partial(_parse_accept_header,
       
    19                                       value_parser=_mimetype_parser,
       
    20                                       value_sort_key=_mimetype_sort_key)
       
    21         # compare scores
       
    22         self.assertEqual(parse_accept_header("audio/*;q=0.2, audio/basic"),
       
    23                          [( ('audio/basic', ('audio', 'basic', {}), 1.0 ) ),
       
    24                           ( ('audio/*', ('audio', '*', {}), 0.2 ) )])
       
    25         self.assertEqual(parse_accept_header("text/plain;q=0.5, text/html, text/x-dvi;q=0.8, text/x-c"),
       
    26                          [( ('text/html', ('text', 'html', {}), 1.0 ) ),
       
    27                           ( ('text/x-c', ('text', 'x-c', {}), 1.0 ) ),
       
    28                           ( ('text/x-dvi', ('text', 'x-dvi', {}), 0.8 ) ),
       
    29                           ( ('text/plain', ('text', 'plain', {}), 0.5 ) )])
       
    30         # compare mimetype precedence for a same given score
       
    31         self.assertEqual(parse_accept_header("audio/*, audio/basic"),
       
    32                          [( ('audio/basic', ('audio', 'basic', {}), 1.0 ) ),
       
    33                           ( ('audio/*', ('audio', '*', {}), 1.0 ) )])
       
    34         self.assertEqual(parse_accept_header("text/*, text/html, text/html;level=1, */*"),
       
    35                          [( ('text/html', ('text', 'html', {'level': '1'}), 1.0 ) ),
       
    36                           ( ('text/html', ('text', 'html', {}), 1.0 ) ),
       
    37                           ( ('text/*', ('text', '*', {}), 1.0 ) ),
       
    38                           ( ('*/*', ('*', '*', {}), 1.0 ) )])
       
    39         # free party
       
    40         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"),
       
    41                          [( ('text/html', ('text', 'html', {'level': '1'}), 1.0 ) ),
       
    42                           ( ('text/html', ('text', 'html', {}), 0.7 ) ),
       
    43                           ( ('*/*', ('*', '*', {}), 0.5 ) ),
       
    44                           ( ('text/html', ('text', 'html', {'level': '2'}), 0.4 ) ),
       
    45                           ( ('text/*', ('text', '*', {}), 0.3 ) )
       
    46                           ])
       
    47         # chrome sample header
       
    48         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"),
       
    49                          [( ('application/xhtml+xml', ('application', 'xhtml+xml', {}), 1.0 ) ),
       
    50                           ( ('application/xml', ('application', 'xml', {}), 1.0 ) ),
       
    51                           ( ('image/png', ('image', 'png', {}), 1.0 ) ),
       
    52                           ( ('text/html', ('text', 'html', {}), 0.9 ) ),
       
    53                           ( ('text/plain', ('text', 'plain', {}), 0.8 ) ),
       
    54                           ( ('*/*', ('*', '*', {}), 0.5 ) ),
       
    55                           ])
       
    56 
       
    57     def test_parse_accept_language(self):
       
    58         self.assertEqual(_parse_accept_header('fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'),
       
    59                          [('fr', 'fr', 1.0), ('fr-fr', 'fr-fr', 0.8),
       
    60                           ('en-us', 'en-us', 0.5), ('en', 'en', 0.3)])
       
    61 
       
    62     def test_parse_accept_charset(self):
       
    63         parse_accept_header = partial(_parse_accept_header,
       
    64                                       value_sort_key=_charset_sort_key)
       
    65         self.assertEqual(parse_accept_header('ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
       
    66                          [('ISO-8859-1', 'ISO-8859-1', 1.0),
       
    67                           ('utf-8', 'utf-8', 0.7),
       
    68                           ('*', '*', 0.7)])
       
    69 
       
    70     def test_base_url(self):
       
    71         dummy_vreg = type('DummyVreg', (object,), {})()
       
    72         dummy_vreg.config = FakeConfig()
       
    73         dummy_vreg.config['base-url'] = 'http://babar.com/'
       
    74         dummy_vreg.config['https-url'] = 'https://toto.com/'
       
    75 
       
    76         req = CubicWebRequestBase(dummy_vreg, https=False)
       
    77         self.assertEqual('http://babar.com/', req.base_url())
       
    78         self.assertEqual('http://babar.com/', req.base_url(False))
       
    79         self.assertEqual('https://toto.com/', req.base_url(True))
       
    80 
       
    81         req = CubicWebRequestBase(dummy_vreg, https=True)
       
    82         self.assertEqual('https://toto.com/', req.base_url())
       
    83         self.assertEqual('http://babar.com/', req.base_url(False))
       
    84         self.assertEqual('https://toto.com/', req.base_url(True))
       
    85 
       
    86 
       
    87 
       
    88 if __name__ == '__main__':
       
    89     unittest_main()