|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAPIKeyCredentials_TLSEnabledByDefaultWhenAPIKeyProvided(t *testing.T) { |
| 11 | + creds := NewAPIKeyStaticCredentials("test-api-key") |
| 12 | + opts := &ConnectionOptions{} |
| 13 | + |
| 14 | + err := creds.applyToOptions(opts) |
| 15 | + require.NoError(t, err) |
| 16 | + |
| 17 | + // TLS should be auto-enabled when api_key is provided and tls not explicitly set |
| 18 | + require.NotNil(t, opts.TLS) |
| 19 | +} |
| 20 | + |
| 21 | +func TestAPIKeyCredentials_TLSCanBeExplicitlyDisabledWithAPIKey(t *testing.T) { |
| 22 | + // Test that TLS can be explicitly disabled using TLSDisabled field |
| 23 | + creds := NewAPIKeyStaticCredentials("test-api-key") |
| 24 | + opts := &ConnectionOptions{ |
| 25 | + TLSDisabled: true, |
| 26 | + } |
| 27 | + |
| 28 | + err := creds.applyToOptions(opts) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + // TLS should remain nil when TLSDisabled is true |
| 32 | + require.Nil(t, opts.TLS) |
| 33 | +} |
| 34 | + |
| 35 | +func TestConnectionOptions_TLSDisabledByDefaultWithoutAPIKey(t *testing.T) { |
| 36 | + // Test that TLS is disabled by default when no API key is provided |
| 37 | + opts := &ConnectionOptions{} |
| 38 | + |
| 39 | + // Without API key credentials, TLS should remain nil |
| 40 | + require.Nil(t, opts.TLS) |
| 41 | +} |
| 42 | + |
| 43 | +func TestAPIKeyCredentials_ExplicitTLSConfigPreservedWithAPIKey(t *testing.T) { |
| 44 | + // Test that explicit TLS configuration is preserved when API key is provided |
| 45 | + tlsConfig := &tls.Config{ |
| 46 | + ServerName: "test-domain", |
| 47 | + MinVersion: tls.VersionTLS12, |
| 48 | + } |
| 49 | + |
| 50 | + creds := NewAPIKeyStaticCredentials("test-api-key") |
| 51 | + opts := &ConnectionOptions{ |
| 52 | + TLS: tlsConfig, |
| 53 | + } |
| 54 | + |
| 55 | + err := creds.applyToOptions(opts) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + // Explicit TLS config should be preserved |
| 59 | + require.NotNil(t, opts.TLS) |
| 60 | + require.Equal(t, "test-domain", opts.TLS.ServerName) |
| 61 | + require.Equal(t, uint16(tls.VersionTLS12), opts.TLS.MinVersion) |
| 62 | +} |
0 commit comments