From 9b98acc2e958ebcb6872c8a598a9c8c360aa2263 Mon Sep 17 00:00:00 2001 From: elij Date: Sun, 20 Aug 2023 17:16:58 -0700 Subject: [PATCH] fix up some tests --- pkg/camo/helpers_test.go | 6 ++-- pkg/camo/proxy_filter_test.go | 6 ++-- pkg/camo/proxy_test.go | 61 ++++++++++++++++++---------------- pkg/camo/proxy_timeout_test.go | 6 ++-- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/pkg/camo/helpers_test.go b/pkg/camo/helpers_test.go index f044904..aa824d1 100644 --- a/pkg/camo/helpers_test.go +++ b/pkg/camo/helpers_test.go @@ -17,9 +17,9 @@ import ( is "gotest.tools/v3/assert/cmp" ) -func makeReq(config Config, testURL string) (*http.Request, error) { +func makeReq(config Config, testURL string, addHdr http.Header) (*http.Request, error) { k := []byte(config.HMACKey) - hexURL, err := encoding.B64EncodeURL(k, testURL, nil) + hexURL, err := encoding.B64EncodeURL(k, testURL, addHdr) if err != nil { return nil, fmt.Errorf("Error encoding req url '%s': %s", testURL, err.Error()) } @@ -65,7 +65,7 @@ func processRequest(req *http.Request, status int, camoConfig Config, filters [] } func makeTestReq(testURL string, status int, config Config) (*http.Response, error) { - req, err := makeReq(config, testURL) + req, err := makeReq(config, testURL, nil) if err != nil { return nil, err } diff --git a/pkg/camo/proxy_filter_test.go b/pkg/camo/proxy_filter_test.go index bfe493e..e177b10 100644 --- a/pkg/camo/proxy_filter_test.go +++ b/pkg/camo/proxy_filter_test.go @@ -24,7 +24,7 @@ func TestFilterListAcceptSimple(t *testing.T) { }, } testURL := "http://www.google.com/images/srpr/logo11w.png" - req, err := makeReq(camoConfig, testURL) + req, err := makeReq(camoConfig, testURL, nil) assert.Check(t, err) _, err = processRequest(req, 200, camoConfig, filters) assert.Check(t, err) @@ -42,7 +42,7 @@ func TestFilterListAcceptSimpleWithFilterError(t *testing.T) { }, } testURL := "http://www.google.com/images/srpr/logo11w.png" - req, err := makeReq(camoConfig, testURL) + req, err := makeReq(camoConfig, testURL, nil) assert.Check(t, err) _, err = processRequest(req, 404, camoConfig, filters) assert.Check(t, err) @@ -53,7 +53,7 @@ func TestFilterListMatrixMultiples(t *testing.T) { t.Parallel() testURL := "http://www.google.com/images/srpr/logo11w.png" - req, err := makeReq(camoConfig, testURL) + req, err := makeReq(camoConfig, testURL, nil) assert.Check(t, err) type chkResponse struct { chk bool diff --git a/pkg/camo/proxy_test.go b/pkg/camo/proxy_test.go index 3c0e967..03f64d5 100644 --- a/pkg/camo/proxy_test.go +++ b/pkg/camo/proxy_test.go @@ -170,7 +170,7 @@ func TestXForwardedFor(t *testing.T) { })) defer ts.Close() - req, err := makeReq(camoConfig, ts.URL) + req, err := makeReq(camoConfig, ts.URL, nil) assert.Check(t, err) req.Header.Set("X-Forwarded-For", "2.2.2.2, 1.1.1.1") @@ -200,7 +200,7 @@ func TestVideoContentTypeAllowed(t *testing.T) { testURL := "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4" // try a range request (should succeed, MaxSize is larger than requested range) - req, err := makeReq(camoConfigWithVideo, testURL) + req, err := makeReq(camoConfigWithVideo, testURL, nil) assert.Check(t, err) req.Header.Add("Range", "bytes=0-10") resp, err := processRequest(req, 206, camoConfigWithVideo, nil) @@ -209,7 +209,7 @@ func TestVideoContentTypeAllowed(t *testing.T) { // try a range request (should fail, MaxSize is smaller than requested range) camoConfigWithVideo.MaxSize = 1 * 1024 - req, err = makeReq(camoConfigWithVideo, testURL) + req, err = makeReq(camoConfigWithVideo, testURL, nil) assert.Check(t, err) req.Header.Add("Range", "bytes=0-1025") _, err = processRequest(req, 404, camoConfigWithVideo, nil) @@ -243,7 +243,7 @@ func TestAudioContentTypeAllowed(t *testing.T) { assert.Check(t, err) // try a range request - req, err := makeReq(camoConfigWithAudio, testURL) + req, err := makeReq(camoConfigWithAudio, testURL, nil) assert.Check(t, err) req.Header.Add("Range", "bytes=0-10") resp, err := processRequest(req, 206, camoConfigWithAudio, nil) @@ -271,7 +271,7 @@ func TestCredetialURLsAllowed(t *testing.T) { func TestSupplyAcceptIfNoneGiven(t *testing.T) { t.Parallel() testURL := "http://images.anandtech.com/doci/6673/OpenMoboAMD30_575px.png" - req, err := makeReq(camoConfig, testURL) + req, err := makeReq(camoConfig, testURL, nil) req.Header.Del("Accept") assert.Check(t, err) _, err = processRequest(req, 200, camoConfig, nil) @@ -424,7 +424,7 @@ func Test404OnLoopback(t *testing.T) { t.Parallel() testURL := "http://mockbin.org/redirect/302?to=http://test.vcap.me" - req, err := makeReq(camoConfig, testURL) + req, err := makeReq(camoConfig, testURL, nil) assert.Check(t, err) resp, err := processRequest(req, 404, camoConfig, nil) @@ -438,36 +438,41 @@ func TestDownloadDisposition(t *testing.T) { camoConfigCopy := camoConfig testURL := "http://www.google.com/images/srpr/logo11w.png" - // with EnableDownloadParam disabled + // with EnableAddHeaders disabled camoConfigCopy.EnableAddHeaders = false - req, err := makeReq(camoConfigCopy, testURL) + req, err := makeReq( + camoConfigCopy, + testURL, + http.Header{ + "content-disposition": []string{ + "attachment; filename=\"image\"", + }, + }, + ) assert.Check(t, err) - resp, err := processRequest(req, 200, camoConfigCopy, nil) - headerAssert(t, "", "Content-Disposition", resp) - assert.Check(t, err) - - params := req.URL.Query() - params.Add("download", "") - req.URL.RawQuery = params.Encode() - resp, err = processRequest(req, 200, camoConfigCopy, nil) - headerAssert(t, "", "Content-Disposition", resp) + // making a request for additional headers, emits a 4th url component + // that is included under the hmac. If the camo proxy disables + // additional header support, the 4th url segment is ignored, + // and the result is an hmac signature failure. Reject 403. + _, err = processRequest(req, 403, camoConfigCopy, nil) assert.Check(t, err) - // with EnableDownloadParam enabled + // with EnableAddHeaders enabled camoConfigCopy.EnableAddHeaders = true - req, err = makeReq(camoConfigCopy, testURL) + req, err = makeReq( + camoConfigCopy, + testURL, + http.Header{ + "content-disposition": []string{ + "attachment; filename=\"image\"", + }, + }, + ) assert.Check(t, err) - resp, err = processRequest(req, 200, camoConfigCopy, nil) - headerAssert(t, "", "Content-Disposition", resp) - assert.Check(t, err) - - params = req.URL.Query() - params.Add("download", "") - req.URL.RawQuery = params.Encode() - resp, err = processRequest(req, 200, camoConfigCopy, nil) - headerAssert(t, "attachment", "Content-Disposition", resp) + resp, err := processRequest(req, 200, camoConfigCopy, nil) + headerAssert(t, "attachment; filename=\"image\"", "Content-Disposition", resp) assert.Check(t, err) } diff --git a/pkg/camo/proxy_timeout_test.go b/pkg/camo/proxy_timeout_test.go index a22886c..cc3ce79 100644 --- a/pkg/camo/proxy_timeout_test.go +++ b/pkg/camo/proxy_timeout_test.go @@ -44,7 +44,7 @@ func TestTimeout(t *testing.T) { })) defer ts.Close() - req, err := makeReq(c, ts.URL) + req, err := makeReq(c, ts.URL, nil) assert.Check(t, err) errc := make(chan error, 1) @@ -240,7 +240,7 @@ func TestServerEarlyEOF(t *testing.T) { )) defer ts.Close() - req, err := makeReq(c, ts.URL) + req, err := makeReq(c, ts.URL, nil) assert.Check(t, err) // response is a 200, not much we can do about that since we response // streaming (chunked)... @@ -286,7 +286,7 @@ func TestServerChunkTooBig(t *testing.T) { )) defer ts.Close() - req, err := makeReq(c, ts.URL) + req, err := makeReq(c, ts.URL, nil) assert.Check(t, err) // response is a 200, not much we can do about that since we response // streaming (chunked)...