mirror of
https://github.com/halverneus/static-file-server.git
synced 2026-05-28 06:30:03 -04:00
Added unit tests to referer config option and fixed bug where YAML options would be ignored.
This commit is contained in:
parent
000acc0bf2
commit
3b5291c19e
@ -14,14 +14,14 @@ import (
|
|||||||
var (
|
var (
|
||||||
// Get the desired configuration value.
|
// Get the desired configuration value.
|
||||||
Get struct {
|
Get struct {
|
||||||
Debug bool `yaml:"debug"`
|
Debug bool `yaml:"debug"`
|
||||||
Folder string `yaml:"folder"`
|
Folder string `yaml:"folder"`
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port uint16 `yaml:"port"`
|
Port uint16 `yaml:"port"`
|
||||||
ShowListing bool `yaml:"show-listing"`
|
ShowListing bool `yaml:"show-listing"`
|
||||||
TLSCert string `yaml:"tls-cert"`
|
TLSCert string `yaml:"tls-cert"`
|
||||||
TLSKey string `yaml:"tls-key"`
|
TLSKey string `yaml:"tls-key"`
|
||||||
URLPrefix string `yaml:"url-prefix"`
|
URLPrefix string `yaml:"url-prefix"`
|
||||||
Referrers []string `yaml:"referrers"`
|
Referrers []string `yaml:"referrers"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -31,18 +31,19 @@ const (
|
|||||||
folderKey = "FOLDER"
|
folderKey = "FOLDER"
|
||||||
hostKey = "HOST"
|
hostKey = "HOST"
|
||||||
portKey = "PORT"
|
portKey = "PORT"
|
||||||
|
referrersKey = "REFERRERS"
|
||||||
showListingKey = "SHOW_LISTING"
|
showListingKey = "SHOW_LISTING"
|
||||||
tlsCertKey = "TLS_CERT"
|
tlsCertKey = "TLS_CERT"
|
||||||
tlsKeyKey = "TLS_KEY"
|
tlsKeyKey = "TLS_KEY"
|
||||||
urlPrefixKey = "URL_PREFIX"
|
urlPrefixKey = "URL_PREFIX"
|
||||||
referrersKey = "REFERRERS"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
defaultDebug = false
|
defaultDebug = false
|
||||||
defaultFolder = "/web"
|
defaultFolder = "/web"
|
||||||
defaultHost = ""
|
defaultHost = ""
|
||||||
defaultPort = uint16(8080)
|
defaultPort = uint16(8080)
|
||||||
|
defaultReferrers = []string{}
|
||||||
defaultShowListing = true
|
defaultShowListing = true
|
||||||
defaultTLSCert = ""
|
defaultTLSCert = ""
|
||||||
defaultTLSKey = ""
|
defaultTLSKey = ""
|
||||||
@ -59,11 +60,11 @@ func setDefaults() {
|
|||||||
Get.Folder = defaultFolder
|
Get.Folder = defaultFolder
|
||||||
Get.Host = defaultHost
|
Get.Host = defaultHost
|
||||||
Get.Port = defaultPort
|
Get.Port = defaultPort
|
||||||
|
Get.Referrers = defaultReferrers
|
||||||
Get.ShowListing = defaultShowListing
|
Get.ShowListing = defaultShowListing
|
||||||
Get.TLSCert = defaultTLSCert
|
Get.TLSCert = defaultTLSCert
|
||||||
Get.TLSKey = defaultTLSKey
|
Get.TLSKey = defaultTLSKey
|
||||||
Get.URLPrefix = defaultURLPrefix
|
Get.URLPrefix = defaultURLPrefix
|
||||||
Get.Referrers = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the configuration file.
|
// Load the configuration file.
|
||||||
@ -111,7 +112,7 @@ func overrideWithEnvVars() {
|
|||||||
Get.TLSCert = envAsStr(tlsCertKey, Get.TLSCert)
|
Get.TLSCert = envAsStr(tlsCertKey, Get.TLSCert)
|
||||||
Get.TLSKey = envAsStr(tlsKeyKey, Get.TLSKey)
|
Get.TLSKey = envAsStr(tlsKeyKey, Get.TLSKey)
|
||||||
Get.URLPrefix = envAsStr(urlPrefixKey, Get.URLPrefix)
|
Get.URLPrefix = envAsStr(urlPrefixKey, Get.URLPrefix)
|
||||||
Get.Referrers = strAsArray(envAsStr(referrersKey, ""))
|
Get.Referrers = envAsStrSlice(referrersKey, Get.Referrers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate the configuration.
|
// validate the configuration.
|
||||||
@ -154,6 +155,15 @@ func envAsStr(key, fallback string) string {
|
|||||||
return fallback
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// envAsStrSlice returns the value of the environment variable as a slice of
|
||||||
|
// strings if set.
|
||||||
|
func envAsStrSlice(key string, fallback []string) []string {
|
||||||
|
if value := os.Getenv(key); "" != value {
|
||||||
|
return strings.Split(value, ",")
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
// envAsUint16 returns the value of the environment variable as a uint16 if set.
|
// envAsUint16 returns the value of the environment variable as a uint16 if set.
|
||||||
func envAsUint16(key string, fallback uint16) uint16 {
|
func envAsUint16(key string, fallback uint16) uint16 {
|
||||||
// Retrieve the string value of the environment variable. If not set,
|
// Retrieve the string value of the environment variable. If not set,
|
||||||
@ -215,11 +225,3 @@ func strAsBool(value string) (result bool, err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func strAsArray(value string) (result []string) {
|
|
||||||
if (value == "") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
result = strings.Split(value, ",");
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
func TestLoad(t *testing.T) {
|
||||||
@ -245,6 +245,98 @@ func TestEnvAsStr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEnvAsStrSlice(t *testing.T) {
|
||||||
|
oe := "ONE_ENTRY"
|
||||||
|
oewc := "ONE_ENTRY_WITH_COMMA"
|
||||||
|
oewtc := "ONE_ENTRY_WITH_TRAILING_COMMA"
|
||||||
|
te := "TWO_ENTRY"
|
||||||
|
tewc := "TWO_ENTRY_WITH_COMMA"
|
||||||
|
oc := "ONLY_COMMA"
|
||||||
|
ev := "EMPTY_VALUE"
|
||||||
|
uv := "UNSET_VALUE"
|
||||||
|
|
||||||
|
fs := "http://my.site"
|
||||||
|
ts := "http://other.site"
|
||||||
|
fbr := []string{"one", "two"}
|
||||||
|
var efbr []string
|
||||||
|
|
||||||
|
oes := fs
|
||||||
|
oer := []string{fs}
|
||||||
|
oewcs := "," + fs
|
||||||
|
oewcr := []string{"", fs}
|
||||||
|
oewtcs := fs + ","
|
||||||
|
oewtcr := []string{fs, ""}
|
||||||
|
tes := fs + "," + ts
|
||||||
|
ter := []string{fs, ts}
|
||||||
|
tewcs := "," + fs + "," + ts
|
||||||
|
tewcr := []string{"", fs, ts}
|
||||||
|
ocs := ","
|
||||||
|
ocr := []string{"", ""}
|
||||||
|
evs := ""
|
||||||
|
|
||||||
|
os.Setenv(oe, oes)
|
||||||
|
os.Setenv(oewc, oewcs)
|
||||||
|
os.Setenv(oewtc, oewtcs)
|
||||||
|
os.Setenv(te, tes)
|
||||||
|
os.Setenv(tewc, tewcs)
|
||||||
|
os.Setenv(oc, ocs)
|
||||||
|
os.Setenv(ev, evs)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
key string
|
||||||
|
fallback []string
|
||||||
|
result []string
|
||||||
|
}{
|
||||||
|
{"One entry", oe, fbr, oer},
|
||||||
|
{"One entry w/comma", oewc, fbr, oewcr},
|
||||||
|
{"One entry w/trailing comma", oewtc, fbr, oewtcr},
|
||||||
|
{"Two entry", te, fbr, ter},
|
||||||
|
{"Two entry w/comma", tewc, fbr, tewcr},
|
||||||
|
{"Only comma", oc, fbr, ocr},
|
||||||
|
{"Empty value w/fallback", ev, fbr, fbr},
|
||||||
|
{"Empty value wo/fallback", ev, efbr, efbr},
|
||||||
|
{"Unset w/fallback", uv, fbr, fbr},
|
||||||
|
{"Unset wo/fallback", uv, efbr, efbr},
|
||||||
|
}
|
||||||
|
|
||||||
|
matches := func(a, b []string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
tally := make(map[int]bool)
|
||||||
|
for i := range a {
|
||||||
|
tally[i] = false
|
||||||
|
}
|
||||||
|
for _, val := range a {
|
||||||
|
for i, other := range b {
|
||||||
|
if other == val && !tally[i] {
|
||||||
|
tally[i] = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, found := range tally {
|
||||||
|
if !found {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
result := envAsStrSlice(tc.key, tc.fallback)
|
||||||
|
if !matches(tc.result, result) {
|
||||||
|
t.Errorf(
|
||||||
|
"For %s with a '%v' fallback expected '%v' but got '%v'",
|
||||||
|
tc.key, tc.fallback, tc.result, result,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnvAsUint16(t *testing.T) {
|
func TestEnvAsUint16(t *testing.T) {
|
||||||
ubv := "UPPER_BOUNDS_VALUE"
|
ubv := "UPPER_BOUNDS_VALUE"
|
||||||
lbv := "LOWER_BOUNDS_VALUE"
|
lbv := "LOWER_BOUNDS_VALUE"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user