Fixes #77 bad tinyproxy configuration generation

This commit is contained in:
Quentin McGaw (desktop)
2020-02-08 14:08:51 +00:00
parent 63fd72524e
commit fc9ebd561c
2 changed files with 74 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package tinyproxy
import ( import (
"fmt" "fmt"
"sort"
"github.com/qdm12/golibs/files" "github.com/qdm12/golibs/files"
"github.com/qdm12/private-internet-access-docker/internal/constants" "github.com/qdm12/private-internet-access-docker/internal/constants"
@@ -23,7 +24,7 @@ func generateConf(logLevel models.TinyProxyLogLevel, port uint16, user, password
"Group": "tinyproxy", "Group": "tinyproxy",
"Port": fmt.Sprintf("%d", port), "Port": fmt.Sprintf("%d", port),
"Timeout": "600", "Timeout": "600",
"DefaultErrorFile": "/usr/share/tinyproxy/default.html", "DefaultErrorFile": "\"/usr/share/tinyproxy/default.html\"",
"MaxClients": "100", "MaxClients": "100",
"MinSpareServers": "5", "MinSpareServers": "5",
"MaxSpareServers": "20", "MaxSpareServers": "20",
@@ -31,7 +32,7 @@ func generateConf(logLevel models.TinyProxyLogLevel, port uint16, user, password
"MaxRequestsPerChild": "0", "MaxRequestsPerChild": "0",
"DisableViaHeader": "Yes", "DisableViaHeader": "Yes",
"LogLevel": string(logLevel), "LogLevel": string(logLevel),
// "StatFile": "/usr/share/tinyproxy/stats.html", // "StatFile": "\"/usr/share/tinyproxy/stats.html\"",
} }
if len(user) > 0 { if len(user) > 0 {
confMapping["BasicAuth"] = fmt.Sprintf("%s %s", user, password) confMapping["BasicAuth"] = fmt.Sprintf("%s %s", user, password)
@@ -40,5 +41,8 @@ func generateConf(logLevel models.TinyProxyLogLevel, port uint16, user, password
line := fmt.Sprintf("%s %s", k, v) line := fmt.Sprintf("%s %s", k, v)
lines = append(lines, line) lines = append(lines, line)
} }
sort.Slice(lines, func(i, j int) bool {
return lines[i] < lines[j]
})
return lines return lines
} }

View File

@@ -0,0 +1,68 @@
package tinyproxy
import (
"testing"
"github.com/qdm12/private-internet-access-docker/internal/constants"
"github.com/qdm12/private-internet-access-docker/internal/models"
"github.com/stretchr/testify/assert"
)
func Test_generateConf(t *testing.T) {
t.Parallel()
tests := map[string]struct {
logLevel models.TinyProxyLogLevel
port uint16
user string
password string
lines []string
}{
"No credentials": {
logLevel: constants.TinyProxyInfoLevel,
port: 2000,
lines: []string{
"DefaultErrorFile \"/usr/share/tinyproxy/default.html\"",
"DisableViaHeader Yes",
"Group tinyproxy",
"LogLevel Info",
"MaxClients 100",
"MaxRequestsPerChild 0",
"MaxSpareServers 20",
"MinSpareServers 5",
"Port 2000",
"StartServers 10",
"Timeout 600",
"User nonrootuser",
},
},
"With credentials": {
logLevel: constants.TinyProxyErrorLevel,
port: 2000,
user: "abc",
password: "def",
lines: []string{
"BasicAuth abc def",
"DefaultErrorFile \"/usr/share/tinyproxy/default.html\"",
"DisableViaHeader Yes",
"Group tinyproxy",
"LogLevel Error",
"MaxClients 100",
"MaxRequestsPerChild 0",
"MaxSpareServers 20",
"MinSpareServers 5",
"Port 2000",
"StartServers 10",
"Timeout 600",
"User nonrootuser",
},
},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
lines := generateConf(tc.logLevel, tc.port, tc.user, tc.password)
assert.Equal(t, tc.lines, lines)
})
}
}