99func TestConfig (t * testing.T ) {
1010 t .Run ("inMemory config" , func (t * testing.T ) {
1111 defer setupEnv (t , "SERVER_STORAGE" , "memory" )()
12+ defer setupEnv (t , "CACHE_STRATEGY" , "memory" )()
13+ defer setupEnv (t , "DOWNSTREAM_HOST" , "http://localhost:8080" )()
1214 config := New ()
1315
1416 assert .NotNil (t , config )
@@ -17,9 +19,132 @@ func TestConfig(t *testing.T) {
1719 t .Run ("redis config" , func (t * testing.T ) {
1820 defer setupEnv (t , "SERVER_STORAGE" , "redis" )()
1921 defer setupEnv (t , "REDIS_CONNECTION_STRING" , "redis" )()
22+ defer setupEnv (t , "CACHE_STRATEGY" , "redis" )()
23+ defer setupEnv (t , "DOWNSTREAM_HOST" , "http://localhost:8080" )()
2024 config := New ()
2125
2226 assert .NotNil (t , config )
2327 assert .Equal (t , "redis" , config .RedisConfig .ConnectionString )
2428 })
29+
30+ t .Run ("header configuration" , func (t * testing.T ) {
31+ tests := []struct {
32+ name string
33+ hashHeaders string
34+ hashHeadersIgnore string
35+ wantHeaders []string
36+ }{
37+ {
38+ name : "empty headers" ,
39+ hashHeaders : "" ,
40+ hashHeadersIgnore : "" ,
41+ wantHeaders : []string {},
42+ },
43+ {
44+ name : "single header" ,
45+ hashHeaders : "Authorization" ,
46+ hashHeadersIgnore : "" ,
47+ wantHeaders : []string {"Authorization" },
48+ },
49+ {
50+ name : "multiple headers" ,
51+ hashHeaders : "Authorization,X-User-ID,Accept" ,
52+ hashHeadersIgnore : "" ,
53+ wantHeaders : []string {"Authorization" , "X-User-ID" , "Accept" },
54+ },
55+ {
56+ name : "headers with ignore" ,
57+ hashHeaders : "Authorization,X-User-ID,Accept" ,
58+ hashHeadersIgnore : "X-User-ID" ,
59+ wantHeaders : []string {"Authorization" , "X-User-ID" , "Accept" },
60+ },
61+ {
62+ name : "case insensitive headers" ,
63+ hashHeaders : "AUTHORIZATION,x-user-id,accept" ,
64+ hashHeadersIgnore : "X-USER-ID" ,
65+ wantHeaders : []string {"AUTHORIZATION" , "x-user-id" , "accept" },
66+ },
67+ {
68+ name : "whitespace in headers" ,
69+ hashHeaders : " Authorization , X-User-ID , Accept " ,
70+ hashHeadersIgnore : " X-User-ID " ,
71+ wantHeaders : []string {"Authorization" , "X-User-ID" , "Accept" },
72+ },
73+ {
74+ name : "duplicate headers" ,
75+ hashHeaders : "Authorization,Authorization,X-User-ID" ,
76+ hashHeadersIgnore : "" ,
77+ wantHeaders : []string {"Authorization" , "Authorization" , "X-User-ID" },
78+ },
79+ }
80+
81+ for _ , tt := range tests {
82+ t .Run (tt .name , func (t * testing.T ) {
83+ defer setupEnv (t , "CACHE_HASH_HEADERS" , tt .hashHeaders )()
84+ defer setupEnv (t , "CACHE_HASH_HEADERS_IGNORE" , tt .hashHeadersIgnore )()
85+
86+ config := New ()
87+
88+ assert .Equal (t , tt .wantHeaders , config .CacheConfig .HashHeaders )
89+ })
90+ }
91+ })
92+
93+ t .Run ("query configuration" , func (t * testing.T ) {
94+ tests := []struct {
95+ name string
96+ shouldHashQuery string
97+ queryIgnore string
98+ wantHashQuery bool
99+ wantIgnore map [string ]bool
100+ }{
101+ {
102+ name : "default query hashing" ,
103+ shouldHashQuery : "" ,
104+ queryIgnore : "" ,
105+ wantHashQuery : true ,
106+ wantIgnore : map [string ]bool {},
107+ },
108+ {
109+ name : "disabled query hashing" ,
110+ shouldHashQuery : "false" ,
111+ queryIgnore : "" ,
112+ wantHashQuery : false ,
113+ wantIgnore : map [string ]bool {},
114+ },
115+ {
116+ name : "query ignore parameters" ,
117+ shouldHashQuery : "true" ,
118+ queryIgnore : "timestamp,request_id" ,
119+ wantHashQuery : true ,
120+ wantIgnore : map [string ]bool {"timestamp" : true , "request_id" : true },
121+ },
122+ {
123+ name : "whitespace in query ignore" ,
124+ shouldHashQuery : "true" ,
125+ queryIgnore : " timestamp , request_id " ,
126+ wantHashQuery : true ,
127+ wantIgnore : map [string ]bool {"timestamp" : true , "request_id" : true },
128+ },
129+ {
130+ name : "case sensitive query ignore" ,
131+ shouldHashQuery : "true" ,
132+ queryIgnore : "Timestamp,Request_ID" ,
133+ wantHashQuery : true ,
134+ wantIgnore : map [string ]bool {"timestamp" : true , "request_id" : true },
135+ },
136+ }
137+
138+ for _ , tt := range tests {
139+ t .Run (tt .name , func (t * testing.T ) {
140+ defer setupEnv (t , "CACHE_SHOULD_HASH_QUERY" , tt .shouldHashQuery )()
141+ defer setupEnv (t , "CACHE_HASH_QUERY_IGNORE" , tt .queryIgnore )()
142+
143+ config := New ()
144+
145+ assert .Equal (t , tt .wantHashQuery , config .CacheConfig .ShouldHashQuery )
146+ assert .Equal (t , tt .wantIgnore , config .CacheConfig .HashQueryIgnore )
147+ })
148+ }
149+ })
25150}
0 commit comments