author | Rémi Cardona <remi.cardona@logilab.fr> |
Mon, 21 Sep 2015 17:54:15 +0200 | |
changeset 10721 | e9abbaa835f5 |
parent 8309 | 48ef505aa9f9 |
permissions | -rw-r--r-- |
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 |
|
8309
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
8 |
from cubicweb.devtools.fake import FakeConfig |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
9 |
|
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
10 |
from cubicweb.web.request import (CubicWebRequestBase, _parse_accept_header, |
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
11 |
_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
|
12 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
13 |
|
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 |
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
|
16 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
17 |
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
|
18 |
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
|
19 |
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
|
20 |
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
|
21 |
# compare scores |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
22 |
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
|
23 |
[( ('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
|
24 |
( ('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
|
25 |
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
|
26 |
[( ('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
|
27 |
( ('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
|
28 |
( ('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
|
29 |
( ('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
|
30 |
# 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
|
31 |
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
|
32 |
[( ('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
|
33 |
( ('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
|
34 |
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
|
35 |
[( ('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
|
36 |
( ('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
|
37 |
( ('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
|
38 |
( ('*/*', ('*', '*', {}), 1.0 ) )]) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
39 |
# free party |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
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"), |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
41 |
[( ('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
|
42 |
( ('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
|
43 |
( ('*/*', ('*', '*', {}), 0.5 ) ), |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
44 |
( ('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
|
45 |
( ('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
|
46 |
]) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
47 |
# 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
|
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"), |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
49 |
[( ('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
|
50 |
( ('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
|
51 |
( ('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
|
52 |
( ('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
|
53 |
( ('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
|
54 |
( ('*/*', ('*', '*', {}), 0.5 ) ), |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
55 |
]) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
56 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
[('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
|
60 |
('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
|
61 |
|
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
[('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
|
67 |
('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
|
68 |
('*', '*', 0.7)]) |
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
69 |
|
8309
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
70 |
def test_base_url(self): |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
71 |
dummy_vreg = type('DummyVreg', (object,), {})() |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
72 |
dummy_vreg.config = FakeConfig() |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
73 |
dummy_vreg.config['base-url'] = 'http://babar.com/' |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
74 |
dummy_vreg.config['https-url'] = 'https://toto.com/' |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
75 |
|
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
76 |
req = CubicWebRequestBase(dummy_vreg, https=False) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
77 |
self.assertEqual('http://babar.com/', req.base_url()) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
78 |
self.assertEqual('http://babar.com/', req.base_url(False)) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
79 |
self.assertEqual('https://toto.com/', req.base_url(True)) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
80 |
|
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
81 |
req = CubicWebRequestBase(dummy_vreg, https=True) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
82 |
self.assertEqual('https://toto.com/', req.base_url()) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
83 |
self.assertEqual('http://babar.com/', req.base_url(False)) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
84 |
self.assertEqual('https://toto.com/', req.base_url(True)) |
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
85 |
|
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
86 |
|
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
7164
diff
changeset
|
87 |
|
7164
93a19c1831aa
[http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff
changeset
|
88 |
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
|
89 |
unittest_main() |