Skip to content

Commit

Permalink
fix up some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Aug 21, 2023
1 parent f026bbe commit 9b98acc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
6 changes: 3 additions & 3 deletions pkg/camo/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/camo/proxy_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
61 changes: 33 additions & 28 deletions pkg/camo/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/camo/proxy_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)...
Expand Down Expand Up @@ -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)...
Expand Down

0 comments on commit 9b98acc

Please sign in to comment.