Skip to content

Commit

Permalink
Fix small header filter bug. Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
michelvocks committed Dec 3, 2019
1 parent cac755a commit 2912ad5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helper/metricsutil/metricsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func FormatFromRequest(req *logical.Request) string {

// Look for prometheus accept header
for _, header := range acceptHeaders {
if strings.Contains(header, PrometheusMetricFormat) {
if strings.Contains(header, PrometheusSchemaMIMEType) {
return PrometheusMetricFormat
}
}
Expand Down
45 changes: 45 additions & 0 deletions helper/metricsutil/metricsutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package metricsutil

import (
"github.com/hashicorp/vault/sdk/logical"
"testing"
)

func TestFormatFromRequest(t *testing.T) {
testCases := []struct {
original *logical.Request
expected string
}{
{
original: &logical.Request{Headers: map[string][]string{
"Accept": {
"application/vnd.google.protobuf",
"schema=\"prometheus/telemetry\"",
},
}},
expected: "prometheus",
},
{
original: &logical.Request{Headers: map[string][]string{
"Accept": {
"schema=\"prometheus\"",
},
}},
expected: "",
},
{
original: &logical.Request{Headers: map[string][]string{
"Accept": {
"application/openmetrics-text",
},
}},
expected: "prometheus",
},
}

for _, tCase := range testCases {
if metricsType := FormatFromRequest(tCase.original); metricsType != tCase.expected {
t.Fatalf("expected %s but got %s", tCase.expected, metricsType)
}
}
}

0 comments on commit 2912ad5

Please sign in to comment.