Skip to content

Commit

Permalink
OPENAPI: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobm-splunk authored and daveshanley committed Jul 23, 2024
1 parent dcf224c commit 0d7463d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
15 changes: 10 additions & 5 deletions config/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const (
PascalCaseRewriteIdHeader = "Rewriteid"
SnakeCaseRewriteIdHeader = "rewrite_id"
SnakeCaseRewriteIdHeader = "Rewrite_id"
KebabCaseRewriteIdHeader = "Rewrite-Id"
)

Expand Down Expand Up @@ -119,12 +119,17 @@ func getRewriteIdHeaderValues(req *http.Request) ([]string, bool) {

// Let's now try to ignore case ; this may produce collisions if a user has two headers with similar keys,
// but different capitalization. This is okay, as this is a last ditch effort to find any possible match
var loweredHeaders = make(http.Header)
loweredHeaders := map[string][]string{}

for headerKey, headerValues := range req.Header {
for _, headerValue := range headerValues {
loweredHeaders.Set(strings.ToLower(headerKey), headerValue)
loweredKey := strings.ToLower(headerKey)

if _, ok := loweredHeaders[loweredKey]; ok {
loweredHeaders[loweredKey] = append(loweredHeaders[loweredKey], headerValues...)
} else {
loweredHeaders[loweredKey] = headerValues
}

}

for _, possibleHeaderKey := range rewriteIdHeaders {
Expand All @@ -135,7 +140,7 @@ func getRewriteIdHeaderValues(req *http.Request) ([]string, bool) {

}

return nil, false
return []string{}, false
}

func FindPathWithRewriteId(paths []*shared.WiretapPathConfig, req *http.Request) *shared.WiretapPathConfig {
Expand Down
93 changes: 93 additions & 0 deletions config/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,96 @@ func TestValidationAllowList_NoPathsRegistered(t *testing.T) {
assert.False(t, ignore)

}

func TestGetRewriteHeaderValues(t *testing.T) {

expectedValue := []string{"ExpectedValue"}

requestList := []*http.Request{
{
Header: http.Header{
"Rewriteid": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"Rewrite-Id": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"Rewrite_id": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"RewriteId": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"RewrIte-Id": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"rewriteid": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"rewrite-id": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{
"rewrite_id": expectedValue,
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
}

for _, request := range requestList {
actualValue, found := getRewriteIdHeaderValues(request)
assert.Equal(t, expectedValue, actualValue)
assert.True(t, found)
}

}

func TestGetRewriteHeaderValues_MissingHeader(t *testing.T) {

requestList := []*http.Request{
{
Header: http.Header{
"Other-Header": []string{"another header"},
"other-header": []string{"another another header"},
},
},
{
Header: http.Header{},
},
}

for _, request := range requestList {
actualValue, found := getRewriteIdHeaderValues(request)
assert.Equal(t, []string{}, actualValue)
assert.False(t, found)
}

}

0 comments on commit 0d7463d

Please sign in to comment.