Skip to content

Commit b84499d

Browse files
authored
Merge pull request #118 from deploymenttheory/dev
relocated header helper fuction
2 parents f80b1f9 + b411022 commit b84499d

6 files changed

+49
-48
lines changed

apihandlers/msgraph/msgraph_api_headers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (g *GraphAPIHandler) GetAPIRequestHeaders(endpoint string) map[string]strin
6464
"Accept": g.GetAcceptHeader(), // Dynamically set based on API requirements.
6565
"Content-Type": g.GetContentTypeHeader(endpoint, g.Logger), // Dynamically set based on the endpoint.
6666
"Authorization": "", // To be set by the client with the actual token.
67-
"User-Agent": "go-api-http-client-graph-handler", // To be set by the client, usually with application info.
67+
"User-Agent": "go-api-http-client-msgraph-handler", // To be set by the client, usually with application info.
6868
}
6969
return headers
7070
}

httpclient/httpclient_client.go

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func BuildClient(config ClientConfig) (*Client, error) {
126126
log.Error("Failed to set up redirect handler", zap.Error(err))
127127
return nil, err
128128
}
129+
129130
// Create a new HTTP client with the provided configuration.
130131
client := &Client{
131132
APIHandler: apiHandler,

httpclient/httpclient_headers.go

+16
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,19 @@ func CheckDeprecationHeader(resp *http.Response, log logger.Logger) {
155155
)
156156
}
157157
}
158+
159+
// RedactSensitiveHeaderData redacts sensitive data if the HideSensitiveData flag is set to true.
160+
func RedactSensitiveHeaderData(client *Client, key string, value string) string {
161+
if client.clientConfig.ClientOptions.HideSensitiveData {
162+
// Define sensitive data keys that should be redacted.
163+
sensitiveKeys := map[string]bool{
164+
"AccessToken": true,
165+
"Authorization": true,
166+
}
167+
168+
if _, found := sensitiveKeys[key]; found {
169+
return "REDACTED"
170+
}
171+
}
172+
return value
173+
}

httpclient/httpclient_headers_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,34 @@ func TestSetCustomHeader(t *testing.T) {
142142
SetCustomHeader(req, "X-Custom-Header", "CustomValue")
143143
assert.Equal(t, "CustomValue", req.Header.Get("X-Custom-Header"))
144144
}
145+
146+
// TestRedactSensitiveData tests the RedactSensitiveData function with various scenarios
147+
func TestRedactSensitiveData(t *testing.T) {
148+
tests := []struct {
149+
name string
150+
hideSensitive bool
151+
key string
152+
value string
153+
expectedOutcome string
154+
}{
155+
{"RedactSensitiveKey", true, "AccessToken", "secret-token", "REDACTED"},
156+
{"RedactSensitiveKeyAuthorization", true, "Authorization", "Bearer secret-token", "REDACTED"},
157+
{"DoNotRedactNonSensitiveKey", true, "NonSensitiveKey", "non-sensitive-value", "non-sensitive-value"},
158+
{"DoNotRedactWhenDisabled", false, "AccessToken", "secret-token", "secret-token"},
159+
}
160+
161+
for _, tt := range tests {
162+
t.Run(tt.name, func(t *testing.T) {
163+
client := &Client{
164+
clientConfig: ClientConfig{
165+
ClientOptions: ClientOptions{
166+
HideSensitiveData: tt.hideSensitive,
167+
},
168+
},
169+
}
170+
171+
result := RedactSensitiveHeaderData(client, tt.key, tt.value)
172+
assert.Equal(t, tt.expectedOutcome, result, "Redaction outcome should match expected")
173+
})
174+
}
175+
}

httpclient/httpclient_helpers.go

-16
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,3 @@ import (
99
func ParseISO8601Date(dateStr string) (time.Time, error) {
1010
return time.Parse(time.RFC3339, dateStr)
1111
}
12-
13-
// RedactSensitiveHeaderData redacts sensitive data if the HideSensitiveData flag is set to true.
14-
func RedactSensitiveHeaderData(client *Client, key string, value string) string {
15-
if client.clientConfig.ClientOptions.HideSensitiveData {
16-
// Define sensitive data keys that should be redacted.
17-
sensitiveKeys := map[string]bool{
18-
"AccessToken": true,
19-
"Authorization": true,
20-
}
21-
22-
if _, found := sensitiveKeys[key]; found {
23-
return "REDACTED"
24-
}
25-
}
26-
return value
27-
}

httpclient/httpclient_helpers_test.go

-31
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,3 @@ func TestParseISO8601Date(t *testing.T) {
4444
})
4545
}
4646
}
47-
48-
// TestRedactSensitiveData tests the RedactSensitiveData function with various scenarios
49-
func TestRedactSensitiveData(t *testing.T) {
50-
tests := []struct {
51-
name string
52-
hideSensitive bool
53-
key string
54-
value string
55-
expectedOutcome string
56-
}{
57-
{"RedactSensitiveKey", true, "AccessToken", "secret-token", "REDACTED"},
58-
{"RedactSensitiveKeyAuthorization", true, "Authorization", "Bearer secret-token", "REDACTED"},
59-
{"DoNotRedactNonSensitiveKey", true, "NonSensitiveKey", "non-sensitive-value", "non-sensitive-value"},
60-
{"DoNotRedactWhenDisabled", false, "AccessToken", "secret-token", "secret-token"},
61-
}
62-
63-
for _, tt := range tests {
64-
t.Run(tt.name, func(t *testing.T) {
65-
client := &Client{
66-
clientConfig: ClientConfig{
67-
ClientOptions: ClientOptions{
68-
HideSensitiveData: tt.hideSensitive,
69-
},
70-
},
71-
}
72-
73-
result := RedactSensitiveHeaderData(client, tt.key, tt.value)
74-
assert.Equal(t, tt.expectedOutcome, result, "Redaction outcome should match expected")
75-
})
76-
}
77-
}

0 commit comments

Comments
 (0)