client_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ecsutil
  4. import (
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "testing"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component/componenttest"
  13. "go.opentelemetry.io/collector/config/confighttp"
  14. )
  15. func TestClient(t *testing.T) {
  16. tr := &fakeRoundTripper{}
  17. baseURL, _ := url.Parse("http://localhost:8080")
  18. client := &clientImpl{
  19. baseURL: *baseURL,
  20. httpClient: http.Client{Transport: tr},
  21. }
  22. require.False(t, tr.closed)
  23. resp, err := client.Get("/stats")
  24. require.NoError(t, err)
  25. require.Equal(t, "hello", string(resp))
  26. require.True(t, tr.closed)
  27. require.Equal(t, baseURL.String()+"/stats", tr.url)
  28. require.Equal(t, 1, len(tr.header))
  29. require.Equal(t, "application/json", tr.header["Content-Type"][0])
  30. require.Equal(t, "GET", tr.method)
  31. }
  32. func TestNewClientProvider(t *testing.T) {
  33. baseURL, _ := url.Parse("http://localhost:8080")
  34. provider := NewClientProvider(*baseURL, confighttp.HTTPClientSettings{}, componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
  35. require.NotNil(t, provider)
  36. _, ok := provider.(*defaultClientProvider)
  37. require.True(t, ok)
  38. client, err := provider.BuildClient()
  39. require.NoError(t, err)
  40. require.Equal(t, "http://localhost:8080", client.(*clientImpl).baseURL.String())
  41. }
  42. func TestDefaultClient(t *testing.T) {
  43. endpoint, _ := url.Parse("http://localhost:8080")
  44. client, err := defaultClient(*endpoint, confighttp.HTTPClientSettings{}, componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
  45. require.NoError(t, err)
  46. require.NotNil(t, client.httpClient.Transport)
  47. require.Equal(t, "http://localhost:8080", client.baseURL.String())
  48. }
  49. func TestBuildReq(t *testing.T) {
  50. endpoint, _ := url.Parse("http://localhost:8080")
  51. p := &defaultClientProvider{
  52. baseURL: *endpoint,
  53. host: componenttest.NewNopHost(),
  54. settings: componenttest.NewNopTelemetrySettings(),
  55. }
  56. cl, err := p.BuildClient()
  57. require.NoError(t, err)
  58. req, err := cl.(*clientImpl).buildReq("/test")
  59. require.NoError(t, err)
  60. require.NotNil(t, req)
  61. require.Equal(t, "application/json", req.Header["Content-Type"][0])
  62. }
  63. func TestBuildBadReq(t *testing.T) {
  64. endpoint, _ := url.Parse("http://localhost:8080")
  65. p := &defaultClientProvider{
  66. baseURL: *endpoint,
  67. host: componenttest.NewNopHost(),
  68. settings: componenttest.NewNopTelemetrySettings(),
  69. }
  70. cl, err := p.BuildClient()
  71. require.NoError(t, err)
  72. _, err = cl.(*clientImpl).buildReq(" ")
  73. require.Error(t, err)
  74. }
  75. func TestGetBad(t *testing.T) {
  76. endpoint, _ := url.Parse("http://localhost:8080")
  77. p := &defaultClientProvider{
  78. baseURL: *endpoint,
  79. host: componenttest.NewNopHost(),
  80. settings: componenttest.NewNopTelemetrySettings(),
  81. }
  82. cl, err := p.BuildClient()
  83. require.NoError(t, err)
  84. resp, err := cl.(*clientImpl).Get(" ")
  85. require.Error(t, err)
  86. require.Nil(t, resp)
  87. }
  88. func TestFailedRT(t *testing.T) {
  89. tr := &fakeRoundTripper{failOnRT: true}
  90. endpoint, _ := url.Parse("http://localhost:8080")
  91. client := &clientImpl{
  92. baseURL: *endpoint,
  93. httpClient: http.Client{Transport: tr},
  94. }
  95. _, err := client.Get("/test")
  96. require.Error(t, err)
  97. }
  98. func TestErrOnRead(t *testing.T) {
  99. tr := &fakeRoundTripper{errOnRead: true}
  100. endpoint, _ := url.Parse("http://localhost:8080")
  101. client := &clientImpl{
  102. baseURL: *endpoint,
  103. httpClient: http.Client{Transport: tr},
  104. settings: componenttest.NewNopTelemetrySettings(),
  105. }
  106. resp, err := client.Get("/foo")
  107. require.Error(t, err)
  108. require.Nil(t, resp)
  109. }
  110. func TestErrCode(t *testing.T) {
  111. tr := &fakeRoundTripper{errCode: true}
  112. endpoint, _ := url.Parse("http://localhost:8080")
  113. client := &clientImpl{
  114. baseURL: *endpoint,
  115. httpClient: http.Client{Transport: tr},
  116. settings: componenttest.NewNopTelemetrySettings(),
  117. }
  118. resp, err := client.Get("/foo")
  119. require.Error(t, err)
  120. require.Nil(t, resp)
  121. }
  122. var _ http.RoundTripper = (*fakeRoundTripper)(nil)
  123. type fakeRoundTripper struct {
  124. closed bool
  125. header http.Header
  126. method string
  127. url string
  128. failOnRT bool
  129. errOnClose bool
  130. errOnRead bool
  131. errCode bool
  132. }
  133. func (f *fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  134. if f.failOnRT {
  135. return nil, fmt.Errorf("failOnRT == true")
  136. }
  137. f.header = req.Header
  138. f.method = req.Method
  139. f.url = req.URL.String()
  140. var reader io.Reader
  141. if f.errOnRead {
  142. reader = &failingReader{}
  143. } else {
  144. reader = strings.NewReader("hello")
  145. }
  146. statusCode := 200
  147. if f.errCode {
  148. statusCode = 503
  149. }
  150. return &http.Response{
  151. StatusCode: statusCode,
  152. Body: &fakeReadCloser{
  153. Reader: reader,
  154. onClose: func() error {
  155. f.closed = true
  156. if f.errOnClose {
  157. return fmt.Errorf("error on close")
  158. }
  159. return nil
  160. },
  161. },
  162. }, nil
  163. }
  164. var _ io.Reader = (*failingReader)(nil)
  165. type failingReader struct{}
  166. func (f *failingReader) Read([]byte) (n int, err error) {
  167. return 0, fmt.Errorf("error on read")
  168. }
  169. var _ io.ReadCloser = (*fakeReadCloser)(nil)
  170. type fakeReadCloser struct {
  171. io.Reader
  172. onClose func() error
  173. }
  174. func (f *fakeReadCloser) Close() error {
  175. return f.onClose()
  176. }