github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/communication/nats/connection_wrap_test.go (about) 1 /* 2 * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package nats 19 20 import ( 21 "net/url" 22 "testing" 23 24 "github.com/pkg/errors" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestParseServerURL(t *testing.T) { 29 tests := []struct { 30 uri string 31 wantAddress *url.URL 32 wantError error 33 }{ 34 {"127.0.0.1", &url.URL{Scheme: "nats", Host: "127.0.0.1:4222"}, nil}, 35 {"nats://127.0.0.1", &url.URL{Scheme: "nats", Host: "127.0.0.1:4222"}, nil}, 36 {"127.0.0.1:4222", &url.URL{Scheme: "nats", Host: "127.0.0.1:4222"}, nil}, 37 {"nats://127.0.0.1:4222", &url.URL{Scheme: "nats", Host: "127.0.0.1:4222"}, nil}, 38 39 {"nats://127.0.0.1:4333", &url.URL{Scheme: "nats", Host: "127.0.0.1:4333"}, nil}, 40 {"nats://example.com:4333", &url.URL{Scheme: "nats", Host: "example.com:4333"}, nil}, 41 42 { 43 "nats:// example.com", 44 nil, 45 errors.New(`failed to parse NATS server URI "nats:// example.com"`), 46 }, 47 { 48 "nats://example.com:a", 49 nil, 50 errors.New(`failed to parse NATS server URI "nats://example.com:a":`), 51 }, 52 } 53 54 for _, tc := range tests { 55 address, err := ParseServerURL(tc.uri) 56 if tc.wantError != nil { 57 assert.Contains(t, err.Error(), tc.wantError.Error()) 58 } else { 59 assert.NoError(t, err) 60 } 61 assert.Equal(t, tc.wantAddress, address) 62 } 63 } 64 65 func TestConnectionWrap_NewConnection(t *testing.T) { 66 connection, err := newConnection(nil, "nats://127.0.0.1:4222") 67 assert.NoError(t, err) 68 assert.Nil(t, connection.Conn) 69 assert.Equal(t, []string{"nats://127.0.0.1:4222"}, connection.Servers()) 70 71 connection, err = newConnection(nil, "nats://127.0.0.1:4222", "nats://example.com:4222") 72 assert.Nil(t, connection.Conn) 73 assert.Equal(t, []string{"nats://127.0.0.1:4222", "nats://example.com:4222"}, connection.Servers()) 74 } 75 76 func TestConnectionWrap_Servers(t *testing.T) { 77 connection, _ := newConnection(nil, "nats://far-server:1234") 78 assert.Equal(t, []string{"nats://far-server:1234"}, connection.Servers()) 79 }