Merge pull request #1142 from owncloud/small-refactoring
small refactorings of test code and some methods
This commit is contained in:
5 files changed
+127
-97
No files matched your search
@@ -12,6 +12,33 @@ import (
|
|||||||
"github.com/owncloud/ocis/proxy/pkg/config"
|
"github.com/owncloud/ocis/proxy/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestLoadSelector(t *testing.T) {
|
||||||
|
type test struct {
|
||||||
|
cfg *config.PolicySelector
|
||||||
|
expectedErr error
|
||||||
|
}
|
||||||
|
sCfg := &config.StaticSelectorConf{Policy: "reva"}
|
||||||
|
mcfg := &config.MigrationSelectorConf{
|
||||||
|
AccFoundPolicy: "found",
|
||||||
|
AccNotFoundPolicy: "not_found",
|
||||||
|
UnauthenticatedPolicy: "unauth",
|
||||||
|
}
|
||||||
|
|
||||||
|
table := []test{
|
||||||
|
{cfg: &config.PolicySelector{Static: sCfg, Migration: mcfg}, expectedErr: ErrMultipleSelectors},
|
||||||
|
{cfg: &config.PolicySelector{}, expectedErr: ErrSelectorConfigIncomplete},
|
||||||
|
{cfg: &config.PolicySelector{Static: sCfg}, expectedErr: nil},
|
||||||
|
{cfg: &config.PolicySelector{Migration: mcfg}, expectedErr: nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range table {
|
||||||
|
_, err := LoadSelector(test.cfg)
|
||||||
|
if err != test.expectedErr {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStaticSelector(t *testing.T) {
|
func TestStaticSelector(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
req := httptest.NewRequest("GET", "https://example.org/foo", nil)
|
req := httptest.NewRequest("GET", "https://example.org/foo", nil)
|
||||||
|
|||||||
+17
-20
@@ -235,35 +235,32 @@ func (p *MultiHostReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request
|
|||||||
|
|
||||||
func (p MultiHostReverseProxy) queryRouteMatcher(endpoint string, target url.URL) bool {
|
func (p MultiHostReverseProxy) queryRouteMatcher(endpoint string, target url.URL) bool {
|
||||||
u, _ := url.Parse(endpoint)
|
u, _ := url.Parse(endpoint)
|
||||||
if strings.HasPrefix(target.Path, u.Path) && endpoint != "/" {
|
if !strings.HasPrefix(target.Path, u.Path) || endpoint == "/" {
|
||||||
query := u.Query()
|
return false
|
||||||
if len(query) != 0 {
|
}
|
||||||
rQuery := target.Query()
|
q := u.Query()
|
||||||
match := true
|
if len(q) == 0 {
|
||||||
for k := range query {
|
return false
|
||||||
v := query.Get(k)
|
}
|
||||||
rv := rQuery.Get(k)
|
tq := target.Query()
|
||||||
if rv != v {
|
for k := range q {
|
||||||
match = false
|
if q.Get(k) != tq.Get(k) {
|
||||||
break
|
return false
|
||||||
}
|
|
||||||
}
|
|
||||||
return match
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *MultiHostReverseProxy) regexRouteMatcher(endpoint string, target url.URL) bool {
|
func (p *MultiHostReverseProxy) regexRouteMatcher(pattern string, target url.URL) bool {
|
||||||
matched, err := regexp.MatchString(endpoint, target.String())
|
matched, err := regexp.MatchString(pattern, target.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Warn().Err(err).Msgf("regex with pattern %s failed", endpoint)
|
p.logger.Warn().Err(err).Msgf("regex with pattern %s failed", pattern)
|
||||||
}
|
}
|
||||||
return matched
|
return matched
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *MultiHostReverseProxy) prefixRouteMatcher(endpoint string, target url.URL) bool {
|
func (p *MultiHostReverseProxy) prefixRouteMatcher(prefix string, target url.URL) bool {
|
||||||
return strings.HasPrefix(target.Path, endpoint) && endpoint != "/"
|
return strings.HasPrefix(target.Path, prefix) && prefix != "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultPolicies() []config.Policy {
|
func defaultPolicies() []config.Policy {
|
||||||
|
|||||||
@@ -7,16 +7,27 @@ import (
|
|||||||
"github.com/owncloud/ocis/proxy/pkg/config"
|
"github.com/owncloud/ocis/proxy/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type matchertest struct {
|
||||||
|
endpoint, target string
|
||||||
|
matches bool
|
||||||
|
}
|
||||||
|
|
||||||
func TestPrefixRouteMatcher(t *testing.T) {
|
func TestPrefixRouteMatcher(t *testing.T) {
|
||||||
cfg := config.New()
|
cfg := config.New()
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
p := NewMultiHostReverseProxy(Config(cfg))
|
||||||
|
|
||||||
endpoint := "/foobar"
|
table := []matchertest{
|
||||||
u, _ := url.Parse("/foobar/baz/some/url")
|
{endpoint: "/foobar", target: "/foobar/baz/some/url", matches: true},
|
||||||
|
{endpoint: "/fobar", target: "/foobar/baz/some/url", matches: false},
|
||||||
|
}
|
||||||
|
|
||||||
matched := p.prefixRouteMatcher(endpoint, *u)
|
for _, test := range table {
|
||||||
if !matched {
|
u, _ := url.Parse(test.target)
|
||||||
t.Errorf("Endpoint %s and URL %s should match", endpoint, u.String())
|
matched := p.prefixRouteMatcher(test.endpoint, *u)
|
||||||
|
if matched != test.matches {
|
||||||
|
t.Errorf("PrefixRouteMatcher returned %t expected %t for endpoint: %s and target %s",
|
||||||
|
matched, test.matches, test.endpoint, u.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,64 +35,26 @@ func TestQueryRouteMatcher(t *testing.T) {
|
|||||||
cfg := config.New()
|
cfg := config.New()
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
p := NewMultiHostReverseProxy(Config(cfg))
|
||||||
|
|
||||||
endpoint := "/foobar?parameter=true"
|
table := []matchertest{
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=true")
|
{endpoint: "/foobar?parameter=true", target: "/foobar/baz/some/url?parameter=true", matches: true},
|
||||||
|
{endpoint: "/foobar", target: "/foobar/baz/some/url?parameter=true", matches: false},
|
||||||
matched := p.queryRouteMatcher(endpoint, *u)
|
{endpoint: "/foobar?parameter=false", target: "/foobar/baz/some/url?parameter=true", matches: false},
|
||||||
if !matched {
|
{endpoint: "/foobar?parameter=false&other=true", target: "/foobar/baz/some/url?parameter=true", matches: false},
|
||||||
t.Errorf("Endpoint %s and URL %s should match", endpoint, u.String())
|
{
|
||||||
|
endpoint: "/foobar?parameter=false&other=true",
|
||||||
|
target: "/foobar/baz/some/url?parameter=false&other=true",
|
||||||
|
matches: true,
|
||||||
|
},
|
||||||
|
{endpoint: "/fobar", target: "/foobar", matches: false},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func TestQueryRouteMatcherWithoutParameters(t *testing.T) {
|
for _, test := range table {
|
||||||
cfg := config.New()
|
u, _ := url.Parse(test.target)
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
matched := p.queryRouteMatcher(test.endpoint, *u)
|
||||||
|
if matched != test.matches {
|
||||||
endpoint := "/foobar"
|
t.Errorf("QueryRouteMatcher returned %t expected %t for endpoint: %s and target %s",
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=true")
|
matched, test.matches, test.endpoint, u.String())
|
||||||
|
}
|
||||||
matched := p.queryRouteMatcher(endpoint, *u)
|
|
||||||
if matched {
|
|
||||||
t.Errorf("Endpoint %s and URL %s should not match", endpoint, u.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQueryRouteMatcherWithDifferingParameters(t *testing.T) {
|
|
||||||
cfg := config.New()
|
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
|
||||||
|
|
||||||
endpoint := "/foobar?parameter=false"
|
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=true")
|
|
||||||
|
|
||||||
matched := p.queryRouteMatcher(endpoint, *u)
|
|
||||||
if matched {
|
|
||||||
t.Errorf("Endpoint %s and URL %s should not match", endpoint, u.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQueryRouteMatcherWithMultipleDifferingParameters(t *testing.T) {
|
|
||||||
cfg := config.New()
|
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
|
||||||
|
|
||||||
endpoint := "/foobar?parameter=false&other=true"
|
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=true")
|
|
||||||
|
|
||||||
matched := p.queryRouteMatcher(endpoint, *u)
|
|
||||||
if matched {
|
|
||||||
t.Errorf("Endpoint %s and URL %s should not match", endpoint, u.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestQueryRouteMatcherWithMultipleParameters(t *testing.T) {
|
|
||||||
cfg := config.New()
|
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
|
||||||
|
|
||||||
endpoint := "/foobar?parameter=false&other=true"
|
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=false&other=true")
|
|
||||||
|
|
||||||
matched := p.queryRouteMatcher(endpoint, *u)
|
|
||||||
if !matched {
|
|
||||||
t.Errorf("Endpoint %s and URL %s should match", endpoint, u.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,24 +62,37 @@ func TestRegexRouteMatcher(t *testing.T) {
|
|||||||
cfg := config.New()
|
cfg := config.New()
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
p := NewMultiHostReverseProxy(Config(cfg))
|
||||||
|
|
||||||
endpoint := ".*some\\/url.*parameter=true"
|
table := []matchertest{
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=true")
|
{endpoint: ".*some\\/url.*parameter=true", target: "/foobar/baz/some/url?parameter=true", matches: true},
|
||||||
|
{endpoint: "([\\])\\w+", target: "/foobar/baz/some/url?parameter=true", matches: false},
|
||||||
|
}
|
||||||
|
|
||||||
matched := p.regexRouteMatcher(endpoint, *u)
|
for _, test := range table {
|
||||||
if !matched {
|
u, _ := url.Parse(test.target)
|
||||||
t.Errorf("Endpoint %s and URL %s should match", endpoint, u.String())
|
matched := p.regexRouteMatcher(test.endpoint, *u)
|
||||||
|
if matched != test.matches {
|
||||||
|
t.Errorf("RegexRouteMatcher returned %t expected %t for endpoint: %s and target %s",
|
||||||
|
matched, test.matches, test.endpoint, u.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRegexRouteMatcherWithInvalidPattern(t *testing.T) {
|
func TestSingleJoiningSlash(t *testing.T) {
|
||||||
cfg := config.New()
|
type test struct {
|
||||||
p := NewMultiHostReverseProxy(Config(cfg))
|
a, b, result string
|
||||||
|
}
|
||||||
|
|
||||||
endpoint := "([\\])\\w+"
|
table := []test{
|
||||||
u, _ := url.Parse("/foobar/baz/some/url?parameter=true")
|
{a: "a", b: "b", result: "a/b"},
|
||||||
|
{a: "a/", b: "b", result: "a/b"},
|
||||||
|
{a: "a", b: "/b", result: "a/b"},
|
||||||
|
{a: "a/", b: "/b", result: "a/b"},
|
||||||
|
}
|
||||||
|
|
||||||
matched := p.regexRouteMatcher(endpoint, *u)
|
for _, test := range table {
|
||||||
if matched {
|
p := singleJoiningSlash(test.a, test.b)
|
||||||
t.Errorf("Endpoint %s and URL %s should not match", endpoint, u.String())
|
if p != test.result {
|
||||||
|
t.Errorf("SingleJoiningSlash got %s expected %s", p, test.result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,9 +60,7 @@ func EncoderForType(fileType string) Encoder {
|
|||||||
switch strings.ToLower(fileType) {
|
switch strings.ToLower(fileType) {
|
||||||
case "png":
|
case "png":
|
||||||
return PngEncoder{}
|
return PngEncoder{}
|
||||||
case "jpg":
|
case "jpg", "jpeg":
|
||||||
fallthrough
|
|
||||||
case "jpeg":
|
|
||||||
return JpegEncoder{}
|
return JpegEncoder{}
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package thumbnail
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestEncoderForType(t *testing.T) {
|
||||||
|
table := map[string]Encoder{
|
||||||
|
"jpg": JpegEncoder{},
|
||||||
|
"JPG": JpegEncoder{},
|
||||||
|
"jpeg": JpegEncoder{},
|
||||||
|
"JPEG": JpegEncoder{},
|
||||||
|
"png": PngEncoder{},
|
||||||
|
"PNG": PngEncoder{},
|
||||||
|
"invalid": nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range table {
|
||||||
|
e := EncoderForType(k)
|
||||||
|
if e != v {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user