test_strings.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # coding=utf-8
  2. import pytest
  3. import six
  4. from library.python import strings
  5. if six.PY3:
  6. from urllib.parse import parse_qs, parse_qsl, unquote
  7. else:
  8. from urlparse import parse_qs, parse_qsl, unquote
  9. class Convertible(object):
  10. text = u'текст'
  11. text_utf8 = text.encode('utf-8')
  12. def __unicode__(self):
  13. return self.text
  14. def __str__(self):
  15. return self.text_utf8
  16. class ConvertibleToUnicodeOnly(Convertible):
  17. def __str__(self):
  18. return self.text.encode('ascii')
  19. class ConvertibleToStrOnly(Convertible):
  20. def __unicode__(self):
  21. return self.text_utf8.decode('ascii')
  22. class NonConvertible(ConvertibleToUnicodeOnly, ConvertibleToStrOnly):
  23. pass
  24. def test_to_basestring():
  25. assert strings.to_basestring('str') == 'str'
  26. assert strings.to_basestring(u'юникод') == u'юникод'
  27. if six.PY2: # __str__ should return str not bytes in Python3
  28. assert strings.to_basestring(Convertible()) == Convertible.text
  29. assert strings.to_basestring(ConvertibleToUnicodeOnly()) == Convertible.text
  30. assert strings.to_basestring(ConvertibleToStrOnly()) == Convertible.text_utf8
  31. assert strings.to_basestring(NonConvertible())
  32. def test_to_unicode():
  33. assert strings.to_unicode(u'юникод') == u'юникод'
  34. assert strings.to_unicode('str') == u'str'
  35. assert strings.to_unicode(u'строка'.encode('utf-8')) == u'строка'
  36. assert strings.to_unicode(u'строка'.encode('cp1251'), 'cp1251') == u'строка'
  37. if six.PY2: # __str__ should return str not bytes in Python3
  38. assert strings.to_unicode(Convertible()) == Convertible.text
  39. assert strings.to_unicode(ConvertibleToUnicodeOnly()) == Convertible.text
  40. with pytest.raises(UnicodeDecodeError):
  41. strings.to_unicode(ConvertibleToStrOnly())
  42. with pytest.raises(UnicodeDecodeError):
  43. strings.to_unicode(NonConvertible())
  44. def test_to_unicode_errors_replace():
  45. assert strings.to_unicode(u'abcабв'.encode('utf-8'), 'ascii')
  46. assert strings.to_unicode(u'абв'.encode('utf-8'), 'ascii')
  47. def test_to_str():
  48. assert strings.to_str('str') == 'str' if six.PY2 else b'str'
  49. assert strings.to_str(u'unicode') == 'unicode' if six.PY2 else b'unicode'
  50. assert strings.to_str(u'юникод') == u'юникод'.encode('utf-8')
  51. assert strings.to_str(u'юникод', 'cp1251') == u'юникод'.encode('cp1251')
  52. if six.PY2:
  53. assert strings.to_str(Convertible()) == Convertible.text_utf8
  54. with pytest.raises(UnicodeEncodeError):
  55. strings.to_str(ConvertibleToUnicodeOnly())
  56. assert strings.to_str(ConvertibleToStrOnly()) == Convertible.text_utf8
  57. with pytest.raises(UnicodeEncodeError):
  58. strings.to_str(NonConvertible())
  59. def test_to_str_errors_replace():
  60. assert strings.to_str(u'abcабв', 'ascii')
  61. assert strings.to_str(u'абв', 'ascii')
  62. def test_to_str_transcode():
  63. assert strings.to_str('str', from_enc='ascii') == 'str' if six.PY2 else b'str'
  64. assert strings.to_str('str', from_enc='utf-8') == 'str' if six.PY2 else b'str'
  65. assert strings.to_str(u'юникод'.encode('utf-8'), from_enc='utf-8') == u'юникод'.encode('utf-8')
  66. assert strings.to_str(u'юникод'.encode('utf-8'), to_enc='utf-8', from_enc='utf-8') == u'юникод'.encode('utf-8')
  67. assert strings.to_str(u'юникод'.encode('utf-8'), to_enc='cp1251', from_enc='utf-8') == u'юникод'.encode('cp1251')
  68. assert strings.to_str(u'юникод'.encode('cp1251'), from_enc='cp1251') == u'юникод'.encode('utf-8')
  69. assert strings.to_str(u'юникод'.encode('cp1251'), to_enc='cp1251', from_enc='cp1251') == u'юникод'.encode('cp1251')
  70. assert strings.to_str(u'юникод'.encode('cp1251'), to_enc='utf-8', from_enc='cp1251') == u'юникод'.encode('utf-8')
  71. assert strings.to_str(u'юникод'.encode('koi8-r'), from_enc='koi8-r') == u'юникод'.encode('utf-8')
  72. assert strings.to_str(u'юникод'.encode('koi8-r'), to_enc='koi8-r', from_enc='koi8-r') == u'юникод'.encode('koi8-r')
  73. assert strings.to_str(u'юникод'.encode('koi8-r'), to_enc='cp1251', from_enc='koi8-r') == u'юникод'.encode('cp1251')
  74. def test_to_str_transcode_wrong():
  75. assert strings.to_str(u'юникод'.encode('utf-8'), from_enc='cp1251')
  76. assert strings.to_str(u'юникод'.encode('cp1251'), from_enc='utf-8')
  77. def test_to_str_transcode_disabled():
  78. # No transcoding enabled, set from_enc to enable
  79. assert strings.to_str(u'юникод'.encode('utf-8'), to_enc='utf-8') == u'юникод'.encode('utf-8')
  80. assert strings.to_str(u'юникод'.encode('utf-8'), to_enc='cp1251') == u'юникод'.encode('utf-8')
  81. assert strings.to_str(u'юникод'.encode('cp1251'), to_enc='utf-8') == u'юникод'.encode('cp1251')
  82. assert strings.to_str(u'юникод'.encode('cp1251'), to_enc='cp1251') == u'юникод'.encode('cp1251')
  83. assert strings.to_str(u'юникод'.encode('cp1251'), to_enc='koi8-r') == u'юникод'.encode('cp1251')
  84. assert strings.to_str(u'юникод'.encode('koi8-r'), to_enc='cp1251') == u'юникод'.encode('koi8-r')
  85. def test_stringize_deep():
  86. assert strings.stringize_deep(
  87. {
  88. 'key 1': 'value 1',
  89. u'ключ 2': u'значение 2',
  90. 'list': [u'ключ 2', 'key 1', (u'к', 2)],
  91. }
  92. ) == {
  93. 'key 1' if six.PY2 else b'key 1': 'value 1' if six.PY2 else b'value 1',
  94. u'ключ 2'.encode('utf-8'): u'значение 2'.encode('utf-8'),
  95. ('list' if six.PY2 else b'list'): [
  96. u'ключ 2'.encode('utf-8'),
  97. 'key 1' if six.PY2 else b'key 1',
  98. (u'к'.encode('utf-8'), 2),
  99. ],
  100. }
  101. def test_stringize_deep_doesnt_transcode():
  102. assert strings.stringize_deep(
  103. {
  104. u'ключ 1'.encode('utf-8'): u'значение 1'.encode('utf-8'),
  105. u'ключ 2'.encode('cp1251'): u'значение 2'.encode('cp1251'),
  106. }
  107. ) == {
  108. u'ключ 1'.encode('utf-8'): u'значение 1'.encode('utf-8'),
  109. u'ключ 2'.encode('cp1251'): u'значение 2'.encode('cp1251'),
  110. }
  111. def test_stringize_deep_nested():
  112. assert strings.stringize_deep(
  113. {
  114. 'key 1': 'value 1',
  115. u'ключ 2': {
  116. 'subkey 1': 'value 1',
  117. u'подключ 2': u'value 2',
  118. },
  119. }
  120. ) == {
  121. 'key 1' if six.PY2 else b'key 1': 'value 1' if six.PY2 else b'value 1',
  122. u'ключ 2'.encode('utf-8'): {
  123. ('subkey 1' if six.PY2 else b'subkey 1'): 'value 1' if six.PY2 else b'value 1',
  124. u'подключ 2'.encode('utf-8'): u'value 2'.encode('utf-8'),
  125. },
  126. }
  127. def test_stringize_deep_plain():
  128. assert strings.stringize_deep('str') == 'str' if six.PY2 else b'str'
  129. assert strings.stringize_deep(u'юникод') == u'юникод'.encode('utf-8')
  130. assert strings.stringize_deep(u'юникод'.encode('utf-8')) == u'юникод'.encode('utf-8')
  131. def test_stringize_deep_nonstr():
  132. with pytest.raises(TypeError):
  133. strings.stringize_deep(Convertible(), relaxed=False)
  134. x = Convertible()
  135. assert x == strings.stringize_deep(x)
  136. def test_unicodize_deep():
  137. assert strings.unicodize_deep(
  138. {
  139. 'key 1': 'value 1',
  140. u'ключ 2': u'значение 2',
  141. u'ключ 3'.encode('utf-8'): u'значение 3'.encode('utf-8'),
  142. }
  143. ) == {
  144. u'key 1': u'value 1',
  145. u'ключ 2': u'значение 2',
  146. u'ключ 3': u'значение 3',
  147. }
  148. def test_unicodize_deep_nested():
  149. assert strings.unicodize_deep(
  150. {
  151. 'key 1': 'value 1',
  152. u'ключ 2': {
  153. 'subkey 1': 'value 1',
  154. u'подключ 2': u'значение 2',
  155. u'подключ 3'.encode('utf-8'): u'значение 3'.encode('utf-8'),
  156. },
  157. }
  158. ) == {
  159. u'key 1': u'value 1',
  160. u'ключ 2': {
  161. u'subkey 1': u'value 1',
  162. u'подключ 2': u'значение 2',
  163. u'подключ 3': u'значение 3',
  164. },
  165. }
  166. def test_unicodize_deep_plain():
  167. assert strings.unicodize_deep('str') == u'str'
  168. assert strings.unicodize_deep(u'юникод') == u'юникод'
  169. assert strings.unicodize_deep(u'юникод'.encode('utf-8')) == u'юникод'
  170. def test_unicodize_deep_nonstr():
  171. with pytest.raises(TypeError):
  172. strings.unicodize_deep(Convertible(), relaxed=False)
  173. x = Convertible()
  174. assert x == strings.stringize_deep(x)
  175. truncate_utf_8_data = [
  176. ("hello", 5, None, None, "hello"),
  177. ("hello", 6, None, None, "hello"),
  178. ("hello", 4, None, None, "h..."),
  179. ("hello", 4, None, "", "hell"),
  180. ("hello", 4, None, ".", "hel."),
  181. ("hello", 4, strings.Whence.End, ".", "hel."),
  182. ("hello", 5, strings.Whence.Start, None, "hello"),
  183. ("hello", 4, strings.Whence.Start, None, "...o"),
  184. ("hello", 4, strings.Whence.Start, ".", ".llo"),
  185. ("yoloha", 5, strings.Whence.Middle, None, "y...a"),
  186. ("hello", 5, strings.Whence.Middle, None, "hello"),
  187. ("hello", 4, strings.Whence.Middle, None, "h..."),
  188. ("hello", 4, strings.Whence.Middle, ".", "he.o"),
  189. # destroyed symbol code must be removed
  190. ("меледа", 4, None, None, "..."),
  191. ("меледа", 5, None, None, "м..."),
  192. ("меледа", 7, None, None, "ме..."),
  193. ("меледа", 12, None, None, "меледа"),
  194. ("меледа", 4, None, ".", "м."),
  195. ("меледа", 5, None, "ак", "ак"),
  196. ("меледа", 6, None, "ак", "мак"),
  197. ("меледа", 4, strings.Whence.Start, None, "..."),
  198. ("меледа", 5, strings.Whence.Start, None, "...а"),
  199. ("меледа", 12, strings.Whence.Start, None, "меледа"),
  200. ("меледа", 9, strings.Whence.Start, ".", ".леда"),
  201. ("меледа", 10, strings.Whence.Start, ".", ".леда"),
  202. # half code from symbol 'м' plus half from symbol 'а' - nothing in the end
  203. ("меледа", 5, strings.Whence.Middle, None, "..."),
  204. ("меледа", 6, strings.Whence.Middle, None, "м..."),
  205. ("меледа", 7, strings.Whence.Middle, None, "м...а"),
  206. ("меледа", 12, strings.Whence.Middle, None, "меледа"),
  207. ("меледа", 8, strings.Whence.Middle, ".", "ме.а"),
  208. ("меледа", 9, strings.Whence.Middle, ".", "ме.да"),
  209. ("меледа", 10, strings.Whence.Middle, ".", "ме.да"),
  210. ("меледа", 11, strings.Whence.Middle, ".", "ме.да"),
  211. (u"меледа", 6, strings.Whence.Middle, None, "м..."),
  212. (u"меледа", 12, strings.Whence.Middle, None, "меледа"),
  213. (u"меледа", 8, strings.Whence.Middle, ".", "ме.а"),
  214. ]
  215. @pytest.mark.parametrize("data, limit, Whence, msg, expected", truncate_utf_8_data)
  216. def test_truncate_utf_8_text(data, limit, Whence, msg, expected):
  217. assert strings.truncate(data, limit, Whence, msg) == expected
  218. def test_truncate_utf_8_text_wrong_limit():
  219. with pytest.raises(AssertionError):
  220. strings.truncate("hell", 2)
  221. with pytest.raises(AssertionError):
  222. strings.truncate("hello", 4, msg="long msg")
  223. @pytest.mark.parametrize(
  224. "given,expected",
  225. [
  226. (
  227. b"a=a",
  228. [(b"a", b"a")],
  229. ),
  230. (
  231. b"a=a&a=b",
  232. [(b"a", b"a"), (b"a", b"b")],
  233. ),
  234. (
  235. b"a=a+&b=b++",
  236. [(b"a", b"a "), (b"b", b"b ")],
  237. ),
  238. (
  239. b"a=a&&b=b",
  240. [(b"a", b"a"), (b"b", b"b")],
  241. ),
  242. (
  243. b"a=a&b=%%3C%2Fscript%3E",
  244. [(b"a", b"a"), (b"b", b"%</script>")],
  245. ),
  246. (
  247. b"clid=%EF%BB%BF123",
  248. [(b"clid", b"\xef\xbb\xbf123")],
  249. ),
  250. ],
  251. )
  252. def test_parse_qsl(given, expected):
  253. assert strings.parse_qsl_binary(given) == expected
  254. @pytest.mark.parametrize(
  255. "given,expected,keep_blank_values",
  256. [
  257. (b"a=", {}, False),
  258. (b"a=", {b"a": [b""]}, True),
  259. (b"a", {}, False),
  260. (b"a", {b"a": [b""]}, True),
  261. (b"a=a&a=b", {b"a": [b"a", b"b"]}, False),
  262. ],
  263. )
  264. def test_parse_qs_with_keep_blank_values(given, expected, keep_blank_values):
  265. assert strings.parse_qs_binary(given, keep_blank_values=keep_blank_values) == expected
  266. @pytest.mark.parametrize(
  267. "given,strict_parsing",
  268. [(b"a", True)],
  269. )
  270. def test_parse_qs_with_strict_parsing(given, strict_parsing):
  271. with pytest.raises(ValueError, match="bad query field.*"):
  272. strings.parse_qs_binary(given, strict_parsing=strict_parsing)
  273. with pytest.raises(ValueError, match="bad query field.*"):
  274. parse_qs(given, strict_parsing=strict_parsing)
  275. @pytest.mark.parametrize(
  276. "given,max_num_fields",
  277. [(b"a=a&b=bb&c=c", 2)],
  278. )
  279. def test_parse_qs_with_max_num_fields(given, max_num_fields):
  280. with pytest.raises(ValueError, match="Max number of fields exceeded"):
  281. strings.parse_qs_binary(given, max_num_fields=max_num_fields)
  282. with pytest.raises(ValueError, match="Max number of fields exceeded"):
  283. parse_qs(given, max_num_fields=max_num_fields)
  284. @pytest.mark.parametrize(
  285. "given,expected",
  286. [
  287. (
  288. b"",
  289. b"",
  290. ),
  291. (
  292. b"without percent",
  293. b"without percent",
  294. ),
  295. (
  296. b"%61 and %62",
  297. b"a and b",
  298. ),
  299. (
  300. b"%FF can't %unparse char%",
  301. b"\xff can't %unparse char%",
  302. ),
  303. ],
  304. )
  305. def test_unquote(given, expected):
  306. assert strings.unquote_binary(given) == expected
  307. URL_PARAMS = [
  308. (b"a=", False, False, None),
  309. (b"a=a&a=b", False, False, None),
  310. (b"a=a&a=b&b=b", False, False, None),
  311. (b"a=a&&b=b", False, False, None),
  312. (b"a=a&b=%%3C%2Fscript%3E", False, False, None),
  313. (b"a=", True, False, None),
  314. (b"a", False, False, None),
  315. (b"a", True, False, None),
  316. ]
  317. @pytest.mark.parametrize(
  318. "string,keep_blank_values,strict_parsing,max_num_fields",
  319. URL_PARAMS if six.PY3 else URL_PARAMS + [(b"clid=%EF%BB%BF123", False, False, None)],
  320. )
  321. def test_parse_qs_compatibility(string, keep_blank_values, strict_parsing, max_num_fields):
  322. for string_method, urlparse_method in (strings.parse_qsl_binary, parse_qsl), (strings.parse_qs_binary, parse_qs):
  323. string_res = string_method(
  324. string,
  325. keep_blank_values=keep_blank_values,
  326. strict_parsing=strict_parsing,
  327. max_num_fields=max_num_fields,
  328. )
  329. urlparse_res = urlparse_method(
  330. string,
  331. keep_blank_values=keep_blank_values,
  332. strict_parsing=strict_parsing,
  333. max_num_fields=max_num_fields,
  334. )
  335. assert string_res == urlparse_res
  336. @pytest.mark.parametrize(
  337. "string",
  338. [
  339. (b""),
  340. (b"without percent"),
  341. (b"a and b"),
  342. ((b"%FF " if six.PY2 else b"") + b"can't %unparse char%"),
  343. ],
  344. )
  345. def test_unquote_compatibility(string):
  346. unquote_res = unquote(string)
  347. if six.PY3:
  348. unquote_res = six.ensure_binary(unquote_res)
  349. assert strings.unquote_binary(string) == unquote_res