import ipaddress
import socket

from ..urlsanitize import PossibleSSRFAttempt, sanitize_user_input_url, CannotResolveHost, InvalidURL

RAISE = object()
RETURN = object()

GOOGLE_IP = ipaddress.ip_address(socket.gethostbyname('google.com'))

SUITE = [
    ('https://google.com/', (RETURN, GOOGLE_IP)),
    ('http://google.com/', (RETURN, GOOGLE_IP)),
    ('http://google.com', (RETURN, GOOGLE_IP)),
    ('http://foo.example.com/', (RAISE, CannotResolveHost())),
    ('http://localhost/', (RAISE, PossibleSSRFAttempt('is_loopback'))),
    ('http://Localhost/', (RAISE, PossibleSSRFAttempt('is_loopback'))),
    ('http://169.254.169.254/latest/meta-data', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://2852039166/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://7147006462/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://0xA9.0xFE.0xA9.0xFE/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://0x41414141A9FEA9FE/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://0xA9FEA9FE/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://0251.0376.0251.0376/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://0251.00376.000251.0000376/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://169.254.169.254.xip.io/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://A.8.8.8.8.1time.169.254.169.254.1time.repeat.rebind.network/', (RAISE, PossibleSSRFAttempt('is_link_local'))),
    ('http://10.124.10.11', (RAISE, PossibleSSRFAttempt('is_private'))),
    ('some-non-url', (RAISE, InvalidURL())),
]


def test_example_suite():
    for input_url, (result, value) in SUITE:
        print(input_url)
        if result is RETURN:
            assert sanitize_user_input_url(input_url) == value, \
                f"sanitize_url({input_url!r}) should be {value!r} was {sanitize_user_input_url(input_url)}"
        elif result is RAISE:
            try:
                sanitize_user_input_url(input_url)
            except value.__class__ as e:
                if value.__class__ == PossibleSSRFAttempt:
                    assert e.reason == value.reason, \
                        f"sanitize_url({input_url!r} should raise {value.__class__.__name__}({value.reason}) raised {value.__class__.__name__}({e.reason})"
                else:
                    assert str(e) == str(value), \
                        f"sanitize_url({input_url!r} should raise {value.__class__.__name__}({str(value)}) raised {value.__class__.__name__}({str(e)})"
        else:
            raise Exception("result in suite should be RETURN or RAISE")


if __name__ == '__main__':
    test_example_suite()

@@ -164,16 +164,119 @@
 ject()%0A%0A
+GOOGLE_IP = SUITE = None  # set in setup_module%0A%0A%0Adef setup_module():%0A    global GOOGLE_IP, SUITE%0A%0A    
 GOOGLE_I
@@ -336,17 +336,20 @@
 .com'))%0A
-%0A
+    
 SUITE = 
@@ -350,16 +350,20 @@
 ITE = %5B%0A
+    
     ('ht
@@ -400,32 +400,36 @@
 OOGLE_IP)),%0A    
+   
+ 
 ('http://google.
@@ -453,32 +453,36 @@
 OOGLE_IP)),%0A    
+  
+  
 ('http://google.
@@ -505,32 +505,36 @@
 OOGLE_IP)),%0A    
+ 
+   
 ('http://foo.exa
@@ -572,32 +572,36 @@
 veHost())),%0A    
+   
+ 
 ('http://localho
@@ -648,32 +648,36 @@
 opback'))),%0A    
+  
+  
 ('http://Localho
@@ -724,32 +724,36 @@
 opback'))),%0A    
+ 
+   
 ('http://169.254
@@ -824,32 +824,36 @@
 _local'))),%0A    
+  
+  
 ('http://2852039
@@ -899,32 +899,36 @@
 link_local'))),%0A
+    
     ('http://714
@@ -978,32 +978,36 @@
 link_local'))),%0A
+    
     ('http://0xA
@@ -1070,32 +1070,36 @@
 _local'))),%0A    
+  
+  
 ('http://0x41414
@@ -1153,32 +1153,36 @@
 link_local'))),%0A
+    
     ('http://0xA
@@ -1232,32 +1232,36 @@
 link_local'))),%0A
+    
     ('http://025
@@ -1324,32 +1324,36 @@
 _local'))),%0A    
+ 
+   
 ('http://0251.00
@@ -1418,32 +1418,36 @@
 _local'))),%0A    
+   
+ 
 ('http://169.254
@@ -1505,32 +1505,36 @@
 link_local'))),%0A
+    
     ('http://A.8
@@ -1641,24 +1641,28 @@
 al'))),%0A    
+ 
+   
 ('http://10.
@@ -1714,24 +1714,28 @@
 rivate'))),%0A
+    
     ('some-n
@@ -1767,16 +1767,20 @@
 RL())),%0A
+    
 %5D%0A%0A%0Adef 

Use setup_module in urlsanitize tests to avoid network at module import time