vitess.io/vitess@v0.16.2/go/vt/vtadmin/cache/refresh_test.go (about) 1 /* 2 Copyright 2022 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cache_test 18 19 import ( 20 "context" 21 "net/http" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "google.golang.org/grpc/metadata" 26 27 "vitess.io/vitess/go/vt/vtadmin/cache" 28 ) 29 30 const refreshKey = "cache_test" 31 32 func init() { 33 cache.SetCacheRefreshKey(refreshKey) 34 } 35 36 func TestShouldRefreshFromIncomingContext(t *testing.T) { 37 t.Parallel() 38 39 t.Run("true", func(t *testing.T) { 40 t.Parallel() 41 42 ctx := cache.NewIncomingRefreshContext(context.Background()) 43 assert.True(t, cache.ShouldRefreshFromIncomingContext(ctx), "incoming context with metadata set should signal refresh") 44 }) 45 46 t.Run("false", func(t *testing.T) { 47 t.Parallel() 48 49 tests := []struct { 50 name string 51 ctx context.Context 52 }{ 53 { 54 name: "no metadata in context", 55 ctx: context.Background(), 56 }, 57 { 58 name: "no cache key in metadata", 59 ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("key", "val")), 60 }, 61 { 62 name: "cache key set to non-bool", 63 ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs(refreshKey, "this is not a boolean")), 64 }, 65 } 66 67 for _, tt := range tests { 68 tt := tt 69 t.Run(tt.name, func(t *testing.T) { 70 t.Parallel() 71 72 assert.False(t, cache.ShouldRefreshFromIncomingContext(tt.ctx), "incoming context should not signal refresh") 73 }) 74 } 75 }) 76 } 77 78 func TestShouldRefreshFromRequest(t *testing.T) { 79 t.Parallel() 80 81 t.Run("true", func(t *testing.T) { 82 t.Parallel() 83 84 r, _ := http.NewRequest("", "", nil) 85 r.Header.Add("x-"+refreshKey, "true") 86 87 assert.True(t, cache.ShouldRefreshFromRequest(r), "request header should signal cache refresh") 88 }) 89 90 t.Run("false", func(t *testing.T) { 91 t.Parallel() 92 93 tests := []struct { 94 name string 95 headers map[string]string 96 }{ 97 { 98 name: "no headers set", 99 headers: nil, 100 }, 101 { 102 name: "no cache refresh header", 103 headers: map[string]string{ 104 "x-forwarded-for": "whatever", 105 "content-type": "application/json", 106 }, 107 }, 108 { 109 name: "cache refresh set to non-boolean", 110 headers: map[string]string{ 111 "x-" + refreshKey: "this is not a boolean", 112 }, 113 }, 114 } 115 116 for _, tt := range tests { 117 tt := tt 118 t.Run(tt.name, func(t *testing.T) { 119 t.Parallel() 120 121 r, _ := http.NewRequest("", "", nil) 122 for header, val := range tt.headers { 123 r.Header.Add(header, val) 124 } 125 126 assert.False(t, cache.ShouldRefreshFromRequest(r), "request headers should not signal cache refresh") 127 }) 128 } 129 }) 130 }