Browse Source

Make sure PROXY_IMAGES option is backward compatible

Bug introduced in PR #1610

Fixes #1753
Frédéric Guillot 3 years ago
parent
commit
aa9b18a8d6
2 changed files with 68 additions and 2 deletions
  1. 60 0
      config/config_test.go
  2. 8 2
      config/parser.go

+ 60 - 0
config/config_test.go

@@ -1226,6 +1226,66 @@ func TestProxyMediaTypes(t *testing.T) {
 	}
 }
 
+func TestProxyMediaTypesWithDuplicatedValues(t *testing.T) {
+	os.Clearenv()
+	os.Setenv("PROXY_MEDIA_TYPES", "image,audio, image")
+
+	parser := NewParser()
+	opts, err := parser.ParseEnvironmentVariables()
+	if err != nil {
+		t.Fatalf(`Parsing failure: %v`, err)
+	}
+
+	expected := []string{"audio", "image"}
+	if len(expected) != len(opts.ProxyMediaTypes()) {
+		t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected)
+	}
+
+	resultMap := make(map[string]bool)
+	for _, mediaType := range opts.ProxyMediaTypes() {
+		resultMap[mediaType] = true
+	}
+
+	for _, mediaType := range expected {
+		if !resultMap[mediaType] {
+			t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected)
+		}
+	}
+}
+
+func TestProxyImagesOptionBackwardCompatibility(t *testing.T) {
+	os.Clearenv()
+	os.Setenv("PROXY_IMAGES", "all")
+
+	parser := NewParser()
+	opts, err := parser.ParseEnvironmentVariables()
+	if err != nil {
+		t.Fatalf(`Parsing failure: %v`, err)
+	}
+
+	expected := []string{"image"}
+	if len(expected) != len(opts.ProxyMediaTypes()) {
+		t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected)
+	}
+
+	resultMap := make(map[string]bool)
+	for _, mediaType := range opts.ProxyMediaTypes() {
+		resultMap[mediaType] = true
+	}
+
+	for _, mediaType := range expected {
+		if !resultMap[mediaType] {
+			t.Fatalf(`Unexpected PROXY_MEDIA_TYPES value, got %v instead of %v`, opts.ProxyMediaTypes(), expected)
+		}
+	}
+
+	expectedProxyOption := "all"
+	result := opts.ProxyOption()
+	if result != expectedProxyOption {
+		t.Fatalf(`Unexpected PROXY_OPTION value, got %q instead of %q`, result, expectedProxyOption)
+	}
+}
+
 func TestDefaultProxyMediaTypes(t *testing.T) {
 	os.Clearenv()
 

+ 8 - 2
config/parser.go

@@ -141,7 +141,6 @@ func (p *Parser) parseLines(lines []string) (err error) {
 		// kept for compatibility purpose
 		case "PROXY_IMAGES":
 			p.opts.proxyOption = parseString(value, defaultProxyOption)
-			p.opts.proxyMediaTypes = append(p.opts.proxyMediaTypes, "image")
 		case "PROXY_HTTP_CLIENT_TIMEOUT":
 			p.opts.proxyHTTPClientTimeout = parseInt(value, defaultProxyHTTPClientTimeout)
 		case "PROXY_OPTION":
@@ -297,9 +296,16 @@ func parseStringList(value string, fallback []string) []string {
 	}
 
 	var strList []string
+	strMap := make(map[string]bool)
+
 	items := strings.Split(value, ",")
 	for _, item := range items {
-		strList = append(strList, strings.TrimSpace(item))
+		itemValue := strings.TrimSpace(item)
+
+		if _, found := strMap[itemValue]; !found {
+			strMap[itemValue] = true
+			strList = append(strList, itemValue)
+		}
 	}
 
 	return strList