Commit cf4939e8 authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #6785 from ethereum-optimism/jg/fix_url_dial

Fix IsURLAvailable
parents 234351c3 454613f9
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestIsURLAvailable(t *testing.T) { func TestIsURLAvailableLocal(t *testing.T) {
listener, err := net.Listen("tcp4", ":0") listener, err := net.Listen("tcp4", ":0")
require.NoError(t, err) require.NoError(t, err)
defer listener.Close() defer listener.Close()
...@@ -18,6 +18,39 @@ func TestIsURLAvailable(t *testing.T) { ...@@ -18,6 +18,39 @@ func TestIsURLAvailable(t *testing.T) {
parts := strings.Split(a, ":") parts := strings.Split(a, ":")
addr := fmt.Sprintf("http://localhost:%s", parts[1]) addr := fmt.Sprintf("http://localhost:%s", parts[1])
// True & False with ports
require.True(t, IsURLAvailable(addr)) require.True(t, IsURLAvailable(addr))
require.False(t, IsURLAvailable("http://localhost:0")) require.False(t, IsURLAvailable("http://localhost:0"))
// Fail open if we don't recognize the scheme
require.True(t, IsURLAvailable("mailto://example.com"))
}
func TestIsURLAvailableNonLocal(t *testing.T) {
if !IsURLAvailable("http://example.com") {
t.Skip("No internet connection found, skipping this test")
}
// True without ports. http & https
require.True(t, IsURLAvailable("http://example.com"))
require.True(t, IsURLAvailable("http://example.com/hello"))
require.True(t, IsURLAvailable("https://example.com"))
require.True(t, IsURLAvailable("https://example.com/hello"))
// True without ports. ws & wss
require.True(t, IsURLAvailable("ws://example.com"))
require.True(t, IsURLAvailable("ws://example.com/hello"))
require.True(t, IsURLAvailable("wss://example.com"))
require.True(t, IsURLAvailable("wss://example.com/hello"))
// False without ports
require.False(t, IsURLAvailable("http://fakedomainnamethatdoesnotexistandshouldneverexist.com"))
require.False(t, IsURLAvailable("http://fakedomainnamethatdoesnotexistandshouldneverexist.com/hello"))
require.False(t, IsURLAvailable("https://fakedomainnamethatdoesnotexistandshouldneverexist.com"))
require.False(t, IsURLAvailable("https://fakedomainnamethatdoesnotexistandshouldneverexist.com/hello"))
require.False(t, IsURLAvailable("ws://fakedomainnamethatdoesnotexistandshouldneverexist.com"))
require.False(t, IsURLAvailable("ws://fakedomainnamethatdoesnotexistandshouldneverexist.com/hello"))
require.False(t, IsURLAvailable("wss://fakedomainnamethatdoesnotexistandshouldneverexist.com"))
require.False(t, IsURLAvailable("wss://fakedomainnamethatdoesnotexistandshouldneverexist.com/hello"))
} }
...@@ -122,7 +122,19 @@ func IsURLAvailable(address string) bool { ...@@ -122,7 +122,19 @@ func IsURLAvailable(address string) bool {
if err != nil { if err != nil {
return false return false
} }
conn, err := net.DialTimeout("tcp", u.Host, 5*time.Second) addr := u.Host
if u.Port() == "" {
switch u.Scheme {
case "http", "ws":
addr += ":80"
case "https", "wss":
addr += ":443"
default:
// Fail open if we can't figure out what the port should be
return true
}
}
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil { if err != nil {
return false return false
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment