|
| 1 | +package shared |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +const testEndpoint = "https://test.endpoint" |
| 11 | + |
| 12 | +func TestNewConfigurationFromOptions(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + clientOptions ClientOptions |
| 16 | + expectedConfig *Configuration |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "ValidOptions", |
| 20 | + clientOptions: ClientOptions{ |
| 21 | + Endpoint: testEndpoint, |
| 22 | + SkipTLSVerify: true, |
| 23 | + Certificate: "", |
| 24 | + Credentials: Credentials{ |
| 25 | + Username: "testUser", |
| 26 | + Password: "testPass", |
| 27 | + Token: "testToken", |
| 28 | + }, |
| 29 | + }, |
| 30 | + expectedConfig: &Configuration{ |
| 31 | + Username: "testUser", |
| 32 | + Password: "testPass", |
| 33 | + Token: "testToken", |
| 34 | + Servers: ServerConfigurations{ |
| 35 | + { |
| 36 | + URL: testEndpoint, |
| 37 | + Description: "Production", |
| 38 | + }, |
| 39 | + }, |
| 40 | + HTTPClient: &http.Client{ |
| 41 | + Transport: &http.Transport{ |
| 42 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "EmptyEndpoint", |
| 49 | + clientOptions: ClientOptions{ |
| 50 | + SkipTLSVerify: true, |
| 51 | + Certificate: "", |
| 52 | + Credentials: Credentials{ |
| 53 | + Username: "testUser", |
| 54 | + Password: "testPass", |
| 55 | + Token: "testToken", |
| 56 | + }, |
| 57 | + }, |
| 58 | + expectedConfig: &Configuration{ |
| 59 | + Username: "testUser", |
| 60 | + Password: "testPass", |
| 61 | + Token: "testToken", |
| 62 | + Servers: ServerConfigurations{}, |
| 63 | + HTTPClient: &http.Client{ |
| 64 | + Transport: &http.Transport{ |
| 65 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "NoCredentials", |
| 72 | + clientOptions: ClientOptions{ |
| 73 | + Endpoint: testEndpoint, |
| 74 | + SkipTLSVerify: true, |
| 75 | + Certificate: "", |
| 76 | + }, |
| 77 | + expectedConfig: &Configuration{ |
| 78 | + Username: "", |
| 79 | + Password: "", |
| 80 | + Token: "", |
| 81 | + Servers: ServerConfigurations{ |
| 82 | + { |
| 83 | + URL: testEndpoint, |
| 84 | + Description: "Production", |
| 85 | + }, |
| 86 | + }, |
| 87 | + HTTPClient: &http.Client{ |
| 88 | + Transport: &http.Transport{ |
| 89 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "AddCertificate", |
| 96 | + clientOptions: ClientOptions{ |
| 97 | + Endpoint: testEndpoint, |
| 98 | + SkipTLSVerify: true, |
| 99 | + Certificate: "testCertData", |
| 100 | + Credentials: Credentials{ |
| 101 | + Username: "testUser", |
| 102 | + Password: "testPass", |
| 103 | + Token: "testToken", |
| 104 | + }, |
| 105 | + }, |
| 106 | + expectedConfig: &Configuration{ |
| 107 | + Username: "testUser", |
| 108 | + Password: "testPass", |
| 109 | + Token: "testToken", |
| 110 | + Servers: ServerConfigurations{ |
| 111 | + { |
| 112 | + URL: testEndpoint, |
| 113 | + Description: "Production", |
| 114 | + }, |
| 115 | + }, |
| 116 | + HTTPClient: &http.Client{ |
| 117 | + Transport: &http.Transport{ |
| 118 | + TLSClientConfig: &tls.Config{ |
| 119 | + InsecureSkipVerify: true, |
| 120 | + RootCAs: AddCertsToClient("testCertData"), |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tt := range tests { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + config := NewConfigurationFromOptions(tt.clientOptions) |
| 131 | + assert.Equal(t, tt.expectedConfig.Username, config.Username) |
| 132 | + assert.Equal(t, tt.expectedConfig.Password, config.Password) |
| 133 | + assert.Equal(t, tt.expectedConfig.Token, config.Token) |
| 134 | + assert.Equal(t, tt.expectedConfig.Servers, config.Servers) |
| 135 | + assert.NotNil(t, config.HTTPClient) |
| 136 | + assert.Equal(t, tt.expectedConfig.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, |
| 137 | + config.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify) |
| 138 | + assert.True(t, config.HTTPClient.Transport.(*http.Transport).TLSClientConfig.RootCAs.Equal(tt.expectedConfig.HTTPClient.Transport.(*http.Transport).TLSClientConfig.RootCAs)) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments