|
| 1 | +package sui |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGrpcTargetFromNodeURL(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + nodeURL string |
| 17 | + want string |
| 18 | + wantErr string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "host with explicit port", |
| 22 | + nodeURL: "http://127.0.0.1:9000", |
| 23 | + want: "127.0.0.1:9000", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "http scheme defaults to port 9000", |
| 27 | + nodeURL: "http://example.com", |
| 28 | + want: "example.com:9000", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "https scheme defaults to port 443", |
| 32 | + nodeURL: "https://example.com", |
| 33 | + want: "example.com:443", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "ipv6 host with port gets bracketed", |
| 37 | + nodeURL: "http://[::1]:9000", |
| 38 | + want: "[::1]:9000", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "ipv6 host without port gets bracketed and defaulted", |
| 42 | + nodeURL: "http://[::1]", |
| 43 | + want: "[::1]:9000", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "invalid URL returns error", |
| 47 | + nodeURL: "http://\x7f", |
| 48 | + wantErr: "parse node URL", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "missing host returns error", |
| 52 | + nodeURL: "http:///path", |
| 53 | + wantErr: "has no host", |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tt := range tests { |
| 58 | + t.Run(tt.name, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + got, err := grpcTargetFromNodeURL(tt.nodeURL) |
| 62 | + if tt.wantErr != "" { |
| 63 | + require.Error(t, err) |
| 64 | + assert.Contains(t, err.Error(), tt.wantErr) |
| 65 | + |
| 66 | + return |
| 67 | + } |
| 68 | + require.NoError(t, err) |
| 69 | + assert.Equal(t, tt.want, got) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestNewPTBClientFromNodeURL(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + log, err := logger.New() |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + t.Run("valid URL with empty token uses default", func(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + |
| 83 | + client, err := NewPTBClientFromNodeURL(log, "http://127.0.0.1:9000", "") |
| 84 | + require.NoError(t, err) |
| 85 | + require.NotNil(t, client) |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("valid URL with explicit token", func(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + |
| 91 | + client, err := NewPTBClientFromNodeURL(log, "http://127.0.0.1:9000", "my-token") |
| 92 | + require.NoError(t, err) |
| 93 | + require.NotNil(t, client) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("invalid URL returns error", func(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + client, err := NewPTBClientFromNodeURL(log, "http://\x7f", "") |
| 100 | + require.Error(t, err) |
| 101 | + require.Nil(t, client) |
| 102 | + }) |
| 103 | +} |
0 commit comments