@@ -4,10 +4,10 @@ import (
4
4
"bytes"
5
5
"crypto/tls"
6
6
"fmt"
7
- "golang.org/x/time/rate"
8
7
"io"
9
8
"math/rand"
10
9
"net"
10
+ "os"
11
11
"regexp"
12
12
"runtime"
13
13
"strings"
@@ -16,6 +16,8 @@ import (
16
16
"testing"
17
17
"time"
18
18
19
+ "golang.org/x/time/rate"
20
+
19
21
"github.com/contentsquare/chproxy/cache"
20
22
21
23
"net/http"
@@ -36,10 +38,44 @@ const max_concurrent_goroutines = 256
36
38
const heavyRequestDuration = time .Millisecond * 512
37
39
const defaultUsername = "default"
38
40
const (
39
- okResponse = "1"
41
+ okResponse = "1"
42
+ badGatewayResponse = "]: cannot reach 127.0.0.1:"
40
43
)
44
+ const testCacheDir = "./test-cache-data"
41
45
42
46
var goodCfg = & config.Config {
47
+ Server : config.Server {
48
+ Metrics : config.Metrics {
49
+ Namespace : "proxy_test" },
50
+ },
51
+ Clusters : []config.Cluster {
52
+ {
53
+ Name : "cluster" ,
54
+ Scheme : "http" ,
55
+ Replicas : []config.Replica {
56
+ {
57
+ Nodes : []string {"localhost:8123" },
58
+ },
59
+ },
60
+ ClusterUsers : []config.ClusterUser {
61
+ {
62
+ Name : "web" ,
63
+ },
64
+ },
65
+ },
66
+ },
67
+ Users : []config.User {
68
+ {
69
+ Name : defaultUsername ,
70
+ ToCluster : "cluster" ,
71
+ ToUser : "web" ,
72
+ },
73
+ },
74
+ ParamGroups : []config.ParamGroup {
75
+ {Name : "param_test" , Params : []config.Param {{Key : "param_key" , Value : "param_value" }}},
76
+ },
77
+ }
78
+ var goodCfgWithCache = & config.Config {
43
79
Clusters : []config.Cluster {
44
80
{
45
81
Name : "cluster" ,
@@ -61,11 +97,23 @@ var goodCfg = &config.Config{
61
97
Name : defaultUsername ,
62
98
ToCluster : "cluster" ,
63
99
ToUser : "web" ,
100
+ Cache : "file_system_cache" ,
64
101
},
65
102
},
66
103
ParamGroups : []config.ParamGroup {
67
104
{Name : "param_test" , Params : []config.Param {{Key : "param_key" , Value : "param_value" }}},
68
105
},
106
+ Caches : []config.Cache {
107
+ {
108
+ Name : "file_system_cache" ,
109
+ Mode : "file_system" ,
110
+ FileSystem : config.FileSystemCacheConfig {
111
+ Dir : testCacheDir ,
112
+ MaxSize : config .ByteSize (1024 * 1024 ),
113
+ },
114
+ Expire : config .Duration (1000 * 60 * 60 ),
115
+ },
116
+ },
69
117
}
70
118
71
119
func newConfiguredProxy (cfg * config.Config ) (* reverseProxy , error ) {
@@ -79,7 +127,7 @@ func init() {
79
127
// we need to initiliaze prometheus metrics
80
128
// otherwise the calls the proxy.applyConfig will fail
81
129
// due to memory issues if someone only runs proxy_test
82
- initMetrics (goodCfg )
130
+ registerMetrics (goodCfg )
83
131
}
84
132
85
133
func TestNewReverseProxy (t * testing.T ) {
@@ -270,6 +318,28 @@ func TestReverseProxy_ServeHTTP1(t *testing.T) {
270
318
expStatusCode int
271
319
f func (p * reverseProxy ) * http.Response
272
320
}{
321
+ {
322
+ cfg : goodCfg ,
323
+ name : "Bad gatway response without cache" ,
324
+ expResponse : badGatewayResponse ,
325
+ expStatusCode : http .StatusBadGateway ,
326
+ f : func (p * reverseProxy ) * http.Response {
327
+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("%s/badGateway?query=SELECT123456" , fakeServer .URL ), nil )
328
+ return makeCustomRequest (p , req )
329
+ },
330
+ },
331
+ {
332
+ cfg : goodCfgWithCache ,
333
+ name : "Bad gatway response with cache" ,
334
+ expResponse : badGatewayResponse ,
335
+ expStatusCode : http .StatusBadGateway ,
336
+ f : func (p * reverseProxy ) * http.Response {
337
+ req := httptest .NewRequest ("GET" , fmt .Sprintf ("%s/badGateway?query=SELECT123456" , fakeServer .URL ), nil )
338
+ // cleaning the cache to be sure it will be a cache miss although the query isn't supposed to be cached
339
+ os .RemoveAll (testCacheDir )
340
+ return makeCustomRequest (p , req )
341
+ },
342
+ },
273
343
{
274
344
cfg : goodCfg ,
275
345
name : "Ok response" ,
@@ -900,6 +970,10 @@ var (
900
970
fmt .Fprintln (w , okResponse )
901
971
return
902
972
}
973
+ if r .URL .Path == "/badGateway" {
974
+ w .WriteHeader (http .StatusBadGateway )
975
+ return
976
+ }
903
977
904
978
body , err := io .ReadAll (r .Body )
905
979
if err != nil {
0 commit comments