Skip to content

Commit

Permalink
add tests max payload size to proxy_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
sigua-cs committed Sep 9, 2022
1 parent c65a989 commit 33755b4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 83 deletions.
83 changes: 1 addition & 82 deletions main_test.go
Expand Up @@ -145,7 +145,7 @@ func TestServe(t *testing.T) {
cachedData, err := cc.Get(key)

if cachedData != nil || err == nil {
t.Fatal("skipped response from cache is expected")
t.Fatal("response bigger than maxPayloadSize should not be cached")
}

resp.Body.Close()
Expand Down Expand Up @@ -176,11 +176,6 @@ func TestServe(t *testing.T) {
Version: cache.Version,
}

path := fmt.Sprintf("%s/cache/%s", testDir, key.String())
if _, err := os.Stat(path); err != nil {
t.Fatalf("err while getting file %q info: %s", path, err)
}

rw := httptest.NewRecorder()

cc := proxy.caches["https_cache_max_payload_size"]
Expand Down Expand Up @@ -423,82 +418,6 @@ func TestServe(t *testing.T) {
},
startHTTP,
},
{
"http cache max payload size",
"testdata/http.cache.max-payload-size.yml",
func(t *testing.T) {
q := "SELECT MaxPayloadSize"
req, err := http.NewRequest("GET", "http://127.0.0.1:9090?query="+url.QueryEscape(q), nil)
checkErr(t, err)
req.SetBasicAuth("default", "qwerty")
req.Close = true

resp, err := httpRequest(t, req, http.StatusOK)
checkResponse(t, resp.Body, expectedOkResp)
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}

key := &cache.Key{
Query: []byte(q),
AcceptEncoding: "gzip",
Version: cache.Version,
}

cc := proxy.caches["http_cache_max_payload_size"]
cachedData, err := cc.Get(key)

if cachedData != nil || err == nil {
t.Fatal("skipped response from cache is expected")
}

resp.Body.Close()
},
startHTTP,
},
{
"http cache max payload size not reached",
"testdata/http.cache.max-payload-size-not-reached.yml",
func(t *testing.T) {
q := "SELECT MaxPayloadSize"
req, err := http.NewRequest("GET", "http://127.0.0.1:9090?query="+url.QueryEscape(q), nil)
checkErr(t, err)
req.SetBasicAuth("default", "qwerty")
req.Close = true

resp, err := httpRequest(t, req, http.StatusOK)
checkResponse(t, resp.Body, expectedOkResp)
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}

key := &cache.Key{
Query: []byte(q),
AcceptEncoding: "gzip",
Version: cache.Version,
}

cc := proxy.caches["http_cache_max_payload_size"]
cachedData, err := cc.Get(key)

if err != nil {
t.Fatalf("unexpected error while getting response from cache: %s", err)
}

rw := httptest.NewRecorder()

err = RespondWithData(rw, cachedData.Data, cachedData.ContentMetadata, cachedData.Ttl, 200)
if err != nil {
t.Fatalf("unexpected error while getting response from cache: %s", err)
}
checkResponse(t, rw.Body, expectedOkResp)

cachedData.Data.Close()

resp.Body.Close()
},
startHTTP,
},
{
"http requests with caching in redis ",
"testdata/http.cache.redis.yml",
Expand Down
4 changes: 3 additions & 1 deletion proxy.go
Expand Up @@ -356,7 +356,9 @@ func (rp *reverseProxy) serveFromCache(s *scope, srw *statResponseWriter, req *h
// Do not cache responses greater than max payload size.
if contentLength > int64(s.user.cache.MaxPayloadSize) {
cacheSkipped.With(labels).Inc()
log.Debugf("%s: Request will not be cached. Content length (%d) is greater than max payload size (%d)", s, contentLength, s.user.cache.MaxPayloadSize)
log.Infof("%s: Request will not be cached. Content length (%d) is greater than max payload size (%d)", s, contentLength, s.user.cache.MaxPayloadSize)

rp.completeTransaction(s, statusCode, userCache, key, q)

err = RespondWithData(srw, reader, contentMetadata, 0*time.Second, tmpFileRespWriter.StatusCode())
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions proxy_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/tls"
"fmt"
"github.com/contentsquare/chproxy/cache"
"io"
"math/rand"
"net"
Expand Down Expand Up @@ -249,6 +250,19 @@ func TestReverseProxy_ServeHTTP1(t *testing.T) {
return makeRequest(p)
},
},
{
cfg: goodCfg,
name: "max payload size limit",
expResponse: okResponse,
expStatusCode: http.StatusOK,
f: func(p *reverseProxy) *http.Response {
p.caches["max_payload_size"] = &cache.AsyncCache{
MaxPayloadSize: 8 * 1024 * 1024,
}
p.users["default"].cache = p.caches["max_payload_size"]
return makeRequest(p)
},
},
{
cfg: goodCfg,
name: "queue overflow for user",
Expand Down Expand Up @@ -397,6 +411,21 @@ func TestReverseProxy_ServeHTTP1(t *testing.T) {
return makeCustomRequest(p, req)
},
},
{
cfg: authCfg,
name: "post request max payload size",
expResponse: okResponse,
expStatusCode: http.StatusOK,
f: func(p *reverseProxy) *http.Response {
uri := fmt.Sprintf("%s?user=foo&password=bar", fakeServer.URL)
req := httptest.NewRequest("POST", uri, nil)
p.caches["max_payload_size"] = &cache.AsyncCache{
MaxPayloadSize: 8 * 1024 * 1024,
}
p.users["foo"].cache = p.caches["max_payload_size"]
return makeCustomRequest(p, req)
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 33755b4

Please sign in to comment.