test_url_build.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import pytest
  2. from yarl import URL
  3. # build classmethod
  4. def test_build_without_arguments():
  5. u = URL.build()
  6. assert str(u) == ""
  7. def test_build_simple():
  8. u = URL.build(scheme="http", host="127.0.0.1")
  9. assert str(u) == "http://127.0.0.1"
  10. def test_build_with_scheme():
  11. u = URL.build(scheme="blob", path="path")
  12. assert str(u) == "blob:path"
  13. def test_build_with_host():
  14. u = URL.build(host="127.0.0.1")
  15. assert str(u) == "//127.0.0.1"
  16. assert u == URL("//127.0.0.1")
  17. def test_build_with_scheme_and_host():
  18. u = URL.build(scheme="http", host="127.0.0.1")
  19. assert str(u) == "http://127.0.0.1"
  20. assert u == URL("http://127.0.0.1")
  21. @pytest.mark.parametrize(
  22. ("port", "exc", "match"),
  23. [
  24. pytest.param(
  25. 8000,
  26. ValueError,
  27. r"""(?x)
  28. ^
  29. Can't\ build\ URL\ with\ "port"\ but\ without\ "host"\.
  30. $
  31. """,
  32. id="port-only",
  33. ),
  34. pytest.param(
  35. "", TypeError, r"^The port is required to be int\.$", id="port-str"
  36. ),
  37. ],
  38. )
  39. def test_build_with_port(port, exc, match):
  40. print(match)
  41. with pytest.raises(exc, match=match):
  42. URL.build(port=port)
  43. def test_build_with_user():
  44. u = URL.build(scheme="http", host="127.0.0.1", user="foo")
  45. assert str(u) == "http://foo@127.0.0.1"
  46. def test_build_with_user_password():
  47. u = URL.build(scheme="http", host="127.0.0.1", user="foo", password="bar")
  48. assert str(u) == "http://foo:bar@127.0.0.1"
  49. def test_build_with_query_and_query_string():
  50. with pytest.raises(ValueError):
  51. URL.build(
  52. scheme="http",
  53. host="127.0.0.1",
  54. user="foo",
  55. password="bar",
  56. port=8000,
  57. path="/index.html",
  58. query=dict(arg="value1"),
  59. query_string="arg=value1",
  60. fragment="top",
  61. )
  62. def test_build_with_all():
  63. u = URL.build(
  64. scheme="http",
  65. host="127.0.0.1",
  66. user="foo",
  67. password="bar",
  68. port=8000,
  69. path="/index.html",
  70. query_string="arg=value1",
  71. fragment="top",
  72. )
  73. assert str(u) == "http://foo:bar@127.0.0.1:8000/index.html?arg=value1#top"
  74. def test_build_with_authority_and_host():
  75. with pytest.raises(ValueError):
  76. URL.build(authority="host.com", host="example.com")
  77. def test_build_with_authority():
  78. url = URL.build(scheme="http", authority="степан:bar@host.com:8000", path="path")
  79. assert (
  80. str(url) == "http://%D1%81%D1%82%D0%B5%D0%BF%D0%B0%D0%BD:bar@host.com:8000/path"
  81. )
  82. def test_build_with_authority_without_encoding():
  83. url = URL.build(
  84. scheme="http", authority="foo:bar@host.com:8000", path="path", encoded=True
  85. )
  86. assert str(url) == "http://foo:bar@host.com:8000/path"
  87. def test_query_str():
  88. u = URL.build(scheme="http", host="127.0.0.1", path="/", query_string="arg=value1")
  89. assert str(u) == "http://127.0.0.1/?arg=value1"
  90. def test_query_dict():
  91. u = URL.build(scheme="http", host="127.0.0.1", path="/", query=dict(arg="value1"))
  92. assert str(u) == "http://127.0.0.1/?arg=value1"
  93. def test_build_path_quoting():
  94. u = URL.build(
  95. scheme="http",
  96. host="127.0.0.1",
  97. path="/фотографія.jpg",
  98. query=dict(arg="Привіт"),
  99. )
  100. assert u == URL("http://127.0.0.1/фотографія.jpg?arg=Привіт")
  101. assert str(u) == (
  102. "http://127.0.0.1/"
  103. "%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D1%96%D1%8F.jpg?"
  104. "arg=%D0%9F%D1%80%D0%B8%D0%B2%D1%96%D1%82"
  105. )
  106. def test_build_query_quoting():
  107. u = URL.build(
  108. scheme="http",
  109. host="127.0.0.1",
  110. path="/фотографія.jpg",
  111. query="arg=Привіт",
  112. )
  113. assert u == URL("http://127.0.0.1/фотографія.jpg?arg=Привіт")
  114. assert str(u) == (
  115. "http://127.0.0.1/"
  116. "%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D1%96%D1%8F.jpg?"
  117. "arg=%D0%9F%D1%80%D0%B8%D0%B2%D1%96%D1%82"
  118. )
  119. def test_build_query_only():
  120. u = URL.build(query={"key": "value"})
  121. assert str(u) == "?key=value"
  122. def test_build_drop_dots():
  123. u = URL.build(scheme="http", host="example.com", path="/path/../to")
  124. assert str(u) == "http://example.com/to"
  125. def test_build_encode():
  126. u = URL.build(
  127. scheme="http",
  128. host="оун-упа.укр",
  129. path="/шлях/криївка",
  130. query_string="ключ=знач",
  131. fragment="фраг",
  132. )
  133. expected = (
  134. "http://xn----8sb1bdhvc.xn--j1amh"
  135. "/%D1%88%D0%BB%D1%8F%D1%85/%D0%BA%D1%80%D0%B8%D1%97%D0%B2%D0%BA%D0%B0"
  136. "?%D0%BA%D0%BB%D1%8E%D1%87=%D0%B7%D0%BD%D0%B0%D1%87"
  137. "#%D1%84%D1%80%D0%B0%D0%B3"
  138. )
  139. assert str(u) == expected
  140. def test_build_already_encoded():
  141. # resulting URL is invalid but not encoded
  142. u = URL.build(
  143. scheme="http",
  144. host="оун-упа.укр",
  145. path="/шлях/криївка",
  146. query_string="ключ=знач",
  147. fragment="фраг",
  148. encoded=True,
  149. )
  150. assert str(u) == "http://оун-упа.укр/шлях/криївка?ключ=знач#фраг"
  151. def test_build_percent_encoded():
  152. u = URL.build(
  153. scheme="http",
  154. host="%2d.org",
  155. user="u%2d",
  156. password="p%2d",
  157. path="/%2d",
  158. query_string="k%2d=v%2d",
  159. fragment="f%2d",
  160. )
  161. assert str(u) == "http://u%252d:p%252d@%2d.org/%252d?k%252d=v%252d#f%252d"
  162. assert u.raw_host == "%2d.org"
  163. assert u.host == "%2d.org"
  164. assert u.raw_user == "u%252d"
  165. assert u.user == "u%2d"
  166. assert u.raw_password == "p%252d"
  167. assert u.password == "p%2d"
  168. assert u.raw_authority == "u%252d:p%252d@%2d.org"
  169. assert u.authority == "u%2d:p%2d@%2d.org:80"
  170. assert u.raw_path == "/%252d"
  171. assert u.path == "/%2d"
  172. assert u.query == {"k%2d": "v%2d"}
  173. assert u.raw_query_string == "k%252d=v%252d"
  174. assert u.query_string == "k%2d=v%2d"
  175. assert u.raw_fragment == "f%252d"
  176. assert u.fragment == "f%2d"
  177. def test_build_with_authority_percent_encoded():
  178. u = URL.build(scheme="http", authority="u%2d:p%2d@%2d.org")
  179. assert str(u) == "http://u%252d:p%252d@%2d.org"
  180. assert u.raw_host == "%2d.org"
  181. assert u.host == "%2d.org"
  182. assert u.raw_user == "u%252d"
  183. assert u.user == "u%2d"
  184. assert u.raw_password == "p%252d"
  185. assert u.password == "p%2d"
  186. assert u.raw_authority == "u%252d:p%252d@%2d.org"
  187. assert u.authority == "u%2d:p%2d@%2d.org:80"
  188. def test_build_with_authority_percent_encoded_already_encoded():
  189. u = URL.build(scheme="http", authority="u%2d:p%2d@%2d.org", encoded=True)
  190. assert str(u) == "http://u%2d:p%2d@%2d.org"
  191. assert u.raw_host == "%2d.org"
  192. assert u.host == "%2d.org"
  193. assert u.user == "u-"
  194. assert u.raw_user == "u%2d"
  195. assert u.password == "p-"
  196. assert u.raw_password == "p%2d"
  197. assert u.authority == "u-:p-@%2d.org:80"
  198. assert u.raw_authority == "u%2d:p%2d@%2d.org"
  199. def test_build_with_authority_with_path_with_leading_slash():
  200. u = URL.build(scheme="http", host="example.com", path="/path_with_leading_slash")
  201. assert str(u) == "http://example.com/path_with_leading_slash"
  202. def test_build_with_authority_with_empty_path():
  203. u = URL.build(scheme="http", host="example.com", path="")
  204. assert str(u) == "http://example.com"
  205. def test_build_with_authority_with_path_without_leading_slash():
  206. with pytest.raises(ValueError):
  207. URL.build(scheme="http", host="example.com", path="path_without_leading_slash")
  208. def test_build_with_none_host():
  209. with pytest.raises(TypeError, match="NoneType is illegal for.*host"):
  210. URL.build(scheme="http", host=None)
  211. def test_build_with_none_path():
  212. with pytest.raises(TypeError):
  213. URL.build(scheme="http", host="example.com", path=None)
  214. def test_build_with_none_query_string():
  215. with pytest.raises(TypeError):
  216. URL.build(scheme="http", host="example.com", query_string=None)
  217. def test_build_with_none_fragment():
  218. with pytest.raises(TypeError):
  219. URL.build(scheme="http", host="example.com", fragment=None)