You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.0 KiB

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