From 2912ad5747b9c728568fc8897c009420e67e8007 Mon Sep 17 00:00:00 2001 From: Michel Vocks Date: Tue, 3 Dec 2019 10:52:57 +0100 Subject: [PATCH] Fix small header filter bug. Add test --- helper/metricsutil/metricsutil.go | 2 +- helper/metricsutil/metricsutil_test.go | 45 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 helper/metricsutil/metricsutil_test.go diff --git a/helper/metricsutil/metricsutil.go b/helper/metricsutil/metricsutil.go index e9f019cb40ea..83ca85a2d8fe 100644 --- a/helper/metricsutil/metricsutil.go +++ b/helper/metricsutil/metricsutil.go @@ -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 } } diff --git a/helper/metricsutil/metricsutil_test.go b/helper/metricsutil/metricsutil_test.go new file mode 100644 index 000000000000..bf33b76045f9 --- /dev/null +++ b/helper/metricsutil/metricsutil_test.go @@ -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) + } + } +}