// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package proxy // import "miniflux.app/v2/internal/proxy" import ( "net/http" "os" "testing" "github.com/gorilla/mux" "miniflux.app/v2/internal/config" ) func TestProxyFilterWithHttpDefault(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "http-only") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `
` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpsDefault(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "http-only") os.Setenv("PROXY_MEDIA_TYPES", "image") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpNever(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "none") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := input if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpsNever(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "none") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := input if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpAlways(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpsAlways(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestAbsoluteProxyFilterWithHttpsAlways(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := AbsoluteProxyRewriter(r, "localhost", input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_URL", "https://proxy-example/proxy") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpsAlwaysAndIncorrectCustomProxyServer(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_URL", "http://:8080example.com") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestAbsoluteProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_URL", "https://proxy-example/proxy") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpInvalid(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "invalid") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithHttpsInvalid(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "invalid") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ProxyRewriter(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) } } func TestProxyFilterWithSrcset(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterWithEmptySrcset(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterWithPictureSource(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterOnlyNonHTTPWithPictureSource(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "https") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyWithImageDataURL(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyWithImageSourceDataURL(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterWithVideo(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "video") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterVideoPoster(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } }