test_url_update_netloc.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import pytest
  2. from yarl import URL
  3. # with_*
  4. def test_with_scheme():
  5. url = URL("http://example.com")
  6. assert str(url.with_scheme("https")) == "https://example.com"
  7. def test_with_scheme_uppercased():
  8. url = URL("http://example.com")
  9. assert str(url.with_scheme("HTTPS")) == "https://example.com"
  10. def test_with_scheme_for_relative_url():
  11. with pytest.raises(ValueError):
  12. URL("path/to").with_scheme("http")
  13. def test_with_scheme_invalid_type():
  14. url = URL("http://example.com")
  15. with pytest.raises(TypeError):
  16. assert str(url.with_scheme(123))
  17. def test_with_user():
  18. url = URL("http://example.com")
  19. assert str(url.with_user("john")) == "http://john@example.com"
  20. def test_with_user_non_ascii():
  21. url = URL("http://example.com")
  22. url2 = url.with_user("бажан")
  23. assert url2.raw_user == "%D0%B1%D0%B0%D0%B6%D0%B0%D0%BD"
  24. assert url2.user == "бажан"
  25. assert url2.raw_authority == "%D0%B1%D0%B0%D0%B6%D0%B0%D0%BD@example.com"
  26. assert url2.authority == "бажан@example.com:80"
  27. def test_with_user_percent_encoded():
  28. url = URL("http://example.com")
  29. url2 = url.with_user("%cf%80")
  30. assert url2.raw_user == "%25cf%2580"
  31. assert url2.user == "%cf%80"
  32. assert url2.raw_authority == "%25cf%2580@example.com"
  33. assert url2.authority == "%cf%80@example.com:80"
  34. def test_with_user_for_relative_url():
  35. with pytest.raises(ValueError):
  36. URL("path/to").with_user("user")
  37. def test_with_user_invalid_type():
  38. url = URL("http://example.com:123")
  39. with pytest.raises(TypeError):
  40. url.with_user(123)
  41. def test_with_user_None():
  42. url = URL("http://john@example.com")
  43. assert str(url.with_user(None)) == "http://example.com"
  44. def test_with_user_ipv6():
  45. url = URL("http://john:pass@[::1]:8080/")
  46. assert str(url.with_user(None)) == "http://[::1]:8080/"
  47. def test_with_user_None_when_password_present():
  48. url = URL("http://john:pass@example.com")
  49. assert str(url.with_user(None)) == "http://example.com"
  50. def test_with_password():
  51. url = URL("http://john@example.com")
  52. assert str(url.with_password("pass")) == "http://john:pass@example.com"
  53. def test_with_password_ipv6():
  54. url = URL("http://john:pass@[::1]:8080/")
  55. assert str(url.with_password(None)) == "http://john@[::1]:8080/"
  56. def test_with_password_non_ascii():
  57. url = URL("http://john@example.com")
  58. url2 = url.with_password("пароль")
  59. assert url2.raw_password == "%D0%BF%D0%B0%D1%80%D0%BE%D0%BB%D1%8C"
  60. assert url2.password == "пароль"
  61. assert url2.raw_authority == "john:%D0%BF%D0%B0%D1%80%D0%BE%D0%BB%D1%8C@example.com"
  62. assert url2.authority == "john:пароль@example.com:80"
  63. def test_with_password_percent_encoded():
  64. url = URL("http://john@example.com")
  65. url2 = url.with_password("%cf%80")
  66. assert url2.raw_password == "%25cf%2580"
  67. assert url2.password == "%cf%80"
  68. assert url2.raw_authority == "john:%25cf%2580@example.com"
  69. assert url2.authority == "john:%cf%80@example.com:80"
  70. def test_with_password_non_ascii_with_colon():
  71. url = URL("http://john@example.com")
  72. url2 = url.with_password("п:а")
  73. assert url2.raw_password == "%D0%BF%3A%D0%B0"
  74. assert url2.password == "п:а"
  75. def test_with_password_for_relative_url():
  76. with pytest.raises(ValueError):
  77. URL("path/to").with_password("pass")
  78. def test_with_password_None():
  79. url = URL("http://john:pass@example.com")
  80. assert str(url.with_password(None)) == "http://john@example.com"
  81. def test_with_password_invalid_type():
  82. url = URL("http://example.com:123")
  83. with pytest.raises(TypeError):
  84. url.with_password(123)
  85. def test_with_password_and_empty_user():
  86. url = URL("http://example.com")
  87. url2 = url.with_password("pass")
  88. assert url2.password == "pass"
  89. assert url2.user is None
  90. assert str(url2) == "http://:pass@example.com"
  91. def test_from_str_with_host_ipv4():
  92. url = URL("http://host:80")
  93. url = url.with_host("192.168.1.1")
  94. assert url.raw_host == "192.168.1.1"
  95. def test_from_str_with_host_ipv6():
  96. url = URL("http://host:80")
  97. url = url.with_host("::1")
  98. assert url.raw_host == "::1"
  99. def test_with_host():
  100. url = URL("http://example.com:123")
  101. assert str(url.with_host("example.org")) == "http://example.org:123"
  102. def test_with_host_empty():
  103. url = URL("http://example.com:123")
  104. with pytest.raises(ValueError):
  105. url.with_host("")
  106. def test_with_host_non_ascii():
  107. url = URL("http://example.com:123")
  108. url2 = url.with_host("оун-упа.укр")
  109. assert url2.raw_host == "xn----8sb1bdhvc.xn--j1amh"
  110. assert url2.host == "оун-упа.укр"
  111. assert url2.raw_authority == "xn----8sb1bdhvc.xn--j1amh:123"
  112. assert url2.authority == "оун-упа.укр:123"
  113. def test_with_host_percent_encoded():
  114. url = URL("http://%25cf%2580%cf%80:%25cf%2580%cf%80@example.com:123")
  115. url2 = url.with_host("%cf%80.org")
  116. assert url2.raw_host == "%cf%80.org"
  117. assert url2.host == "%cf%80.org"
  118. assert url2.raw_authority == "%25cf%2580%CF%80:%25cf%2580%CF%80@%cf%80.org:123"
  119. assert url2.authority == "%cf%80π:%cf%80π@%cf%80.org:123"
  120. def test_with_host_for_relative_url():
  121. with pytest.raises(ValueError):
  122. URL("path/to").with_host("example.com")
  123. def test_with_host_invalid_type():
  124. url = URL("http://example.com:123")
  125. with pytest.raises(TypeError):
  126. url.with_host(None)
  127. def test_with_port():
  128. url = URL("http://example.com")
  129. assert str(url.with_port(8888)) == "http://example.com:8888"
  130. def test_with_port_with_no_port():
  131. url = URL("http://example.com")
  132. assert str(url.with_port(None)) == "http://example.com"
  133. def test_with_port_ipv6():
  134. url = URL("http://[::1]:8080/")
  135. assert str(url.with_port(80)) == "http://[::1]:80/"
  136. def test_with_port_keeps_query_and_fragment():
  137. url = URL("http://example.com/?a=1#frag")
  138. assert str(url.with_port(8888)) == "http://example.com:8888/?a=1#frag"
  139. def test_with_port_percent_encoded():
  140. url = URL("http://user%name:pass%word@example.com/")
  141. assert str(url.with_port(808)) == "http://user%25name:pass%25word@example.com:808/"
  142. def test_with_port_for_relative_url():
  143. with pytest.raises(ValueError):
  144. URL("path/to").with_port(1234)
  145. def test_with_port_invalid_type():
  146. with pytest.raises(TypeError):
  147. URL("http://example.com").with_port("123")
  148. with pytest.raises(TypeError):
  149. URL("http://example.com").with_port(True)
  150. def test_with_port_invalid_range():
  151. with pytest.raises(ValueError):
  152. URL("http://example.com").with_port(-1)