Skip to content

Commit

Permalink
Run black, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
engn33r committed Jan 8, 2024
1 parent 83fea0e commit cf2eb0b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
4 changes: 1 addition & 3 deletions websocket/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ def __init__(self):

class HttpTest(unittest.TestCase):
def test_read_header(self):
status, header, _ = read_headers(
HeaderSockMock("data/header01.txt")
)
status, header, _ = read_headers(HeaderSockMock("data/header01.txt"))
self.assertEqual(status, 101)
self.assertEqual(header["connection"], "Upgrade")
# header02.txt is intentionally malformed
Expand Down
43 changes: 30 additions & 13 deletions websocket/tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_proxy_info,
parse_url,
)
from websocket._exceptions import WebSocketProxyException

"""
test_url.py
Expand Down Expand Up @@ -119,12 +120,12 @@ def test_parse_url(self):


class IsNoProxyHostTest(unittest.TestCase):
def setup(self):
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):
def tearDown(self):
if self.no_proxy:
os.environ["no_proxy"] = self.no_proxy
elif "no_proxy" in os.environ:
Expand All @@ -133,6 +134,10 @@ def teardown(self):
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", "*"])
)
Expand Down Expand Up @@ -200,7 +205,7 @@ def test_hostname_match_domain(self):


class ProxyInfoTest(unittest.TestCase):
def setup(self):
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)
Expand All @@ -211,7 +216,7 @@ def setup(self):
if "no_proxy" in os.environ:
del os.environ["no_proxy"]

def teardown(self):
def tearDown(self):
if self.http_proxy:
os.environ["http_proxy"] = self.http_proxy
elif "http_proxy" in os.environ:
Expand All @@ -228,20 +233,19 @@ def teardown(self):
del os.environ["no_proxy"]

def test_proxy_from_args(self):
self.assertEqual(
get_proxy_info("echo.websocket.events", False, proxy_host="localhost"),
("localhost", 0, None),
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"),
("localhost", 0, None),
)
self.assertEqual(
get_proxy_info(
"echo.websocket.events", True, proxy_host="localhost", proxy_port=3128
Expand All @@ -254,9 +258,10 @@ def test_proxy_from_args(self):
"echo.websocket.events",
False,
proxy_host="localhost",
proxy_port=9001,
proxy_auth=("a", "b"),
),
("localhost", 0, ("a", "b")),
("localhost", 9001, ("a", "b")),
)
self.assertEqual(
get_proxy_info(
Expand All @@ -273,9 +278,10 @@ def test_proxy_from_args(self):
"echo.websocket.events",
True,
proxy_host="localhost",
proxy_port=8765,
proxy_auth=("a", "b"),
),
("localhost", 0, ("a", "b")),
("localhost", 8765, ("a", "b")),
)
self.assertEqual(
get_proxy_info(
Expand Down Expand Up @@ -311,6 +317,17 @@ def test_proxy_from_args(self):
(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(
Expand Down
20 changes: 6 additions & 14 deletions websocket/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def __init__(self, fname):


class WebSocketTest(unittest.TestCase):
def setup(self):
def setUp(self):
ws.enableTrace(TRACEABLE)

def teardown(self):
def tearDown(self):
pass

def test_default_timeout(self):
Expand Down Expand Up @@ -158,15 +158,11 @@ def test_ws_utils(self):
self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (False, None))

def test_read_header(self):
status, header, _ = read_headers(
HeaderSockMock("data/header01.txt")
)
status, header, _ = read_headers(HeaderSockMock("data/header01.txt"))
self.assertEqual(status, 101)
self.assertEqual(header["connection"], "Upgrade")

status, header, _ = read_headers(
HeaderSockMock("data/header03.txt")
)
status, header, _ = read_headers(HeaderSockMock("data/header03.txt"))
self.assertEqual(status, 101)
self.assertEqual(header["connection"], "Upgrade, Keep-Alive")

Expand Down Expand Up @@ -379,9 +375,7 @@ def test_ping_pong(self):
@unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
def test_support_redirect(self):
s = ws.WebSocket()
self.assertRaises(
WebSocketBadStatusException, s.connect, "ws://google.com/"
)
self.assertRaises(WebSocketBadStatusException, s.connect, "ws://google.com/")
# Need to find a URL that has a redirect code leading to a websocket

@unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
Expand Down Expand Up @@ -495,9 +489,7 @@ def test_ipv6(self):
def test_bad_urls(self):
websock3 = ws.WebSocket()
self.assertRaises(ValueError, websock3.connect, "ws//example.com")
self.assertRaises(
WebSocketAddressException, websock3.connect, "ws://example"
)
self.assertRaises(WebSocketAddressException, websock3.connect, "ws://example")
self.assertRaises(ValueError, websock3.connect, "example.com")


Expand Down

0 comments on commit cf2eb0b

Please sign in to comment.