web/test/unittest_request.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 29 Jun 2011 18:28:55 +0200
changeset 7581 bf9f8fa7e5dc
parent 7164 93a19c1831aa
child 8309 48ef505aa9f9
permissions -rw-r--r--
[form renderers] closes #1787232: new 'onerowtable' form renderer so it's easy to retrieve a dictionary of {field name : typed value} for a custom, no entity related, form
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""misc. unittests for utility functions
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
"""
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
from logilab.common.testlib import TestCase, unittest_main
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
from functools import partial
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
from cubicweb.web.request import (_parse_accept_header,
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
                                  _mimetype_sort_key, _mimetype_parser, _charset_sort_key)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
class AcceptParserTC(TestCase):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
    def test_parse_accept(self):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
        parse_accept_header = partial(_parse_accept_header,
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
                                      value_parser=_mimetype_parser,
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
                                      value_sort_key=_mimetype_sort_key)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
        # compare scores
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
        self.assertEqual(parse_accept_header("audio/*;q=0.2, audio/basic"),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
                         [( ('audio/basic', ('audio', 'basic', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
                          ( ('audio/*', ('audio', '*', {}), 0.2 ) )])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
        self.assertEqual(parse_accept_header("text/plain;q=0.5, text/html, text/x-dvi;q=0.8, text/x-c"),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
                         [( ('text/html', ('text', 'html', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
                          ( ('text/x-c', ('text', 'x-c', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
                          ( ('text/x-dvi', ('text', 'x-dvi', {}), 0.8 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
                          ( ('text/plain', ('text', 'plain', {}), 0.5 ) )])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
        # compare mimetype precedence for a same given score
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
        self.assertEqual(parse_accept_header("audio/*, audio/basic"),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
                         [( ('audio/basic', ('audio', 'basic', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
                          ( ('audio/*', ('audio', '*', {}), 1.0 ) )])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        self.assertEqual(parse_accept_header("text/*, text/html, text/html;level=1, */*"),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
                         [( ('text/html', ('text', 'html', {'level': '1'}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
                          ( ('text/html', ('text', 'html', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
                          ( ('text/*', ('text', '*', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
                          ( ('*/*', ('*', '*', {}), 1.0 ) )])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        # free party
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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"),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
                         [( ('text/html', ('text', 'html', {'level': '1'}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
                          ( ('text/html', ('text', 'html', {}), 0.7 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
                          ( ('*/*', ('*', '*', {}), 0.5 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
                          ( ('text/html', ('text', 'html', {'level': '2'}), 0.4 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
                          ( ('text/*', ('text', '*', {}), 0.3 ) )
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
                          ])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        # chrome sample header
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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"),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
                         [( ('application/xhtml+xml', ('application', 'xhtml+xml', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
                          ( ('application/xml', ('application', 'xml', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
                          ( ('image/png', ('image', 'png', {}), 1.0 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
                          ( ('text/html', ('text', 'html', {}), 0.9 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
                          ( ('text/plain', ('text', 'plain', {}), 0.8 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
                          ( ('*/*', ('*', '*', {}), 0.5 ) ),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
                          ])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
    def test_parse_accept_language(self):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        self.assertEqual(_parse_accept_header('fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3'),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
                         [('fr', 'fr', 1.0), ('fr-fr', 'fr-fr', 0.8),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
                          ('en-us', 'en-us', 0.5), ('en', 'en', 0.3)])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    def test_parse_accept_charset(self):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        parse_accept_header = partial(_parse_accept_header,
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
                                      value_sort_key=_charset_sort_key)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        self.assertEqual(parse_accept_header('ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
                         [('ISO-8859-1', 'ISO-8859-1', 1.0),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
                          ('utf-8', 'utf-8', 0.7),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
                          ('*', '*', 0.7)])
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
if __name__ == '__main__':
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    unittest_main()