rest_client_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ecsutil
  4. import (
  5. "fmt"
  6. "net/url"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component/componenttest"
  10. "go.opentelemetry.io/collector/config/confighttp"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/ecsutil/endpoints"
  12. )
  13. type fakeClient struct{}
  14. func (f *fakeClient) Get(path string) ([]byte, error) {
  15. return []byte(path), nil
  16. }
  17. func TestRestClient(t *testing.T) {
  18. u, _ := url.Parse("http://www.test.com")
  19. rest, err := NewRestClient(*u, confighttp.HTTPClientSettings{}, componenttest.NewNopTelemetrySettings())
  20. require.NoError(t, err)
  21. require.NotNil(t, rest)
  22. }
  23. func TestRestClientFromClient(t *testing.T) {
  24. rest := NewRestClientFromClient(&fakeClient{})
  25. metadata, err := rest.GetResponse(endpoints.TaskMetadataPath)
  26. require.Nil(t, err)
  27. require.Equal(t, endpoints.TaskMetadataPath, string(metadata))
  28. }
  29. type fakeErrorClient struct{}
  30. func (f *fakeErrorClient) Get(_ string) ([]byte, error) {
  31. return nil, fmt.Errorf("")
  32. }
  33. func TestRestClientError(t *testing.T) {
  34. rest := NewRestClientFromClient(&fakeErrorClient{})
  35. metadata, err := rest.GetResponse(endpoints.TaskMetadataPath)
  36. require.Error(t, err)
  37. require.Nil(t, metadata)
  38. }
  39. type fakeMetadataErrorClient struct{}
  40. func (f *fakeMetadataErrorClient) Get(_ string) ([]byte, error) {
  41. return nil, fmt.Errorf("")
  42. }
  43. func TestRestClientMetadataError(t *testing.T) {
  44. rest := NewRestClientFromClient(&fakeMetadataErrorClient{})
  45. metadata, err := rest.GetResponse(endpoints.TaskMetadataPath)
  46. require.Error(t, err)
  47. require.Nil(t, metadata)
  48. }