33from contextlib import contextmanager
44from http .client import HTTPMessage
55from io import BytesIO
6+ from typing import NamedTuple
67from unittest .mock import MagicMock , patch
78
8- from vlogs_handler . request import post_ndjson
9+ from vlogs_handler import request
910
1011MODULE_PATH = "vlogs_handler.request"
1112
@@ -20,7 +21,7 @@ def make_http_error(status=404, message="Not Found", url="https://example.com"):
2021
2122def make_urlopen_fake (exception = None ):
2223 @contextmanager
23- def urlopen_fake (* args , ** kwargs ):
24+ def urlopen_fake (req , timeout ):
2425 if exception :
2526 raise exception
2627 yield MagicMock ()
@@ -37,7 +38,7 @@ def test_should_submit_successfully(self):
3738 # when
3839 with patch (MODULE_PATH + ".urllib.request.urlopen" ) as m :
3940 m .side_effect = make_urlopen_fake ()
40- got = post_ndjson (url = url , data = data )
41+ got = request . post_ndjson (url = url , data = data )
4142
4243 # then
4344 self .assertTrue (got )
@@ -50,7 +51,7 @@ def test_should_handle_http_exception(self):
5051 # when
5152 with patch (MODULE_PATH + ".urllib.request.urlopen" ) as m :
5253 m .side_effect = make_urlopen_fake (exception = make_http_error ())
53- got = post_ndjson (url = url , data = data )
54+ got = request . post_ndjson (url = url , data = data )
5455
5556 # then
5657 self .assertFalse (got )
@@ -65,7 +66,7 @@ def test_should_handle_url_exception(self):
6566 m .side_effect = make_urlopen_fake (
6667 exception = urllib .error .URLError ("Network is unreachable" )
6768 )
68- got = post_ndjson (url = url , data = data )
69+ got = request . post_ndjson (url = url , data = data )
6970
7071 # then
7172 self .assertFalse (got )
@@ -79,7 +80,25 @@ def test_should_handle_general_exception(self):
7980 # when
8081 with patch (MODULE_PATH + ".urllib.request.urlopen" ) as m :
8182 m .side_effect = make_urlopen_fake (exception = RuntimeError )
82- got = post_ndjson (url = url , data = data , timeout = timeout )
83+ got = request . post_ndjson (url = url , data = data , timeout = timeout )
8384
8485 # then
8586 self .assertFalse (got )
87+
88+
89+ class TestIsURL (unittest .TestCase ):
90+ def test_all (self ):
91+ class Case (NamedTuple ):
92+ url : str
93+ want : bool
94+
95+ cases = [
96+ Case ("http://www.example.com" , True ),
97+ Case ("http://localhost:9428" , True ),
98+ Case ("http://0.0.0.0:9428" , True ),
99+ Case ("www.example.com" , False ),
100+ ]
101+
102+ for tc in cases :
103+ with self .subTest (url = tc .url ):
104+ self .assertIs (request .is_url (tc .url ), tc .want )
0 commit comments