// Copyright 2020 Frédéric Guillot. All rights reserved. // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. package proxy // import "miniflux.app/proxy" import ( "net/http" "os" "testing" "github.com/gorilla/mux" "miniflux.app/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 := ProxyRewriter(r, input) expected := `

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

Test

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

test

` expected := `

test

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

test

` expected := `

test

` 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) } }