Skip to content

Commit

Permalink
Scrape: test for q-value compliance with RFC 9110 in Accept header
Browse files Browse the repository at this point in the history
Signed-off-by: Julien <[email protected]>
  • Loading branch information
roidelapluie committed Sep 9, 2024
1 parent 8805301 commit 0a88943
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions scrape/scrape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/go-kit/log"
"github.com/gogo/protobuf/proto"
"github.com/google/go-cmp/cmp"
"github.com/grafana/regexp"
"github.com/prometheus/client_golang/prometheus"
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -2379,8 +2380,11 @@ func TestTargetScraperScrapeOK(t *testing.T) {
expectedTimeout = "1.5"
)

var protobufParsing bool
var allowUTF8 bool
var (
protobufParsing bool
allowUTF8 bool
qValuePattern = regexp.MustCompile(`q=([0-9]+(\.\d+)?)`)
)

server := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -2393,6 +2397,17 @@ func TestTargetScraperScrapeOK(t *testing.T) {
"Expected Accept header to prefer application/vnd.google.protobuf.")
}

contentTypes := strings.Split(accept, ",")
for _, ct := range contentTypes {
match := qValuePattern.FindStringSubmatch(ct)
require.Len(t, match, 3)
qValue, err := strconv.ParseFloat(match[1], 64)
require.NoError(t, err, "Error parsing q value")
require.GreaterOrEqual(t, qValue, float64(0))
require.LessOrEqual(t, qValue, float64(1))
require.LessOrEqual(t, len(strings.Split(match[1], ".")[1]), 3, "q value should have at most 3 decimal places")
}

timeout := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds")
require.Equal(t, expectedTimeout, timeout, "Expected scrape timeout header.")

Expand Down

0 comments on commit 0a88943

Please sign in to comment.