From 1cd3f8936d765fcc13996a39e8c259decb52f234 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Tue, 24 Mar 2020 10:43:01 +0100 Subject: [PATCH] add unit tests --- pkg/proxy/proxy_test.go | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 pkg/proxy/proxy_test.go diff --git a/pkg/proxy/proxy_test.go b/pkg/proxy/proxy_test.go new file mode 100644 index 0000000000..7a5d3d64bb --- /dev/null +++ b/pkg/proxy/proxy_test.go @@ -0,0 +1,112 @@ +package proxy + +import ( + "net/url" + "testing" + + "github.com/owncloud/ocis-proxy/pkg/config" +) + +func TestPrefixRouteMatcher(t *testing.T) { + cfg := config.New() + p := NewMultiHostReverseProxy(Config(cfg)) + + endpoint := "/foobar" + u, _ := url.Parse("/foobar/baz/some/url") + + matched := p.prefixRouteMatcher(endpoint, *u) + if !matched { + t.Errorf("Endpoint %s and URL %s should match", endpoint, u.String()) + } +} + +func TestQueryRouteMatcher(t *testing.T) { + cfg := config.New() + p := NewMultiHostReverseProxy(Config(cfg)) + + endpoint := "/foobar?parameter=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 match", endpoint, u.String()) + } +} + +func TestQueryRouteMatcherWithoutParameters(t *testing.T) { + cfg := config.New() + p := NewMultiHostReverseProxy(Config(cfg)) + + endpoint := "/foobar" + 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 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()) + } +} + +func TestRegexRouteMatcher(t *testing.T) { + cfg := config.New() + p := NewMultiHostReverseProxy(Config(cfg)) + + endpoint := ".*some\\/url.*parameter=true" + u, _ := url.Parse("/foobar/baz/some/url?parameter=true") + + matched := p.regexRouteMatcher(endpoint, *u) + if !matched { + t.Errorf("Endpoint %s and URL %s should match", endpoint, u.String()) + } +} + +func TestRegexRouteMatcherWithInvalidPattern(t *testing.T) { + cfg := config.New() + p := NewMultiHostReverseProxy(Config(cfg)) + + endpoint := "([\\])\\w+" + u, _ := url.Parse("/foobar/baz/some/url?parameter=true") + + matched := p.regexRouteMatcher(endpoint, *u) + if matched { + t.Errorf("Endpoint %s and URL %s should not match", endpoint, u.String()) + } +}