Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_url.py 14KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # -*- coding: utf-8 -*-
  2. #
  3. import os
  4. import unittest
  5. from websocket._url import get_proxy_info, parse_url, _is_address_in_network, _is_no_proxy_host
  6. """
  7. test_url.py
  8. websocket - WebSocket client library for Python
  9. Copyright 2023 engn33r
  10. Licensed under the Apache License, Version 2.0 (the "License");
  11. you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing, software
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. """
  20. class UrlTest(unittest.TestCase):
  21. def test_address_in_network(self):
  22. self.assertTrue(_is_address_in_network('127.0.0.1', '127.0.0.0/8'))
  23. self.assertTrue(_is_address_in_network('127.1.0.1', '127.0.0.0/8'))
  24. self.assertFalse(_is_address_in_network('127.1.0.1', '127.0.0.0/24'))
  25. def testParseUrl(self):
  26. p = parse_url("ws://www.example.com/r")
  27. self.assertEqual(p[0], "www.example.com")
  28. self.assertEqual(p[1], 80)
  29. self.assertEqual(p[2], "/r")
  30. self.assertEqual(p[3], False)
  31. p = parse_url("ws://www.example.com/r/")
  32. self.assertEqual(p[0], "www.example.com")
  33. self.assertEqual(p[1], 80)
  34. self.assertEqual(p[2], "/r/")
  35. self.assertEqual(p[3], False)
  36. p = parse_url("ws://www.example.com/")
  37. self.assertEqual(p[0], "www.example.com")
  38. self.assertEqual(p[1], 80)
  39. self.assertEqual(p[2], "/")
  40. self.assertEqual(p[3], False)
  41. p = parse_url("ws://www.example.com")
  42. self.assertEqual(p[0], "www.example.com")
  43. self.assertEqual(p[1], 80)
  44. self.assertEqual(p[2], "/")
  45. self.assertEqual(p[3], False)
  46. p = parse_url("ws://www.example.com:8080/r")
  47. self.assertEqual(p[0], "www.example.com")
  48. self.assertEqual(p[1], 8080)
  49. self.assertEqual(p[2], "/r")
  50. self.assertEqual(p[3], False)
  51. p = parse_url("ws://www.example.com:8080/")
  52. self.assertEqual(p[0], "www.example.com")
  53. self.assertEqual(p[1], 8080)
  54. self.assertEqual(p[2], "/")
  55. self.assertEqual(p[3], False)
  56. p = parse_url("ws://www.example.com:8080")
  57. self.assertEqual(p[0], "www.example.com")
  58. self.assertEqual(p[1], 8080)
  59. self.assertEqual(p[2], "/")
  60. self.assertEqual(p[3], False)
  61. p = parse_url("wss://www.example.com:8080/r")
  62. self.assertEqual(p[0], "www.example.com")
  63. self.assertEqual(p[1], 8080)
  64. self.assertEqual(p[2], "/r")
  65. self.assertEqual(p[3], True)
  66. p = parse_url("wss://www.example.com:8080/r?key=value")
  67. self.assertEqual(p[0], "www.example.com")
  68. self.assertEqual(p[1], 8080)
  69. self.assertEqual(p[2], "/r?key=value")
  70. self.assertEqual(p[3], True)
  71. self.assertRaises(ValueError, parse_url, "http://www.example.com/r")
  72. p = parse_url("ws://[2a03:4000:123:83::3]/r")
  73. self.assertEqual(p[0], "2a03:4000:123:83::3")
  74. self.assertEqual(p[1], 80)
  75. self.assertEqual(p[2], "/r")
  76. self.assertEqual(p[3], False)
  77. p = parse_url("ws://[2a03:4000:123:83::3]:8080/r")
  78. self.assertEqual(p[0], "2a03:4000:123:83::3")
  79. self.assertEqual(p[1], 8080)
  80. self.assertEqual(p[2], "/r")
  81. self.assertEqual(p[3], False)
  82. p = parse_url("wss://[2a03:4000:123:83::3]/r")
  83. self.assertEqual(p[0], "2a03:4000:123:83::3")
  84. self.assertEqual(p[1], 443)
  85. self.assertEqual(p[2], "/r")
  86. self.assertEqual(p[3], True)
  87. p = parse_url("wss://[2a03:4000:123:83::3]:8080/r")
  88. self.assertEqual(p[0], "2a03:4000:123:83::3")
  89. self.assertEqual(p[1], 8080)
  90. self.assertEqual(p[2], "/r")
  91. self.assertEqual(p[3], True)
  92. class IsNoProxyHostTest(unittest.TestCase):
  93. def setUp(self):
  94. self.no_proxy = os.environ.get("no_proxy", None)
  95. if "no_proxy" in os.environ:
  96. del os.environ["no_proxy"]
  97. def tearDown(self):
  98. if self.no_proxy:
  99. os.environ["no_proxy"] = self.no_proxy
  100. elif "no_proxy" in os.environ:
  101. del os.environ["no_proxy"]
  102. def testMatchAll(self):
  103. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['*']))
  104. self.assertTrue(_is_no_proxy_host("192.168.0.1", ['*']))
  105. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['other.websocket.org', '*']))
  106. os.environ['no_proxy'] = '*'
  107. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  108. self.assertTrue(_is_no_proxy_host("192.168.0.1", None))
  109. os.environ['no_proxy'] = 'other.websocket.org, *'
  110. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  111. def testIpAddress(self):
  112. self.assertTrue(_is_no_proxy_host("127.0.0.1", ['127.0.0.1']))
  113. self.assertFalse(_is_no_proxy_host("127.0.0.2", ['127.0.0.1']))
  114. self.assertTrue(_is_no_proxy_host("127.0.0.1", ['other.websocket.org', '127.0.0.1']))
  115. self.assertFalse(_is_no_proxy_host("127.0.0.2", ['other.websocket.org', '127.0.0.1']))
  116. os.environ['no_proxy'] = '127.0.0.1'
  117. self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
  118. self.assertFalse(_is_no_proxy_host("127.0.0.2", None))
  119. os.environ['no_proxy'] = 'other.websocket.org, 127.0.0.1'
  120. self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
  121. self.assertFalse(_is_no_proxy_host("127.0.0.2", None))
  122. def testIpAddressInRange(self):
  123. self.assertTrue(_is_no_proxy_host("127.0.0.1", ['127.0.0.0/8']))
  124. self.assertTrue(_is_no_proxy_host("127.0.0.2", ['127.0.0.0/8']))
  125. self.assertFalse(_is_no_proxy_host("127.1.0.1", ['127.0.0.0/24']))
  126. os.environ['no_proxy'] = '127.0.0.0/8'
  127. self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
  128. self.assertTrue(_is_no_proxy_host("127.0.0.2", None))
  129. os.environ['no_proxy'] = '127.0.0.0/24'
  130. self.assertFalse(_is_no_proxy_host("127.1.0.1", None))
  131. def testHostnameMatch(self):
  132. self.assertTrue(_is_no_proxy_host("my.websocket.org", ['my.websocket.org']))
  133. self.assertTrue(_is_no_proxy_host("my.websocket.org", ['other.websocket.org', 'my.websocket.org']))
  134. self.assertFalse(_is_no_proxy_host("my.websocket.org", ['other.websocket.org']))
  135. os.environ['no_proxy'] = 'my.websocket.org'
  136. self.assertTrue(_is_no_proxy_host("my.websocket.org", None))
  137. self.assertFalse(_is_no_proxy_host("other.websocket.org", None))
  138. os.environ['no_proxy'] = 'other.websocket.org, my.websocket.org'
  139. self.assertTrue(_is_no_proxy_host("my.websocket.org", None))
  140. def testHostnameMatchDomain(self):
  141. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['.websocket.org']))
  142. self.assertTrue(_is_no_proxy_host("my.other.websocket.org", ['.websocket.org']))
  143. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['my.websocket.org', '.websocket.org']))
  144. self.assertFalse(_is_no_proxy_host("any.websocket.com", ['.websocket.org']))
  145. os.environ['no_proxy'] = '.websocket.org'
  146. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  147. self.assertTrue(_is_no_proxy_host("my.other.websocket.org", None))
  148. self.assertFalse(_is_no_proxy_host("any.websocket.com", None))
  149. os.environ['no_proxy'] = 'my.websocket.org, .websocket.org'
  150. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  151. class ProxyInfoTest(unittest.TestCase):
  152. def setUp(self):
  153. self.http_proxy = os.environ.get("http_proxy", None)
  154. self.https_proxy = os.environ.get("https_proxy", None)
  155. self.no_proxy = os.environ.get("no_proxy", None)
  156. if "http_proxy" in os.environ:
  157. del os.environ["http_proxy"]
  158. if "https_proxy" in os.environ:
  159. del os.environ["https_proxy"]
  160. if "no_proxy" in os.environ:
  161. del os.environ["no_proxy"]
  162. def tearDown(self):
  163. if self.http_proxy:
  164. os.environ["http_proxy"] = self.http_proxy
  165. elif "http_proxy" in os.environ:
  166. del os.environ["http_proxy"]
  167. if self.https_proxy:
  168. os.environ["https_proxy"] = self.https_proxy
  169. elif "https_proxy" in os.environ:
  170. del os.environ["https_proxy"]
  171. if self.no_proxy:
  172. os.environ["no_proxy"] = self.no_proxy
  173. elif "no_proxy" in os.environ:
  174. del os.environ["no_proxy"]
  175. def testProxyFromArgs(self):
  176. self.assertEqual(get_proxy_info("echo.websocket.events", False, proxy_host="localhost"), ("localhost", 0, None))
  177. self.assertEqual(get_proxy_info("echo.websocket.events", False, proxy_host="localhost", proxy_port=3128),
  178. ("localhost", 3128, None))
  179. self.assertEqual(get_proxy_info("echo.websocket.events", True, proxy_host="localhost"), ("localhost", 0, None))
  180. self.assertEqual(get_proxy_info("echo.websocket.events", True, proxy_host="localhost", proxy_port=3128),
  181. ("localhost", 3128, None))
  182. self.assertEqual(get_proxy_info("echo.websocket.events", False, proxy_host="localhost", proxy_auth=("a", "b")),
  183. ("localhost", 0, ("a", "b")))
  184. self.assertEqual(
  185. get_proxy_info("echo.websocket.events", False, proxy_host="localhost", proxy_port=3128, proxy_auth=("a", "b")),
  186. ("localhost", 3128, ("a", "b")))
  187. self.assertEqual(get_proxy_info("echo.websocket.events", True, proxy_host="localhost", proxy_auth=("a", "b")),
  188. ("localhost", 0, ("a", "b")))
  189. self.assertEqual(
  190. get_proxy_info("echo.websocket.events", True, proxy_host="localhost", proxy_port=3128, proxy_auth=("a", "b")),
  191. ("localhost", 3128, ("a", "b")))
  192. self.assertEqual(get_proxy_info("echo.websocket.events", True, proxy_host="localhost", proxy_port=3128,
  193. no_proxy=["example.com"], proxy_auth=("a", "b")),
  194. ("localhost", 3128, ("a", "b")))
  195. self.assertEqual(get_proxy_info("echo.websocket.events", True, proxy_host="localhost", proxy_port=3128,
  196. no_proxy=["echo.websocket.events"], proxy_auth=("a", "b")),
  197. (None, 0, None))
  198. def testProxyFromEnv(self):
  199. os.environ["http_proxy"] = "http://localhost/"
  200. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", None, None))
  201. os.environ["http_proxy"] = "http://localhost:3128/"
  202. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", 3128, None))
  203. os.environ["http_proxy"] = "http://localhost/"
  204. os.environ["https_proxy"] = "http://localhost2/"
  205. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", None, None))
  206. os.environ["http_proxy"] = "http://localhost:3128/"
  207. os.environ["https_proxy"] = "http://localhost2:3128/"
  208. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", 3128, None))
  209. os.environ["http_proxy"] = "http://localhost/"
  210. os.environ["https_proxy"] = "http://localhost2/"
  211. self.assertEqual(get_proxy_info("echo.websocket.events", True), ("localhost2", None, None))
  212. os.environ["http_proxy"] = "http://localhost:3128/"
  213. os.environ["https_proxy"] = "http://localhost2:3128/"
  214. self.assertEqual(get_proxy_info("echo.websocket.events", True), ("localhost2", 3128, None))
  215. os.environ["http_proxy"] = "http://a:b@localhost/"
  216. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", None, ("a", "b")))
  217. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  218. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", 3128, ("a", "b")))
  219. os.environ["http_proxy"] = "http://a:b@localhost/"
  220. os.environ["https_proxy"] = "http://a:b@localhost2/"
  221. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", None, ("a", "b")))
  222. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  223. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  224. self.assertEqual(get_proxy_info("echo.websocket.events", False), ("localhost", 3128, ("a", "b")))
  225. os.environ["http_proxy"] = "http://a:b@localhost/"
  226. os.environ["https_proxy"] = "http://a:b@localhost2/"
  227. self.assertEqual(get_proxy_info("echo.websocket.events", True), ("localhost2", None, ("a", "b")))
  228. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  229. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  230. self.assertEqual(get_proxy_info("echo.websocket.events", True), ("localhost2", 3128, ("a", "b")))
  231. os.environ["http_proxy"] = "http://john%40example.com:P%40SSWORD@localhost:3128/"
  232. os.environ["https_proxy"] = "http://john%40example.com:P%40SSWORD@localhost2:3128/"
  233. self.assertEqual(get_proxy_info("echo.websocket.events", True), ("localhost2", 3128, ("john@example.com", "P@SSWORD")))
  234. os.environ["http_proxy"] = "http://a:b@localhost/"
  235. os.environ["https_proxy"] = "http://a:b@localhost2/"
  236. os.environ["no_proxy"] = "example1.com,example2.com"
  237. self.assertEqual(get_proxy_info("example.1.com", True), ("localhost2", None, ("a", "b")))
  238. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  239. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  240. os.environ["no_proxy"] = "example1.com,example2.com, echo.websocket.events"
  241. self.assertEqual(get_proxy_info("echo.websocket.events", True), (None, 0, None))
  242. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  243. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  244. os.environ["no_proxy"] = "example1.com,example2.com, .websocket.events"
  245. self.assertEqual(get_proxy_info("echo.websocket.events", True), (None, 0, None))
  246. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  247. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  248. os.environ["no_proxy"] = "127.0.0.0/8, 192.168.0.0/16"
  249. self.assertEqual(get_proxy_info("127.0.0.1", False), (None, 0, None))
  250. self.assertEqual(get_proxy_info("192.168.1.1", False), (None, 0, None))
  251. if __name__ == "__main__":
  252. unittest.main()