0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-30 22:34:18 -05:00

add tests for denyHosts flag

This commit is contained in:
Will Norris 2019-03-22 04:43:10 +00:00
parent 7e19b5ca6b
commit 24a2dda8e0

View file

@ -112,38 +112,45 @@ func TestAllowed(t *testing.T) {
url string url string
options Options options Options
allowHosts []string allowHosts []string
denyHosts []string
referrers []string referrers []string
key []byte key []byte
request *http.Request request *http.Request
allowed bool allowed bool
}{ }{
// no allowHosts or signature key // no allowHosts or signature key
{"http://test/image", emptyOptions, nil, nil, nil, nil, true}, {"http://test/image", emptyOptions, nil, nil, nil, nil, nil, true},
// allowHosts // allowHosts
{"http://good/image", emptyOptions, allowHosts, nil, nil, nil, true}, {"http://good/image", emptyOptions, allowHosts, nil, nil, nil, nil, true},
{"http://bad/image", emptyOptions, allowHosts, nil, nil, nil, false}, {"http://bad/image", emptyOptions, allowHosts, nil, nil, nil, nil, false},
// referrer // referrer
{"http://test/image", emptyOptions, nil, allowHosts, nil, genRequest(map[string]string{"Referer": "http://good/foo"}), true}, {"http://test/image", emptyOptions, nil, nil, allowHosts, nil, genRequest(map[string]string{"Referer": "http://good/foo"}), true},
{"http://test/image", emptyOptions, nil, allowHosts, nil, genRequest(map[string]string{"Referer": "http://bad/foo"}), false}, {"http://test/image", emptyOptions, nil, nil, allowHosts, nil, genRequest(map[string]string{"Referer": "http://bad/foo"}), false},
{"http://test/image", emptyOptions, nil, allowHosts, nil, genRequest(map[string]string{"Referer": "MALFORMED!!"}), false}, {"http://test/image", emptyOptions, nil, nil, allowHosts, nil, genRequest(map[string]string{"Referer": "MALFORMED!!"}), false},
{"http://test/image", emptyOptions, nil, allowHosts, nil, genRequest(map[string]string{}), false}, {"http://test/image", emptyOptions, nil, nil, allowHosts, nil, genRequest(map[string]string{}), false},
// signature key // signature key
{"http://test/image", Options{Signature: "NDx5zZHx7QfE8E-ijowRreq6CJJBZjwiRfOVk_mkfQQ="}, nil, nil, key, nil, true}, {"http://test/image", Options{Signature: "NDx5zZHx7QfE8E-ijowRreq6CJJBZjwiRfOVk_mkfQQ="}, nil, nil, nil, key, nil, true},
{"http://test/image", Options{Signature: "deadbeef"}, nil, nil, key, nil, false}, {"http://test/image", Options{Signature: "deadbeef"}, nil, nil, nil, key, nil, false},
{"http://test/image", emptyOptions, nil, nil, key, nil, false}, {"http://test/image", emptyOptions, nil, nil, nil, key, nil, false},
// allowHosts and signature // allowHosts and signature
{"http://good/image", emptyOptions, allowHosts, nil, key, nil, true}, {"http://good/image", emptyOptions, allowHosts, nil, nil, key, nil, true},
{"http://bad/image", Options{Signature: "gWivrPhXBbsYEwpmWAKjbJEiAEgZwbXbltg95O2tgNI="}, nil, nil, key, nil, true}, {"http://bad/image", Options{Signature: "gWivrPhXBbsYEwpmWAKjbJEiAEgZwbXbltg95O2tgNI="}, nil, nil, nil, key, nil, true},
{"http://bad/image", emptyOptions, allowHosts, nil, key, nil, false}, {"http://bad/image", emptyOptions, allowHosts, nil, nil, key, nil, false},
// deny requests that match denyHosts, even if signature is valid or also matches allowHosts
{"http://test/image", emptyOptions, nil, []string{"test"}, nil, nil, nil, false},
{"http://test/image", emptyOptions, []string{"test"}, []string{"test"}, nil, nil, nil, false},
{"http://test/image", Options{Signature: "NDx5zZHx7QfE8E-ijowRreq6CJJBZjwiRfOVk_mkfQQ="}, nil, []string{"test"}, nil, key, nil, false},
} }
for _, tt := range tests { for _, tt := range tests {
p := NewProxy(nil, nil) p := NewProxy(nil, nil)
p.AllowHosts = tt.allowHosts p.AllowHosts = tt.allowHosts
p.DenyHosts = tt.denyHosts
p.SignatureKey = tt.key p.SignatureKey = tt.key
p.Referrers = tt.referrers p.Referrers = tt.referrers