@@ -94,11 +94,19 @@ def run(
9494 "Run PostgREST and yield an endpoint that is ready for connections."
9595
9696 with tempfile .TemporaryDirectory () as tmpdir :
97+
98+ # with python requests, "localhost" doesn't automatically resolves
99+ # to [::1], hence we use this explicitly when host is ipv6 special
100+ # address
101+ ipv6_special_addresses = ["*6" , "!6" ]
102+
103+ localhost = "[::1]" if host in ipv6_special_addresses else "localhost"
104+
97105 if port :
98106 env ["PGRST_SERVER_PORT" ] = str (port )
99- env ["PGRST_SERVER_HOST" ] = host or " localhost"
107+ env ["PGRST_SERVER_HOST" ] = host or localhost
100108 # When constructing IPv6 address, host address should be bracketed like [host]
101- apihost = f"[{ host } ]" if host and is_ipv6 (host ) else " localhost"
109+ apihost = f"[{ host } ]" if host and is_ipv6 (host ) else localhost
102110 baseurl = f"http://{ apihost } :{ port } "
103111 else :
104112 socketfile = pathlib .Path (tmpdir ) / "postgrest.sock"
@@ -107,7 +115,7 @@ def run(
107115
108116 adminport = freeport (port )
109117 env ["PGRST_ADMIN_SERVER_PORT" ] = str (adminport )
110- adminhost = f"[{ host } ]" if host and is_ipv6 (host ) else " localhost"
118+ adminhost = f"[{ host } ]" if host and is_ipv6 (host ) else localhost
111119 adminurl = f"http://{ adminhost } :{ adminport } "
112120
113121 command = [POSTGREST_BIN ]
0 commit comments