// 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_IMAGES", "http-only") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `
` output := ImageProxyRewriter(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_IMAGES", "http-only") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(r, 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_IMAGES", "all") os.Setenv("PROXY_IMAGE_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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "invalid") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "invalid") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` output := ImageProxyRewriter(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_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ImageProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterWithEmptySrcset(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ImageProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterWithPictureSource(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ImageProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestProxyFilterOnlyNonHTTPWithPictureSource(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "https") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ImageProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestImageProxyWithImageDataURL(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ImageProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } } func TestImageProxyWithImageSourceDataURL(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") 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/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") input := `` expected := `` output := ImageProxyRewriter(r, input) if expected != output { t.Errorf(`Not expected output: got %s`, output) } }