| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package extensionmiddleware |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"net/http" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
"google.golang.org/grpc" |
| 13 |
) |
| 14 |
|
| 15 |
func TestGetHTTPRoundTripperFunc(t *testing.T) { |
| 16 |
// Create a base round tripper for testing |
| 17 |
baseRT := http.DefaultTransport |
| 18 |
|
| 19 |
t.Run("nil function", func(t *testing.T) { |
| 20 |
var nilFunc GetHTTPRoundTripperFunc |
| 21 |
rt, err := nilFunc.GetHTTPRoundTripper(baseRT) |
| 22 |
require.NoError(t, err) |
| 23 |
require.Equal(t, baseRT, rt) |
| 24 |
}) |
| 25 |
|
| 26 |
t.Run("identity function", func(t *testing.T) { |
| 27 |
identityFunc := GetHTTPRoundTripperFunc(func(base http.RoundTripper) (http.RoundTripper, error) { |
| 28 |
return base, nil |
| 29 |
}) |
| 30 |
rt, err := identityFunc.GetHTTPRoundTripper(baseRT) |
| 31 |
require.NoError(t, err) |
| 32 |
require.Equal(t, baseRT, rt) |
| 33 |
}) |
| 34 |
|
| 35 |
t.Run("error function", func(t *testing.T) { |
| 36 |
expectedErr := errors.New("round tripper error") |
| 37 |
errorFunc := GetHTTPRoundTripperFunc(func(_ http.RoundTripper) (http.RoundTripper, error) { |
| 38 |
return nil, expectedErr |
| 39 |
}) |
| 40 |
|
| 41 |
rt, err := errorFunc.GetHTTPRoundTripper(baseRT) |
| 42 |
require.Error(t, err) |
| 43 |
require.Equal(t, expectedErr, err) |
| 44 |
require.Nil(t, rt) |
| 45 |
}) |
| 46 |
} |
| 47 |
|
| 48 |
func TestGetGRPCClientOptionsFunc(t *testing.T) { |
| 49 |
t.Run("nil function", func(t *testing.T) { |
| 50 |
var nilFunc GetGRPCClientOptionsFunc |
| 51 |
options, err := nilFunc.GetGRPCClientOptions() |
| 52 |
require.NoError(t, err) |
| 53 |
require.Nil(t, options) |
| 54 |
}) |
| 55 |
|
| 56 |
t.Run("empty options function", func(t *testing.T) { |
| 57 |
emptyFunc := GetGRPCClientOptionsFunc(func() ([]grpc.DialOption, error) { |
| 58 |
return []grpc.DialOption{}, nil |
| 59 |
}) |
| 60 |
|
| 61 |
options, err := emptyFunc.GetGRPCClientOptions() |
| 62 |
require.NoError(t, err) |
| 63 |
require.Empty(t, options) |
| 64 |
}) |
| 65 |
|
| 66 |
t.Run("options function", func(t *testing.T) { |
| 67 |
// Create some test dial options |
| 68 |
dialOpt1 := grpc.WithAuthority("test-authority") |
| 69 |
dialOpt2 := grpc.WithDisableRetry() |
| 70 |
|
| 71 |
optionsFunc := GetGRPCClientOptionsFunc(func() ([]grpc.DialOption, error) { |
| 72 |
return []grpc.DialOption{dialOpt1, dialOpt2}, nil |
| 73 |
}) |
| 74 |
|
| 75 |
options, err := optionsFunc.GetGRPCClientOptions() |
| 76 |
require.NoError(t, err) |
| 77 |
require.Len(t, options, 2) |
| 78 |
}) |
| 79 |
|
| 80 |
t.Run("error function", func(t *testing.T) { |
| 81 |
expectedErr := errors.New("grpc options error") |
| 82 |
errorFunc := GetGRPCClientOptionsFunc(func() ([]grpc.DialOption, error) { |
| 83 |
return nil, expectedErr |
| 84 |
}) |
| 85 |
|
| 86 |
options, err := errorFunc.GetGRPCClientOptions() |
| 87 |
require.Error(t, err) |
| 88 |
require.Equal(t, expectedErr, err) |
| 89 |
require.Nil(t, options) |
| 90 |
}) |
| 91 |
} |