Skip to content

Commit 0d7463d

Browse files
jacobm-splunkdaveshanley
authored andcommitted
OPENAPI: added tests
1 parent dcf224c commit 0d7463d

File tree

2 files changed

+103
-5
lines changed

2 files changed

+103
-5
lines changed

config/paths.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
const (
1515
PascalCaseRewriteIdHeader = "Rewriteid"
16-
SnakeCaseRewriteIdHeader = "rewrite_id"
16+
SnakeCaseRewriteIdHeader = "Rewrite_id"
1717
KebabCaseRewriteIdHeader = "Rewrite-Id"
1818
)
1919

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

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

124124
for headerKey, headerValues := range req.Header {
125-
for _, headerValue := range headerValues {
126-
loweredHeaders.Set(strings.ToLower(headerKey), headerValue)
125+
loweredKey := strings.ToLower(headerKey)
126+
127+
if _, ok := loweredHeaders[loweredKey]; ok {
128+
loweredHeaders[loweredKey] = append(loweredHeaders[loweredKey], headerValues...)
129+
} else {
130+
loweredHeaders[loweredKey] = headerValues
127131
}
132+
128133
}
129134

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

136141
}
137142

138-
return nil, false
143+
return []string{}, false
139144
}
140145

141146
func FindPathWithRewriteId(paths []*shared.WiretapPathConfig, req *http.Request) *shared.WiretapPathConfig {

config/paths_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,96 @@ func TestValidationAllowList_NoPathsRegistered(t *testing.T) {
494494
assert.False(t, ignore)
495495

496496
}
497+
498+
func TestGetRewriteHeaderValues(t *testing.T) {
499+
500+
expectedValue := []string{"ExpectedValue"}
501+
502+
requestList := []*http.Request{
503+
{
504+
Header: http.Header{
505+
"Rewriteid": expectedValue,
506+
"Other-Header": []string{"another header"},
507+
"other-header": []string{"another another header"},
508+
},
509+
},
510+
{
511+
Header: http.Header{
512+
"Rewrite-Id": expectedValue,
513+
"Other-Header": []string{"another header"},
514+
"other-header": []string{"another another header"},
515+
},
516+
},
517+
{
518+
Header: http.Header{
519+
"Rewrite_id": expectedValue,
520+
"Other-Header": []string{"another header"},
521+
"other-header": []string{"another another header"},
522+
},
523+
},
524+
{
525+
Header: http.Header{
526+
"RewriteId": expectedValue,
527+
"Other-Header": []string{"another header"},
528+
"other-header": []string{"another another header"},
529+
},
530+
},
531+
{
532+
Header: http.Header{
533+
"RewrIte-Id": expectedValue,
534+
"Other-Header": []string{"another header"},
535+
"other-header": []string{"another another header"},
536+
},
537+
},
538+
{
539+
Header: http.Header{
540+
"rewriteid": expectedValue,
541+
"Other-Header": []string{"another header"},
542+
"other-header": []string{"another another header"},
543+
},
544+
},
545+
{
546+
Header: http.Header{
547+
"rewrite-id": expectedValue,
548+
"Other-Header": []string{"another header"},
549+
"other-header": []string{"another another header"},
550+
},
551+
},
552+
{
553+
Header: http.Header{
554+
"rewrite_id": expectedValue,
555+
"Other-Header": []string{"another header"},
556+
"other-header": []string{"another another header"},
557+
},
558+
},
559+
}
560+
561+
for _, request := range requestList {
562+
actualValue, found := getRewriteIdHeaderValues(request)
563+
assert.Equal(t, expectedValue, actualValue)
564+
assert.True(t, found)
565+
}
566+
567+
}
568+
569+
func TestGetRewriteHeaderValues_MissingHeader(t *testing.T) {
570+
571+
requestList := []*http.Request{
572+
{
573+
Header: http.Header{
574+
"Other-Header": []string{"another header"},
575+
"other-header": []string{"another another header"},
576+
},
577+
},
578+
{
579+
Header: http.Header{},
580+
},
581+
}
582+
583+
for _, request := range requestList {
584+
actualValue, found := getRewriteIdHeaderValues(request)
585+
assert.Equal(t, []string{}, actualValue)
586+
assert.False(t, found)
587+
}
588+
589+
}

0 commit comments

Comments
 (0)