github.com/yandex/pandora@v0.5.32/components/phttp/import/import_test.go (about)

     1  package phttp
     2  
     3  import (
     4  	"net"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/require"
    10  	phttp "github.com/yandex/pandora/components/guns/http"
    11  )
    12  
    13  func TestImport_NotPanics(t *testing.T) {
    14  	require.NotPanics(t, func() {
    15  		Import(afero.NewOsFs())
    16  	})
    17  }
    18  
    19  func Test_preResolveTargetAddr(t *testing.T) {
    20  	listener, err := net.ListenTCP("tcp4", nil)
    21  	if listener != nil {
    22  		defer listener.Close()
    23  	}
    24  	require.NoError(t, err)
    25  
    26  	port := strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)
    27  
    28  	tests := []struct {
    29  		name           string
    30  		target         string
    31  		wantTargetAddr string
    32  		wantDNSCache   bool
    33  		wantErr        bool
    34  	}{
    35  		{
    36  			name:           "ip target",
    37  			target:         "localhost:" + port,
    38  			wantTargetAddr: "127.0.0.1:" + port,
    39  			wantDNSCache:   false,
    40  			wantErr:        false,
    41  		},
    42  		{
    43  			name:           "ip target",
    44  			target:         "127.0.0.1:80",
    45  			wantTargetAddr: "127.0.0.1:80",
    46  			wantDNSCache:   false,
    47  			wantErr:        false,
    48  		},
    49  		{
    50  			name:           "failed",
    51  			target:         "localhost:54321",
    52  			wantTargetAddr: "localhost:54321",
    53  			wantDNSCache:   true,
    54  			wantErr:        true,
    55  		},
    56  	}
    57  	for _, tt := range tests {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			conf := &phttp.ClientConfig{}
    60  			conf.Dialer.DNSCache = true
    61  
    62  			got, err := phttp.PreResolveTargetAddr(conf, tt.target)
    63  			if tt.wantErr {
    64  				require.Error(t, err)
    65  			} else {
    66  				require.NoError(t, err)
    67  			}
    68  			require.Equal(t, tt.wantTargetAddr, got)
    69  			require.Equal(t, tt.wantDNSCache, conf.Dialer.DNSCache)
    70  		})
    71  	}
    72  }