Skip to content

Commit e526808

Browse files
authored
ingestors/golang: fix GetKeyValuePairValue to return correct value (#555)
Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 935c0fd commit e526808

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

ingestors/golang/metlo/block.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func GetXForwardedForFromValue(value *string) *string {
125125

126126
func GetSourceIp(reqHeaders []NV, traceMeta TraceMeta) *string {
127127
for _, header := range reqHeaders {
128-
if strings.ToLower(header.Name) == "x-forwarded-for" {
128+
if strings.EqualFold(header.Name, "x-forwarded-for") {
129129
xForwardedValue := GetXForwardedForFromValue(&header.Value)
130130
return xForwardedValue
131131
}
@@ -160,8 +160,8 @@ func CheckStringCondition(condOperator *string, condValue *string, reqValue *str
160160

161161
func GetKeyValuePairValue(key string, keyValuePairs []NV) *string {
162162
for _, pair := range keyValuePairs {
163-
if strings.ToLower(pair.Name) == key {
164-
return &pair.Name
163+
if strings.EqualFold(pair.Name, key) {
164+
return &pair.Value
165165
}
166166
}
167167
return nil
@@ -222,7 +222,7 @@ func CheckKeyValuePair(condOperator *string, condKey *string, condValue *string,
222222
return false
223223
}
224224
for _, pair := range keyValuePairs {
225-
if strings.ToLower(pair.Name) == strings.ToLower(*condKey) && CheckStringCondition(condOperator, condValue, &pair.Value) {
225+
if strings.EqualFold(pair.Name, *condKey) && CheckStringCondition(condOperator, condValue, &pair.Value) {
226226
return true
227227
}
228228
}
@@ -424,7 +424,7 @@ func HandleSessionIdentifier(authentication *Authentication, headers []NV, key *
424424
return
425425
}
426426
headerKey := *authentication.HeaderKey
427-
headerValuePtr := GetKeyValuePairValue(strings.ToLower(headerKey), headers)
427+
headerValuePtr := GetKeyValuePairValue(headerKey, headers)
428428
if headerValuePtr != nil {
429429
key.WriteRune('_')
430430
key.WriteString(*headerValuePtr)

ingestors/golang/metlo/block_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package metlo
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetKeyValuePairValue(t *testing.T) {
8+
keyValuePairs := []NV{
9+
{Name: "key1", Value: "value1"},
10+
{Name: "key2", Value: "value2"},
11+
{Name: "key3", Value: "value3"},
12+
}
13+
14+
t.Run("Existing key, case insensitive", func(t *testing.T) {
15+
key := "KEY1"
16+
expectedValue := "value1"
17+
18+
result := GetKeyValuePairValue(key, keyValuePairs)
19+
20+
if result == nil {
21+
t.Errorf("Expected value for key '%s' to be '%s', but got nil", key, expectedValue)
22+
} else if *result != expectedValue {
23+
t.Errorf("Expected value for key '%s' to be '%s', but got '%s'", key, expectedValue, *result)
24+
}
25+
})
26+
27+
t.Run("Existing key, exact match", func(t *testing.T) {
28+
key := "key3"
29+
expectedValue := "value3"
30+
31+
result := GetKeyValuePairValue(key, keyValuePairs)
32+
33+
if result == nil {
34+
t.Errorf("Expected value for key '%s' to be '%s', but got nil", key, expectedValue)
35+
} else if *result != expectedValue {
36+
t.Errorf("Expected value for key '%s' to be '%s', but got '%s'", key, expectedValue, *result)
37+
}
38+
})
39+
40+
t.Run("Non-existing key", func(t *testing.T) {
41+
key := "nonexistent"
42+
result := GetKeyValuePairValue(key, keyValuePairs)
43+
44+
if result != nil {
45+
t.Errorf("Expected value for non-existing key '%s' to be nil, but got '%s'", key, *result)
46+
}
47+
})
48+
}

0 commit comments

Comments
 (0)