Skip to content

Commit

Permalink
Make sure PROXY_IMAGES option is backward compatible
Browse files Browse the repository at this point in the history
Bug introduced in PR miniflux#1610

Fixes miniflux#1753
  • Loading branch information
fguillot committed Apr 3, 2023
1 parent 8b6dd3e commit aa9b18a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
60 changes: 60 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
10 changes: 8 additions & 2 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aa9b18a

Please sign in to comment.