import ipaddress
import socket

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

RAISE = object()
RETURN = object()

GOOGLE_IP = SUITE = None  # set in setup_module


def setup_module():
    global GOOGLE_IP, SUITE

    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")

@@ -25,16 +25,54 @@
 socket%0A%0A
+from testil import eq, assert_raises%0A%0A
 from ..u
@@ -1901,32 +1901,33 @@
 rint(input_url)%0A
+%0A
         if resul
@@ -1951,23 +1951,19 @@
         
-assert 
+eq(
 sanitize
@@ -1992,362 +1992,179 @@
 url)
- == value, %5C%0A                f%22sanitize_url(%7Binput_url!r%7D) should be %7Bvalue!r%7D was %7Bsanitize_user_input_url(input_url)%7D%22%0A        elif result is RAISE:%0A            try:%0A                sanitize_user_input_url(input_url)%0A            except value.__class__ as e:%0A                if value.__class__ == PossibleSSRFAttempt:%0A                    assert 
+, value)%0A        elif result is RAISE:%0A            if type(value) == PossibleSSRFAttempt:%0A                with assert_raises(PossibleSSRFAttempt, msg=lambda e: eq(
 e.reason
  == 
@@ -2159,19 +2159,17 @@
 e.reason
- ==
+,
  value.r
@@ -2173,19 +2173,19 @@
 e.reason
-, %5C
+)):
 %0A       
@@ -2189,38 +2189,32 @@
                 
-    f%22
 sanitize_url(%7Bin
@@ -2199,36 +2199,36 @@
       sanitize_u
-rl(%7B
+ser_
 input_url!r%7D sho
@@ -2224,119 +2224,20 @@
 _url
-!r%7D should raise %7Bvalue.__class__.__name__%7D(%7Bvalue.reason%7D) raised %7Bvalue.__class__.__name__%7D(%7Be.reason%7D)%22%0A    
+(input_url)%0A
     
@@ -2270,29 +2270,44 @@
     
-   
+with
  assert
- str(e) == 
+_raises(type(value), msg=
 str(
@@ -2316,11 +2316,10 @@
 lue)
-, %5C
+):
 %0A   
@@ -2339,14 +2339,8 @@
     
-    f%22
 sani
@@ -2349,12 +2349,12 @@
 ze_u
-rl(%7B
+ser_
 inpu
@@ -2362,110 +2362,19 @@
 _url
-!r%7D should raise %7Bvalue.__class__.__name__%7D(%7Bstr(value)%7D) raised %7Bvalue.__class__.__name__%7D(%7Bstr(e)%7D)%22
+(input_url)
 %0A   

Refactor urlsanitize tests to use testil for precision and clarity