|
| 1 | +package httpclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestLoadConfigFromFile(t *testing.T) { |
| 12 | + // Create a temporary file |
| 13 | + tmpFile, err := os.CreateTemp("", "config-*.json") |
| 14 | + assert.NoError(t, err) |
| 15 | + defer os.Remove(tmpFile.Name()) // Clean up the file after test |
| 16 | + |
| 17 | + // Write updated JSON configuration to the temp file |
| 18 | + configJSON := `{ |
| 19 | + "Auth": { |
| 20 | + "ClientID": "787xxxxd-98bb-xxxx-8d17-xxx0f8cbfb7b", |
| 21 | + "ClientSecret": "xxxxxxxxxxxxx" |
| 22 | + }, |
| 23 | + "Environment": { |
| 24 | + "InstanceName": "lbgsandbox", |
| 25 | + "OverrideBaseDomain": "", |
| 26 | + "APIType": "jamfpro" |
| 27 | + }, |
| 28 | + "ClientOptions": { |
| 29 | + "LogLevel": "LogLevelDebug", |
| 30 | + "LogOutputFormat": "console", |
| 31 | + "LogConsoleSeparator": " ", |
| 32 | + "HideSensitiveData": true, |
| 33 | + "EnableDynamicRateLimiting": true, |
| 34 | + "MaxRetryAttempts": 5, |
| 35 | + "MaxConcurrentRequests": 3, |
| 36 | + "EnableCookieJar": true, |
| 37 | + "FollowRedirects": true, |
| 38 | + "MaxRedirects": 5 |
| 39 | + } |
| 40 | + }` |
| 41 | + _, err = tmpFile.WriteString(configJSON) |
| 42 | + assert.NoError(t, err) |
| 43 | + assert.NoError(t, tmpFile.Close()) |
| 44 | + |
| 45 | + // Test loading from the temp file |
| 46 | + config, err := LoadConfigFromFile(tmpFile.Name()) |
| 47 | + assert.NoError(t, err) |
| 48 | + assert.Equal(t, "787xxxxd-98bb-xxxx-8d17-xxx0f8cbfb7b", config.Auth.ClientID) |
| 49 | + assert.Equal(t, "xxxxxxxxxxxxx", config.Auth.ClientSecret) |
| 50 | + assert.Equal(t, "lbgsandbox", config.Environment.InstanceName) |
| 51 | + assert.Equal(t, "jamfpro", config.Environment.APIType) |
| 52 | + assert.Equal(t, "LogLevelDebug", config.ClientOptions.LogLevel) |
| 53 | + assert.Equal(t, "console", config.ClientOptions.LogOutputFormat) |
| 54 | + assert.Equal(t, " ", config.ClientOptions.LogConsoleSeparator) |
| 55 | + assert.True(t, config.ClientOptions.HideSensitiveData) |
| 56 | + assert.True(t, config.ClientOptions.EnableDynamicRateLimiting) |
| 57 | + assert.Equal(t, 5, config.ClientOptions.MaxRetryAttempts) |
| 58 | + assert.Equal(t, 3, config.ClientOptions.MaxConcurrentRequests) |
| 59 | + assert.True(t, config.ClientOptions.EnableCookieJar) |
| 60 | + assert.True(t, config.ClientOptions.FollowRedirects) |
| 61 | + assert.Equal(t, 5, config.ClientOptions.MaxRedirects) |
| 62 | +} |
| 63 | + |
| 64 | +func TestGetEnvOrDefault(t *testing.T) { |
| 65 | + const envKey = "TEST_ENV_VAR" |
| 66 | + defer os.Unsetenv(envKey) |
| 67 | + |
| 68 | + // Scenario 1: Environment variable is set |
| 69 | + expectedValue := "test_value" |
| 70 | + os.Setenv(envKey, expectedValue) |
| 71 | + assert.Equal(t, expectedValue, getEnvOrDefault(envKey, "default_value")) |
| 72 | + |
| 73 | + // Scenario 2: Environment variable is not set |
| 74 | + assert.Equal(t, "default_value", getEnvOrDefault("NON_EXISTENT_ENV_VAR", "default_value")) |
| 75 | +} |
| 76 | + |
| 77 | +func TestParseBool(t *testing.T) { |
| 78 | + assert.True(t, parseBool("true")) |
| 79 | + assert.False(t, parseBool("false")) |
| 80 | + assert.False(t, parseBool("invalid_value")) |
| 81 | +} |
| 82 | + |
| 83 | +func TestParseInt(t *testing.T) { |
| 84 | + assert.Equal(t, 42, parseInt("42", 10)) |
| 85 | + assert.Equal(t, 10, parseInt("invalid_value", 10)) |
| 86 | +} |
| 87 | + |
| 88 | +func TestParseDuration(t *testing.T) { |
| 89 | + assert.Equal(t, 5*time.Minute, parseDuration("5m", 1*time.Minute)) |
| 90 | + assert.Equal(t, 1*time.Minute, parseDuration("invalid_value", 1*time.Minute)) |
| 91 | +} |
| 92 | + |
| 93 | +func TestSetLoggerDefaultValues(t *testing.T) { |
| 94 | + config := &ClientConfig{ClientOptions: ClientOptions{}} |
| 95 | + setLoggerDefaultValues(config) |
| 96 | + assert.Equal(t, ",", config.ClientOptions.LogConsoleSeparator) |
| 97 | +} |
0 commit comments