test_url.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # -*- coding: utf-8 -*-
  2. #
  3. """
  4. websocket - WebSocket client library for Python
  5. Copyright (C) 2010 Hiroki Ohtani(liris)
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. import sys
  19. import os
  20. from websocket._url import get_proxy_info, parse_url, _is_address_in_network, _is_no_proxy_host
  21. if sys.version_info[0] == 2 and sys.version_info[1] < 7:
  22. import unittest2 as unittest
  23. else:
  24. import unittest
  25. sys.path[0:0] = [""]
  26. class UrlTest(unittest.TestCase):
  27. def test_address_in_network(self):
  28. self.assertTrue(_is_address_in_network('127.0.0.1', '127.0.0.0/8'))
  29. self.assertTrue(_is_address_in_network('127.1.0.1', '127.0.0.0/8'))
  30. self.assertFalse(_is_address_in_network('127.1.0.1', '127.0.0.0/24'))
  31. def testParseUrl(self):
  32. p = parse_url("ws://www.example.com/r")
  33. self.assertEqual(p[0], "www.example.com")
  34. self.assertEqual(p[1], 80)
  35. self.assertEqual(p[2], "/r")
  36. self.assertEqual(p[3], False)
  37. p = parse_url("ws://www.example.com/r/")
  38. self.assertEqual(p[0], "www.example.com")
  39. self.assertEqual(p[1], 80)
  40. self.assertEqual(p[2], "/r/")
  41. self.assertEqual(p[3], False)
  42. p = parse_url("ws://www.example.com/")
  43. self.assertEqual(p[0], "www.example.com")
  44. self.assertEqual(p[1], 80)
  45. self.assertEqual(p[2], "/")
  46. self.assertEqual(p[3], False)
  47. p = parse_url("ws://www.example.com")
  48. self.assertEqual(p[0], "www.example.com")
  49. self.assertEqual(p[1], 80)
  50. self.assertEqual(p[2], "/")
  51. self.assertEqual(p[3], False)
  52. p = parse_url("ws://www.example.com:8080/r")
  53. self.assertEqual(p[0], "www.example.com")
  54. self.assertEqual(p[1], 8080)
  55. self.assertEqual(p[2], "/r")
  56. self.assertEqual(p[3], False)
  57. p = parse_url("ws://www.example.com:8080/")
  58. self.assertEqual(p[0], "www.example.com")
  59. self.assertEqual(p[1], 8080)
  60. self.assertEqual(p[2], "/")
  61. self.assertEqual(p[3], False)
  62. p = parse_url("ws://www.example.com:8080")
  63. self.assertEqual(p[0], "www.example.com")
  64. self.assertEqual(p[1], 8080)
  65. self.assertEqual(p[2], "/")
  66. self.assertEqual(p[3], False)
  67. p = parse_url("wss://www.example.com:8080/r")
  68. self.assertEqual(p[0], "www.example.com")
  69. self.assertEqual(p[1], 8080)
  70. self.assertEqual(p[2], "/r")
  71. self.assertEqual(p[3], True)
  72. p = parse_url("wss://www.example.com:8080/r?key=value")
  73. self.assertEqual(p[0], "www.example.com")
  74. self.assertEqual(p[1], 8080)
  75. self.assertEqual(p[2], "/r?key=value")
  76. self.assertEqual(p[3], True)
  77. self.assertRaises(ValueError, parse_url, "http://www.example.com/r")
  78. if sys.version_info[0] == 2 and sys.version_info[1] < 7:
  79. return
  80. p = parse_url("ws://[2a03:4000:123:83::3]/r")
  81. self.assertEqual(p[0], "2a03:4000:123:83::3")
  82. self.assertEqual(p[1], 80)
  83. self.assertEqual(p[2], "/r")
  84. self.assertEqual(p[3], False)
  85. p = parse_url("ws://[2a03:4000:123:83::3]:8080/r")
  86. self.assertEqual(p[0], "2a03:4000:123:83::3")
  87. self.assertEqual(p[1], 8080)
  88. self.assertEqual(p[2], "/r")
  89. self.assertEqual(p[3], False)
  90. p = parse_url("wss://[2a03:4000:123:83::3]/r")
  91. self.assertEqual(p[0], "2a03:4000:123:83::3")
  92. self.assertEqual(p[1], 443)
  93. self.assertEqual(p[2], "/r")
  94. self.assertEqual(p[3], True)
  95. p = parse_url("wss://[2a03:4000:123:83::3]:8080/r")
  96. self.assertEqual(p[0], "2a03:4000:123:83::3")
  97. self.assertEqual(p[1], 8080)
  98. self.assertEqual(p[2], "/r")
  99. self.assertEqual(p[3], True)
  100. class IsNoProxyHostTest(unittest.TestCase):
  101. def setUp(self):
  102. self.no_proxy = os.environ.get("no_proxy", None)
  103. if "no_proxy" in os.environ:
  104. del os.environ["no_proxy"]
  105. def tearDown(self):
  106. if self.no_proxy:
  107. os.environ["no_proxy"] = self.no_proxy
  108. elif "no_proxy" in os.environ:
  109. del os.environ["no_proxy"]
  110. def testMatchAll(self):
  111. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['*']))
  112. self.assertTrue(_is_no_proxy_host("192.168.0.1", ['*']))
  113. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['other.websocket.org', '*']))
  114. os.environ['no_proxy'] = '*'
  115. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  116. self.assertTrue(_is_no_proxy_host("192.168.0.1", None))
  117. os.environ['no_proxy'] = 'other.websocket.org, *'
  118. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  119. def testIpAddress(self):
  120. self.assertTrue(_is_no_proxy_host("127.0.0.1", ['127.0.0.1']))
  121. self.assertFalse(_is_no_proxy_host("127.0.0.2", ['127.0.0.1']))
  122. self.assertTrue(_is_no_proxy_host("127.0.0.1", ['other.websocket.org', '127.0.0.1']))
  123. self.assertFalse(_is_no_proxy_host("127.0.0.2", ['other.websocket.org', '127.0.0.1']))
  124. os.environ['no_proxy'] = '127.0.0.1'
  125. self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
  126. self.assertFalse(_is_no_proxy_host("127.0.0.2", None))
  127. os.environ['no_proxy'] = 'other.websocket.org, 127.0.0.1'
  128. self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
  129. self.assertFalse(_is_no_proxy_host("127.0.0.2", None))
  130. def testIpAddressInRange(self):
  131. self.assertTrue(_is_no_proxy_host("127.0.0.1", ['127.0.0.0/8']))
  132. self.assertTrue(_is_no_proxy_host("127.0.0.2", ['127.0.0.0/8']))
  133. self.assertFalse(_is_no_proxy_host("127.1.0.1", ['127.0.0.0/24']))
  134. os.environ['no_proxy'] = '127.0.0.0/8'
  135. self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
  136. self.assertTrue(_is_no_proxy_host("127.0.0.2", None))
  137. os.environ['no_proxy'] = '127.0.0.0/24'
  138. self.assertFalse(_is_no_proxy_host("127.1.0.1", None))
  139. def testHostnameMatch(self):
  140. self.assertTrue(_is_no_proxy_host("my.websocket.org", ['my.websocket.org']))
  141. self.assertTrue(_is_no_proxy_host("my.websocket.org", ['other.websocket.org', 'my.websocket.org']))
  142. self.assertFalse(_is_no_proxy_host("my.websocket.org", ['other.websocket.org']))
  143. os.environ['no_proxy'] = 'my.websocket.org'
  144. self.assertTrue(_is_no_proxy_host("my.websocket.org", None))
  145. self.assertFalse(_is_no_proxy_host("other.websocket.org", None))
  146. os.environ['no_proxy'] = 'other.websocket.org, my.websocket.org'
  147. self.assertTrue(_is_no_proxy_host("my.websocket.org", None))
  148. def testHostnameMatchDomain(self):
  149. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['.websocket.org']))
  150. self.assertTrue(_is_no_proxy_host("my.other.websocket.org", ['.websocket.org']))
  151. self.assertTrue(_is_no_proxy_host("any.websocket.org", ['my.websocket.org', '.websocket.org']))
  152. self.assertFalse(_is_no_proxy_host("any.websocket.com", ['.websocket.org']))
  153. os.environ['no_proxy'] = '.websocket.org'
  154. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  155. self.assertTrue(_is_no_proxy_host("my.other.websocket.org", None))
  156. self.assertFalse(_is_no_proxy_host("any.websocket.com", None))
  157. os.environ['no_proxy'] = 'my.websocket.org, .websocket.org'
  158. self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
  159. class ProxyInfoTest(unittest.TestCase):
  160. def setUp(self):
  161. self.http_proxy = os.environ.get("http_proxy", None)
  162. self.https_proxy = os.environ.get("https_proxy", None)
  163. self.no_proxy = os.environ.get("no_proxy", None)
  164. if "http_proxy" in os.environ:
  165. del os.environ["http_proxy"]
  166. if "https_proxy" in os.environ:
  167. del os.environ["https_proxy"]
  168. if "no_proxy" in os.environ:
  169. del os.environ["no_proxy"]
  170. def tearDown(self):
  171. if self.http_proxy:
  172. os.environ["http_proxy"] = self.http_proxy
  173. elif "http_proxy" in os.environ:
  174. del os.environ["http_proxy"]
  175. if self.https_proxy:
  176. os.environ["https_proxy"] = self.https_proxy
  177. elif "https_proxy" in os.environ:
  178. del os.environ["https_proxy"]
  179. if self.no_proxy:
  180. os.environ["no_proxy"] = self.no_proxy
  181. elif "no_proxy" in os.environ:
  182. del os.environ["no_proxy"]
  183. def testProxyFromArgs(self):
  184. self.assertEqual(get_proxy_info("echo.websocket.org", False, proxy_host="localhost"), ("localhost", 0, None))
  185. self.assertEqual(get_proxy_info("echo.websocket.org", False, proxy_host="localhost", proxy_port=3128),
  186. ("localhost", 3128, None))
  187. self.assertEqual(get_proxy_info("echo.websocket.org", True, proxy_host="localhost"), ("localhost", 0, None))
  188. self.assertEqual(get_proxy_info("echo.websocket.org", True, proxy_host="localhost", proxy_port=3128),
  189. ("localhost", 3128, None))
  190. self.assertEqual(get_proxy_info("echo.websocket.org", False, proxy_host="localhost", proxy_auth=("a", "b")),
  191. ("localhost", 0, ("a", "b")))
  192. self.assertEqual(
  193. get_proxy_info("echo.websocket.org", False, proxy_host="localhost", proxy_port=3128, proxy_auth=("a", "b")),
  194. ("localhost", 3128, ("a", "b")))
  195. self.assertEqual(get_proxy_info("echo.websocket.org", True, proxy_host="localhost", proxy_auth=("a", "b")),
  196. ("localhost", 0, ("a", "b")))
  197. self.assertEqual(
  198. get_proxy_info("echo.websocket.org", True, proxy_host="localhost", proxy_port=3128, proxy_auth=("a", "b")),
  199. ("localhost", 3128, ("a", "b")))
  200. self.assertEqual(get_proxy_info("echo.websocket.org", True, proxy_host="localhost", proxy_port=3128,
  201. no_proxy=["example.com"], proxy_auth=("a", "b")),
  202. ("localhost", 3128, ("a", "b")))
  203. self.assertEqual(get_proxy_info("echo.websocket.org", True, proxy_host="localhost", proxy_port=3128,
  204. no_proxy=["echo.websocket.org"], proxy_auth=("a", "b")),
  205. (None, 0, None))
  206. def testProxyFromEnv(self):
  207. os.environ["http_proxy"] = "http://localhost/"
  208. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", None, None))
  209. os.environ["http_proxy"] = "http://localhost:3128/"
  210. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", 3128, None))
  211. os.environ["http_proxy"] = "http://localhost/"
  212. os.environ["https_proxy"] = "http://localhost2/"
  213. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", None, None))
  214. os.environ["http_proxy"] = "http://localhost:3128/"
  215. os.environ["https_proxy"] = "http://localhost2:3128/"
  216. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", 3128, None))
  217. os.environ["http_proxy"] = "http://localhost/"
  218. os.environ["https_proxy"] = "http://localhost2/"
  219. self.assertEqual(get_proxy_info("echo.websocket.org", True), ("localhost2", None, None))
  220. os.environ["http_proxy"] = "http://localhost:3128/"
  221. os.environ["https_proxy"] = "http://localhost2:3128/"
  222. self.assertEqual(get_proxy_info("echo.websocket.org", True), ("localhost2", 3128, None))
  223. os.environ["http_proxy"] = "http://a:b@localhost/"
  224. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", None, ("a", "b")))
  225. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  226. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", 3128, ("a", "b")))
  227. os.environ["http_proxy"] = "http://a:b@localhost/"
  228. os.environ["https_proxy"] = "http://a:b@localhost2/"
  229. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", None, ("a", "b")))
  230. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  231. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  232. self.assertEqual(get_proxy_info("echo.websocket.org", False), ("localhost", 3128, ("a", "b")))
  233. os.environ["http_proxy"] = "http://a:b@localhost/"
  234. os.environ["https_proxy"] = "http://a:b@localhost2/"
  235. self.assertEqual(get_proxy_info("echo.websocket.org", True), ("localhost2", None, ("a", "b")))
  236. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  237. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  238. self.assertEqual(get_proxy_info("echo.websocket.org", True), ("localhost2", 3128, ("a", "b")))
  239. os.environ["http_proxy"] = "http://a:b@localhost/"
  240. os.environ["https_proxy"] = "http://a:b@localhost2/"
  241. os.environ["no_proxy"] = "example1.com,example2.com"
  242. self.assertEqual(get_proxy_info("example.1.com", True), ("localhost2", None, ("a", "b")))
  243. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  244. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  245. os.environ["no_proxy"] = "example1.com,example2.com, echo.websocket.org"
  246. self.assertEqual(get_proxy_info("echo.websocket.org", True), (None, 0, None))
  247. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  248. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  249. os.environ["no_proxy"] = "example1.com,example2.com, .websocket.org"
  250. self.assertEqual(get_proxy_info("echo.websocket.org", True), (None, 0, None))
  251. os.environ["http_proxy"] = "http://a:b@localhost:3128/"
  252. os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
  253. os.environ["no_proxy"] = "127.0.0.0/8, 192.168.0.0/16"
  254. self.assertEqual(get_proxy_info("127.0.0.1", False), (None, 0, None))
  255. self.assertEqual(get_proxy_info("192.168.1.1", False), (None, 0, None))
  256. if __name__ == "__main__":
  257. unittest.main()