2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-10-08 01:21:45 -04:00
|
|
|
|
2023-08-10 00:15:55 -04:00
|
|
|
package proxy // import "miniflux.app/v2/proxy"
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-08-10 00:15:55 -04:00
|
|
|
"miniflux.app/v2/config"
|
2020-10-08 01:21:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpDefault(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "http-only")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpsDefault(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "http-only")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-08 01:21:45 -04:00
|
|
|
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpNever(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "none")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-08 01:21:45 -04:00
|
|
|
expected := input
|
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpsNever(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "none")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-08 01:21:45 -04:00
|
|
|
expected := input
|
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpAlways(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpsAlways(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
|
|
|
expected := `<p><img src="/proxy/LdPNR1GBDigeeNp2ArUQRyZsVqT_PWLfHGjYFrrWWIY=/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 23:33:47 -04:00
|
|
|
func TestProxyFilterWithHttpsAlwaysAndCustomProxyServer(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_URL", "https://proxy-example/proxy")
|
2022-08-29 23:33:47 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2022-08-29 23:33:47 -04:00
|
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2022-08-29 23:33:47 -04:00
|
|
|
expected := `<p><img src="https://proxy-example/proxy/aHR0cHM6Ly93ZWJzaXRlL2ZvbGRlci9pbWFnZS5wbmc=" alt="Test"/></p>`
|
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:21:45 -04:00
|
|
|
func TestProxyFilterWithHttpInvalid(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "invalid")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>`
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithHttpsInvalid(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "invalid")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-08 01:21:45 -04:00
|
|
|
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithSrcset(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" srcset="http://website/folder/image2.png 656w, http://website/folder/image3.png 360w" alt="test"></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" srcset="/proxy/aY5Hb4urDnUCly2vTJ7ExQeeaVS-52O7kjUr2v9VrAs=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, /proxy/QgAmrJWiAud_nNAsz3F8OTxaIofwAiO36EDzH_YfMzo=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMy5wbmc= 360w" alt="test"/></p>`
|
|
|
|
output := ProxyRewriter(r, input)
|
2022-07-06 00:13:40 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithEmptySrcset(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2022-07-06 00:13:40 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2022-07-06 00:13:40 -04:00
|
|
|
|
|
|
|
input := `<p><img src="http://website/folder/image.png" srcset="" alt="test"></p>`
|
2023-02-25 03:36:19 -05:00
|
|
|
expected := `<p><img src="/proxy/okK5PsdNY8F082UMQEAbLPeUFfbe2WnNfInNmR9T4WA=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" srcset="" alt="test"/></p>`
|
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterWithPictureSource(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-08 01:21:45 -04:00
|
|
|
|
2022-07-04 15:48:48 -04:00
|
|
|
input := `<picture><source srcset="http://website/folder/image2.png 656w, http://website/folder/image3.png 360w, https://website/some,image.png 2x"></picture>`
|
2023-02-25 03:36:19 -05:00
|
|
|
expected := `<picture><source srcset="/proxy/aY5Hb4urDnUCly2vTJ7ExQeeaVS-52O7kjUr2v9VrAs=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, /proxy/QgAmrJWiAud_nNAsz3F8OTxaIofwAiO36EDzH_YfMzo=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMy5wbmc= 360w, /proxy/ZIw0hv8WhSTls5aSqhnFaCXlUrKIqTnBRaY0-NaLnds=/aHR0cHM6Ly93ZWJzaXRlL3NvbWUsaW1hZ2UucG5n 2x"/></picture>`
|
|
|
|
output := ProxyRewriter(r, input)
|
2022-07-04 15:48:48 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyFilterOnlyNonHTTPWithPictureSource(t *testing.T) {
|
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "https")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
|
|
|
os.Setenv("PROXY_PRIVATE_KEY", "test")
|
2022-07-04 15:48:48 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2022-07-04 15:48:48 -04:00
|
|
|
|
|
|
|
input := `<picture><source srcset="http://website/folder/image2.png 656w, https://website/some,image.png 2x"></picture>`
|
2023-02-25 03:36:19 -05:00
|
|
|
expected := `<picture><source srcset="/proxy/aY5Hb4urDnUCly2vTJ7ExQeeaVS-52O7kjUr2v9VrAs=/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlMi5wbmc= 656w, https://website/some,image.png 2x"/></picture>`
|
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-08 01:21:45 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 01:19:05 -04:00
|
|
|
|
2023-02-25 03:36:19 -05:00
|
|
|
func TestProxyWithImageDataURL(t *testing.T) {
|
2020-10-15 01:19:05 -04:00
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
2020-10-15 01:19:05 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-15 01:19:05 -04:00
|
|
|
|
|
|
|
input := `<img src="data:image/gif;base64,test">`
|
|
|
|
expected := `<img src="data:image/gif;base64,test"/>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-15 01:19:05 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 03:36:19 -05:00
|
|
|
func TestProxyWithImageSourceDataURL(t *testing.T) {
|
2020-10-15 01:19:05 -04:00
|
|
|
os.Clearenv()
|
2023-02-25 03:36:19 -05:00
|
|
|
os.Setenv("PROXY_OPTION", "all")
|
|
|
|
os.Setenv("PROXY_MEDIA_TYPES", "image")
|
2020-10-15 01:19:05 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
parser := config.NewParser()
|
|
|
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(`Parsing failure: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
2023-02-25 03:36:19 -05:00
|
|
|
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
2020-10-15 01:19:05 -04:00
|
|
|
|
|
|
|
input := `<picture><source srcset="data:image/gif;base64,test"/></picture>`
|
|
|
|
expected := `<picture><source srcset="data:image/gif;base64,test"/></picture>`
|
2023-02-25 03:36:19 -05:00
|
|
|
output := ProxyRewriter(r, input)
|
2020-10-15 01:19:05 -04:00
|
|
|
|
|
|
|
if expected != output {
|
|
|
|
t.Errorf(`Not expected output: got %s`, output)
|
|
|
|
}
|
|
|
|
}
|