client_test.go 792 Bytes
Newer Older
Hamdi Allam's avatar
Hamdi Allam committed
1 2 3
package node

import (
4
	"context"
Hamdi Allam's avatar
Hamdi Allam committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
	"fmt"
	"net"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestDialEthClientUnavailable(t *testing.T) {
	listener, err := net.Listen("tcp4", ":0")
	require.NoError(t, err)
	defer listener.Close()

	a := listener.Addr().String()
	parts := strings.Split(a, ":")
	addr := fmt.Sprintf("http://localhost:%s", parts[1])

	metrics := &clientMetrics{}

	// available
25
	_, err = DialEthClient(context.Background(), addr, metrics)
Hamdi Allam's avatar
Hamdi Allam committed
26 27 28
	require.NoError(t, err)

	// :0 requests a new unbound port
29
	_, err = DialEthClient(context.Background(), "http://localhost:0", metrics)
Hamdi Allam's avatar
Hamdi Allam committed
30 31 32
	require.Error(t, err)

	// Fail open if we don't recognize the scheme
33
	_, err = DialEthClient(context.Background(), "mailto://example.com", metrics)
Hamdi Allam's avatar
Hamdi Allam committed
34 35
	require.Error(t, err)
}