123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- # -*- coding: utf-8 -*-
- #
- import os
- import unittest
- from websocket._url import (
- _is_address_in_network,
- _is_no_proxy_host,
- get_proxy_info,
- parse_url,
- )
- from websocket._exceptions import WebSocketProxyException
- """
- test_url.py
- websocket - WebSocket client library for Python
- Copyright 2024 engn33r
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- """
- class UrlTest(unittest.TestCase):
- def test_address_in_network(self):
- self.assertTrue(_is_address_in_network("127.0.0.1", "127.0.0.0/8"))
- self.assertTrue(_is_address_in_network("127.1.0.1", "127.0.0.0/8"))
- self.assertFalse(_is_address_in_network("127.1.0.1", "127.0.0.0/24"))
- def test_parse_url(self):
- p = parse_url("ws://www.example.com/r")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 80)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], False)
- p = parse_url("ws://www.example.com/r/")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 80)
- self.assertEqual(p[2], "/r/")
- self.assertEqual(p[3], False)
- p = parse_url("ws://www.example.com/")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 80)
- self.assertEqual(p[2], "/")
- self.assertEqual(p[3], False)
- p = parse_url("ws://www.example.com")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 80)
- self.assertEqual(p[2], "/")
- self.assertEqual(p[3], False)
- p = parse_url("ws://www.example.com:8080/r")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], False)
- p = parse_url("ws://www.example.com:8080/")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/")
- self.assertEqual(p[3], False)
- p = parse_url("ws://www.example.com:8080")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/")
- self.assertEqual(p[3], False)
- p = parse_url("wss://www.example.com:8080/r")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], True)
- p = parse_url("wss://www.example.com:8080/r?key=value")
- self.assertEqual(p[0], "www.example.com")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/r?key=value")
- self.assertEqual(p[3], True)
- self.assertRaises(ValueError, parse_url, "http://www.example.com/r")
- p = parse_url("ws://[2a03:4000:123:83::3]/r")
- self.assertEqual(p[0], "2a03:4000:123:83::3")
- self.assertEqual(p[1], 80)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], False)
- p = parse_url("ws://[2a03:4000:123:83::3]:8080/r")
- self.assertEqual(p[0], "2a03:4000:123:83::3")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], False)
- p = parse_url("wss://[2a03:4000:123:83::3]/r")
- self.assertEqual(p[0], "2a03:4000:123:83::3")
- self.assertEqual(p[1], 443)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], True)
- p = parse_url("wss://[2a03:4000:123:83::3]:8080/r")
- self.assertEqual(p[0], "2a03:4000:123:83::3")
- self.assertEqual(p[1], 8080)
- self.assertEqual(p[2], "/r")
- self.assertEqual(p[3], True)
- class IsNoProxyHostTest(unittest.TestCase):
- def setUp(self):
- self.no_proxy = os.environ.get("no_proxy", None)
- if "no_proxy" in os.environ:
- del os.environ["no_proxy"]
- def tearDown(self):
- if self.no_proxy:
- os.environ["no_proxy"] = self.no_proxy
- elif "no_proxy" in os.environ:
- del os.environ["no_proxy"]
- def test_match_all(self):
- self.assertTrue(_is_no_proxy_host("any.websocket.org", ["*"]))
- self.assertTrue(_is_no_proxy_host("192.168.0.1", ["*"]))
- self.assertFalse(_is_no_proxy_host("192.168.0.1", ["192.168.1.1"]))
- self.assertFalse(
- _is_no_proxy_host("any.websocket.org", ["other.websocket.org"])
- )
- self.assertTrue(
- _is_no_proxy_host("any.websocket.org", ["other.websocket.org", "*"])
- )
- os.environ["no_proxy"] = "*"
- self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
- self.assertTrue(_is_no_proxy_host("192.168.0.1", None))
- os.environ["no_proxy"] = "other.websocket.org, *"
- self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
- def test_ip_address(self):
- self.assertTrue(_is_no_proxy_host("127.0.0.1", ["127.0.0.1"]))
- self.assertFalse(_is_no_proxy_host("127.0.0.2", ["127.0.0.1"]))
- self.assertTrue(
- _is_no_proxy_host("127.0.0.1", ["other.websocket.org", "127.0.0.1"])
- )
- self.assertFalse(
- _is_no_proxy_host("127.0.0.2", ["other.websocket.org", "127.0.0.1"])
- )
- os.environ["no_proxy"] = "127.0.0.1"
- self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
- self.assertFalse(_is_no_proxy_host("127.0.0.2", None))
- os.environ["no_proxy"] = "other.websocket.org, 127.0.0.1"
- self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
- self.assertFalse(_is_no_proxy_host("127.0.0.2", None))
- def test_ip_address_in_range(self):
- self.assertTrue(_is_no_proxy_host("127.0.0.1", ["127.0.0.0/8"]))
- self.assertTrue(_is_no_proxy_host("127.0.0.2", ["127.0.0.0/8"]))
- self.assertFalse(_is_no_proxy_host("127.1.0.1", ["127.0.0.0/24"]))
- os.environ["no_proxy"] = "127.0.0.0/8"
- self.assertTrue(_is_no_proxy_host("127.0.0.1", None))
- self.assertTrue(_is_no_proxy_host("127.0.0.2", None))
- os.environ["no_proxy"] = "127.0.0.0/24"
- self.assertFalse(_is_no_proxy_host("127.1.0.1", None))
- def test_hostname_match(self):
- self.assertTrue(_is_no_proxy_host("my.websocket.org", ["my.websocket.org"]))
- self.assertTrue(
- _is_no_proxy_host(
- "my.websocket.org", ["other.websocket.org", "my.websocket.org"]
- )
- )
- self.assertFalse(_is_no_proxy_host("my.websocket.org", ["other.websocket.org"]))
- os.environ["no_proxy"] = "my.websocket.org"
- self.assertTrue(_is_no_proxy_host("my.websocket.org", None))
- self.assertFalse(_is_no_proxy_host("other.websocket.org", None))
- os.environ["no_proxy"] = "other.websocket.org, my.websocket.org"
- self.assertTrue(_is_no_proxy_host("my.websocket.org", None))
- def test_hostname_match_domain(self):
- self.assertTrue(_is_no_proxy_host("any.websocket.org", [".websocket.org"]))
- self.assertTrue(_is_no_proxy_host("my.other.websocket.org", [".websocket.org"]))
- self.assertTrue(
- _is_no_proxy_host(
- "any.websocket.org", ["my.websocket.org", ".websocket.org"]
- )
- )
- self.assertFalse(_is_no_proxy_host("any.websocket.com", [".websocket.org"]))
- os.environ["no_proxy"] = ".websocket.org"
- self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
- self.assertTrue(_is_no_proxy_host("my.other.websocket.org", None))
- self.assertFalse(_is_no_proxy_host("any.websocket.com", None))
- os.environ["no_proxy"] = "my.websocket.org, .websocket.org"
- self.assertTrue(_is_no_proxy_host("any.websocket.org", None))
- class ProxyInfoTest(unittest.TestCase):
- def setUp(self):
- self.http_proxy = os.environ.get("http_proxy", None)
- self.https_proxy = os.environ.get("https_proxy", None)
- self.no_proxy = os.environ.get("no_proxy", None)
- if "http_proxy" in os.environ:
- del os.environ["http_proxy"]
- if "https_proxy" in os.environ:
- del os.environ["https_proxy"]
- if "no_proxy" in os.environ:
- del os.environ["no_proxy"]
- def tearDown(self):
- if self.http_proxy:
- os.environ["http_proxy"] = self.http_proxy
- elif "http_proxy" in os.environ:
- del os.environ["http_proxy"]
- if self.https_proxy:
- os.environ["https_proxy"] = self.https_proxy
- elif "https_proxy" in os.environ:
- del os.environ["https_proxy"]
- if self.no_proxy:
- os.environ["no_proxy"] = self.no_proxy
- elif "no_proxy" in os.environ:
- del os.environ["no_proxy"]
- def test_proxy_from_args(self):
- self.assertRaises(
- WebSocketProxyException,
- get_proxy_info,
- "echo.websocket.events",
- False,
- proxy_host="localhost",
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events", False, proxy_host="localhost", proxy_port=3128
- ),
- ("localhost", 3128, None),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events", True, proxy_host="localhost", proxy_port=3128
- ),
- ("localhost", 3128, None),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- False,
- proxy_host="localhost",
- proxy_port=9001,
- proxy_auth=("a", "b"),
- ),
- ("localhost", 9001, ("a", "b")),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- False,
- proxy_host="localhost",
- proxy_port=3128,
- proxy_auth=("a", "b"),
- ),
- ("localhost", 3128, ("a", "b")),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- True,
- proxy_host="localhost",
- proxy_port=8765,
- proxy_auth=("a", "b"),
- ),
- ("localhost", 8765, ("a", "b")),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- True,
- proxy_host="localhost",
- proxy_port=3128,
- proxy_auth=("a", "b"),
- ),
- ("localhost", 3128, ("a", "b")),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- True,
- proxy_host="localhost",
- proxy_port=3128,
- no_proxy=["example.com"],
- proxy_auth=("a", "b"),
- ),
- ("localhost", 3128, ("a", "b")),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- True,
- proxy_host="localhost",
- proxy_port=3128,
- no_proxy=["echo.websocket.events"],
- proxy_auth=("a", "b"),
- ),
- (None, 0, None),
- )
- self.assertEqual(
- get_proxy_info(
- "echo.websocket.events",
- True,
- proxy_host="localhost",
- proxy_port=3128,
- no_proxy=[".websocket.events"],
- ),
- (None, 0, None),
- )
- def test_proxy_from_env(self):
- os.environ["http_proxy"] = "http://localhost/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), ("localhost", None, None)
- )
- os.environ["http_proxy"] = "http://localhost:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), ("localhost", 3128, None)
- )
- os.environ["http_proxy"] = "http://localhost/"
- os.environ["https_proxy"] = "http://localhost2/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), ("localhost", None, None)
- )
- os.environ["http_proxy"] = "http://localhost:3128/"
- os.environ["https_proxy"] = "http://localhost2:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), ("localhost", 3128, None)
- )
- os.environ["http_proxy"] = "http://localhost/"
- os.environ["https_proxy"] = "http://localhost2/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True), ("localhost2", None, None)
- )
- os.environ["http_proxy"] = "http://localhost:3128/"
- os.environ["https_proxy"] = "http://localhost2:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True), ("localhost2", 3128, None)
- )
- os.environ["http_proxy"] = ""
- os.environ["https_proxy"] = "http://localhost2/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True), ("localhost2", None, None)
- )
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), (None, 0, None)
- )
- os.environ["http_proxy"] = ""
- os.environ["https_proxy"] = "http://localhost2:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True), ("localhost2", 3128, None)
- )
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), (None, 0, None)
- )
- os.environ["http_proxy"] = "http://localhost/"
- os.environ["https_proxy"] = ""
- self.assertEqual(get_proxy_info("echo.websocket.events", True), (None, 0, None))
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), ("localhost", None, None)
- )
- os.environ["http_proxy"] = "http://localhost:3128/"
- os.environ["https_proxy"] = ""
- self.assertEqual(get_proxy_info("echo.websocket.events", True), (None, 0, None))
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False), ("localhost", 3128, None)
- )
- os.environ["http_proxy"] = "http://a:b@localhost/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False),
- ("localhost", None, ("a", "b")),
- )
- os.environ["http_proxy"] = "http://a:b@localhost:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False),
- ("localhost", 3128, ("a", "b")),
- )
- os.environ["http_proxy"] = "http://a:b@localhost/"
- os.environ["https_proxy"] = "http://a:b@localhost2/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False),
- ("localhost", None, ("a", "b")),
- )
- os.environ["http_proxy"] = "http://a:b@localhost:3128/"
- os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", False),
- ("localhost", 3128, ("a", "b")),
- )
- os.environ["http_proxy"] = "http://a:b@localhost/"
- os.environ["https_proxy"] = "http://a:b@localhost2/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True),
- ("localhost2", None, ("a", "b")),
- )
- os.environ["http_proxy"] = "http://a:b@localhost:3128/"
- os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True),
- ("localhost2", 3128, ("a", "b")),
- )
- os.environ[
- "http_proxy"
- ] = "http://john%40example.com:P%40SSWORD@localhost:3128/"
- os.environ[
- "https_proxy"
- ] = "http://john%40example.com:P%40SSWORD@localhost2:3128/"
- self.assertEqual(
- get_proxy_info("echo.websocket.events", True),
- ("localhost2", 3128, ("john@example.com", "P@SSWORD")),
- )
- os.environ["http_proxy"] = "http://a:b@localhost/"
- os.environ["https_proxy"] = "http://a:b@localhost2/"
- os.environ["no_proxy"] = "example1.com,example2.com"
- self.assertEqual(
- get_proxy_info("example.1.com", True), ("localhost2", None, ("a", "b"))
- )
- os.environ["http_proxy"] = "http://a:b@localhost:3128/"
- os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
- os.environ["no_proxy"] = "example1.com,example2.com, echo.websocket.events"
- self.assertEqual(get_proxy_info("echo.websocket.events", True), (None, 0, None))
- os.environ["http_proxy"] = "http://a:b@localhost:3128/"
- os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
- os.environ["no_proxy"] = "example1.com,example2.com, .websocket.events"
- self.assertEqual(get_proxy_info("echo.websocket.events", True), (None, 0, None))
- os.environ["http_proxy"] = "http://a:b@localhost:3128/"
- os.environ["https_proxy"] = "http://a:b@localhost2:3128/"
- os.environ["no_proxy"] = "127.0.0.0/8, 192.168.0.0/16"
- self.assertEqual(get_proxy_info("127.0.0.1", False), (None, 0, None))
- self.assertEqual(get_proxy_info("192.168.1.1", False), (None, 0, None))
- if __name__ == "__main__":
- unittest.main()
|