// 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` 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 := `

Test

` 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithAbsoluteProxyURL(r, input) expected := `

Test

` if expected != output { t.Errorf(`Not expected output: got %q instead of %q`, output, expected) } } func TestAbsoluteProxyFilterWithCustomPortInBaseURL(t *testing.T) { os.Clearenv() os.Setenv("BASE_URL", "http://example.org:88/folder/") os.Setenv("MEDIA_PROXY_PRIVATE_KEY", "test") var err error parser := config.NewParser() config.Opts, err = parser.ParseEnvironmentVariables() if err != nil { t.Fatalf(`Parsing failure: %v`, err) } if config.Opts.BaseURL() != "http://example.org:88/folder" { t.Fatalf(`Unexpected base URL, got "%s"`, config.Opts.BaseURL()) } r := mux.NewRouter() r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `

Test

` output := RewriteDocumentWithAbsoluteProxyURL(r, input) expected := `

Test

` 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, 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithAbsoluteProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

Test

` output := RewriteDocumentWithRelativeProxyURL(r, input) expected := `

Test

` 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 := `

test

` expected := `

test

` 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 := `

test

` expected := `

test

` 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) } } func TestProxyFilterVideoPosterOnce(t *testing.T) { os.Clearenv() os.Setenv("PROXY_OPTION", "all") os.Setenv("PROXY_MEDIA_TYPES", "image,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) } }