1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package featureflag
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
)
func TestGRPCMetadataFeatureFlag(t *testing.T) {
testCases := []struct {
flag string
headers map[string]string
enabled bool
onByDefault bool
desc string
}{
{"", nil, false, false, "empty name and no headers"},
{"flag", nil, false, false, "no headers"},
{"flag", map[string]string{"flag": "true"}, false, false, "no 'gitaly-feature' prefix in flag name"},
{"flag", map[string]string{"gitaly-feature-flag": "TRUE"}, false, false, "not valid header value"},
{"flag_under_score", map[string]string{"gitaly-feature-flag-under-score": "true"}, true, false, "flag name with underscores"},
{"flag-dash-ok", map[string]string{"gitaly-feature-flag-dash-ok": "true"}, true, false, "flag name with dashes"},
{"flag", map[string]string{"gitaly-feature-flag": "false"}, false, true, "flag explicitly disabled"},
{"flag", map[string]string{}, true, true, "flag enabled by default but missing"},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
md := metadata.New(tc.headers)
ctx := metadata.NewIncomingContext(context.Background(), md)
assert.Equal(t, tc.enabled, IsEnabled(ctx, FeatureFlag{tc.flag, tc.onByDefault}))
})
}
}
func TestAllEnabledFlags(t *testing.T) {
ctx := metadata.NewIncomingContext(
context.Background(),
metadata.New(
map[string]string{
ffPrefix + "meow": "true",
ffPrefix + "foo": "true",
ffPrefix + "woof": "false", // not enabled
ffPrefix + "bar": "TRUE", // not enabled
},
),
)
assert.ElementsMatch(t, AllFlags(ctx), []string{"meow:true", "foo:true", "woof:false", "bar:TRUE"})
}
|