Skip to content

Commit dd9d2f7

Browse files
authored
Merge pull request #128 from deploymenttheory/dev
Added test coverage for refactored http client
2 parents 9adf1a0 + 4ecfce1 commit dd9d2f7

18 files changed

+305
-454
lines changed

.github/workflows/test.yml

+1-9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,4 @@ jobs:
2323
uses: actions/checkout@v4
2424

2525
- name: Run tests
26-
run: go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./...
27-
28-
- name: Upload Coverage
29-
uses: codecov/codecov-action@v4
30-
continue-on-error: true
31-
with:
32-
token: ${{secrets.CODECOV_TOKEN}}
33-
file: ./coverage.txt
34-
fail_ci_if_error: false
26+
run: go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./...
File renamed without changes.

httpclient/auth_method_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package httpclient
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestDetermineAuthMethod(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
authConfig AuthConfig
13+
expectedAuth string
14+
expectError bool
15+
}{
16+
{
17+
name: "Valid OAuth credentials",
18+
authConfig: AuthConfig{
19+
ClientID: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID format
20+
ClientSecret: "validSecretWith16Chars", // Ensure it's at least 16 characters
21+
},
22+
expectedAuth: "oauth",
23+
expectError: false,
24+
},
25+
{
26+
name: "Valid Bearer credentials",
27+
authConfig: AuthConfig{
28+
Username: "validUsername",
29+
Password: "validPassword",
30+
},
31+
expectedAuth: "bearer",
32+
expectError: false,
33+
},
34+
{
35+
name: "Invalid OAuth credentials",
36+
authConfig: AuthConfig{
37+
ClientID: "invalidClientID",
38+
ClientSecret: "invalidClientSecret",
39+
},
40+
expectedAuth: "unknown",
41+
expectError: true,
42+
},
43+
{
44+
name: "Invalid Bearer credentials",
45+
authConfig: AuthConfig{
46+
Username: "invalidUser",
47+
Password: "short",
48+
},
49+
expectedAuth: "unknown",
50+
expectError: true,
51+
},
52+
{
53+
name: "Missing credentials",
54+
authConfig: AuthConfig{},
55+
expectedAuth: "unknown",
56+
expectError: true,
57+
},
58+
}
59+
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
authMethod, err := DetermineAuthMethod(tt.authConfig)
63+
64+
if tt.expectError {
65+
assert.Error(t, err)
66+
} else {
67+
assert.NoError(t, err)
68+
}
69+
assert.Equal(t, tt.expectedAuth, authMethod)
70+
})
71+
}
72+
}

httpclient/httpclient_client.go renamed to httpclient/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// http_client.go
1+
// httpclient/client.go
22
/* The `http_client` package provides a configurable HTTP client tailored for interacting with specific APIs.
33
It supports different authentication methods, including "bearer" and "oauth". The client is designed with a
44
focus on concurrency management, structured error handling, and flexible configuration options.

httpclient/httpclient_client_configuration.go renamed to httpclient/client_configuration.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// httpclient/client_configuration.go
2+
// Description: This file contains functions to load and validate configuration values from a JSON file or environment variables.
13
package httpclient
24

35
import (
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)