github.com/noirx94/tendermintmp@v0.0.1/rpc/jsonrpc/client/http_json_client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestHTTPClientMakeHTTPDialer(t *testing.T) {
    14  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    15  		_, _ = w.Write([]byte("Hi!\n"))
    16  	})
    17  	ts := httptest.NewServer(handler)
    18  	defer ts.Close()
    19  
    20  	tsTLS := httptest.NewTLSServer(handler)
    21  	defer tsTLS.Close()
    22  	// This silences a TLS handshake error, caused by the dialer just immediately
    23  	// disconnecting, which we can just ignore.
    24  	tsTLS.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
    25  
    26  	for _, testURL := range []string{ts.URL, tsTLS.URL} {
    27  		u, err := newParsedURL(testURL)
    28  		require.NoError(t, err)
    29  		dialFn, err := makeHTTPDialer(testURL)
    30  		require.Nil(t, err)
    31  
    32  		addr, err := dialFn(u.Scheme, u.GetHostWithPath())
    33  		require.NoError(t, err)
    34  		require.NotNil(t, addr)
    35  	}
    36  }
    37  
    38  func Test_parsedURL(t *testing.T) {
    39  	type test struct {
    40  		url                  string
    41  		expectedURL          string
    42  		expectedHostWithPath string
    43  		expectedDialAddress  string
    44  	}
    45  
    46  	tests := map[string]test{
    47  		"unix endpoint": {
    48  			url:                  "unix:///tmp/test",
    49  			expectedURL:          "unix://.tmp.test",
    50  			expectedHostWithPath: "/tmp/test",
    51  			expectedDialAddress:  "/tmp/test",
    52  		},
    53  
    54  		"http endpoint": {
    55  			url:                  "https://example.com",
    56  			expectedURL:          "https://example.com",
    57  			expectedHostWithPath: "example.com",
    58  			expectedDialAddress:  "example.com",
    59  		},
    60  
    61  		"http endpoint with port": {
    62  			url:                  "https://example.com:8080",
    63  			expectedURL:          "https://example.com:8080",
    64  			expectedHostWithPath: "example.com:8080",
    65  			expectedDialAddress:  "example.com:8080",
    66  		},
    67  
    68  		"http path routed endpoint": {
    69  			url:                  "https://example.com:8080/rpc",
    70  			expectedURL:          "https://example.com:8080/rpc",
    71  			expectedHostWithPath: "example.com:8080/rpc",
    72  			expectedDialAddress:  "example.com:8080",
    73  		},
    74  	}
    75  
    76  	for name, tt := range tests {
    77  		tt := tt // suppressing linter
    78  		t.Run(name, func(t *testing.T) {
    79  			parsed, err := newParsedURL(tt.url)
    80  			require.NoError(t, err)
    81  			require.Equal(t, tt.expectedDialAddress, parsed.GetDialAddress())
    82  			require.Equal(t, tt.expectedURL, parsed.GetTrimmedURL())
    83  			require.Equal(t, tt.expectedHostWithPath, parsed.GetHostWithPath())
    84  		})
    85  	}
    86  }