// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package mediaproxy // import "miniflux.app/v2/internal/mediaproxy" 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := input if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := input if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, output, expected) } } func TestAbsoluteProxyFilterWithHttpsScheme(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image") os.Setenv("PROXY_PRIVATE_KEY", "test") os.Setenv("HTTPS", "1") 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 := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, output, expected) } } func TestAbsoluteProxyFilterWithHttpsAlwaysAndAudioTag(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "audio") 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 := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, output, expected) } } func TestAbsoluteProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) { os.Clearenv() os.Setenv("MEDIA_PROXY_MODE", "all") os.Setenv("MEDIA_PROXY_RESOURCE_TYPES", "image") os.Setenv("MEDIA_PROXY_CUSTOM_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 := RewriteDocumentWithAbsoluteProxyURL(r, "localhost", input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(r, input) expected := `` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, 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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(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 := RewriteDocumentWithRelativeProxyURL(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } }