github.com/thanos-io/thanos@v0.32.5/cmd/thanos/query_frontend_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package main 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/efficientgo/core/testutil" 11 ) 12 13 func Test_extractOrgId(t *testing.T) { 14 var testData = []struct { 15 configuredHeaders []string 16 requestHeaders map[string]string 17 expectOrgId string 18 }{ 19 { 20 configuredHeaders: []string{"x-grafana-user", "x-server-id"}, 21 requestHeaders: map[string]string{"x-grafana-user": "imagrafanauser", "x-server-id": "iamserverid"}, 22 expectOrgId: "imagrafanauser", 23 }, 24 { 25 configuredHeaders: []string{"x-server-id", "x-grafana-user"}, 26 requestHeaders: map[string]string{"x-grafana-user": "imagrafanauser", "x-server-id": "iamserverid"}, 27 expectOrgId: "iamserverid", 28 }, 29 { 30 configuredHeaders: []string{}, 31 requestHeaders: map[string]string{"another-header": "another"}, 32 expectOrgId: "anonymous", 33 }, 34 } 35 for _, data := range testData { 36 config := queryFrontendConfig{ 37 orgIdHeaders: data.configuredHeaders, 38 } 39 req, _ := http.NewRequest("", "", nil) 40 for k, v := range data.requestHeaders { 41 req.Header.Set(k, v) 42 } 43 id := extractOrgId(&config, req) 44 testutil.Equals(t, data.expectOrgId, id) 45 } 46 }