2021-11-29 17:16:27 +00:00
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-05-01 17:47:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-29 17:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseMandatoryFlags(t *testing.T) {
|
|
|
|
var mandatoryFlags = []struct {
|
|
|
|
args []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-geoip2-city", "/city-path"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-geoip2-asn", "/asn-path"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
|
|
|
"-tls-crt", "/crt-path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
|
|
|
"-tls-key", "/key-path",
|
|
|
|
},
|
2022-05-01 17:47:27 +00:00
|
|
|
},
|
2023-03-18 19:38:18 +00:00
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-enable-http3",
|
|
|
|
},
|
|
|
|
},
|
2022-05-01 17:47:27 +00:00
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-bind", ":8000",
|
|
|
|
"-trusted-port-header", "port-header",
|
|
|
|
},
|
2021-11-29 17:16:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range mandatoryFlags {
|
|
|
|
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
|
|
|
|
_, err := Setup(tt.args)
|
2022-05-01 17:47:27 +00:00
|
|
|
require.NotNil(t, err)
|
2021-11-29 17:16:27 +00:00
|
|
|
assert.Contains(t, err.Error(), "mandatory")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFlags(t *testing.T) {
|
|
|
|
var flags = []struct {
|
|
|
|
args []string
|
|
|
|
conf settings
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]string{"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path"},
|
|
|
|
settings{
|
|
|
|
GeodbPath: geodbPath{
|
|
|
|
City: "/city-path",
|
|
|
|
ASN: "/asn-path",
|
|
|
|
},
|
2022-05-01 17:47:27 +00:00
|
|
|
BindAddress: ":8080",
|
2021-11-29 17:16:27 +00:00
|
|
|
Server: serverSettings{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-bind", ":8001", "-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path"},
|
|
|
|
settings{
|
|
|
|
GeodbPath: geodbPath{
|
|
|
|
City: "/city-path",
|
|
|
|
ASN: "/asn-path",
|
|
|
|
},
|
2022-05-01 17:47:27 +00:00
|
|
|
BindAddress: ":8001",
|
2021-11-29 17:16:27 +00:00
|
|
|
Server: serverSettings{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path", "-tls-bind", ":9000",
|
|
|
|
"-tls-crt", "/crt-path", "-tls-key", "/key-path",
|
|
|
|
},
|
|
|
|
settings{
|
|
|
|
GeodbPath: geodbPath{
|
|
|
|
City: "/city-path",
|
|
|
|
ASN: "/asn-path",
|
|
|
|
},
|
2022-05-01 17:47:27 +00:00
|
|
|
BindAddress: ":8080",
|
|
|
|
TLSAddress: ":9000",
|
|
|
|
TLSCrtPath: "/crt-path",
|
|
|
|
TLSKeyPath: "/key-path",
|
|
|
|
Server: serverSettings{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
|
|
|
"-trusted-header", "header", "-trusted-port-header", "port-header",
|
|
|
|
},
|
|
|
|
settings{
|
|
|
|
GeodbPath: geodbPath{
|
|
|
|
City: "/city-path",
|
|
|
|
ASN: "/asn-path",
|
|
|
|
},
|
|
|
|
BindAddress: ":8080",
|
|
|
|
TrustedHeader: "header",
|
|
|
|
TrustedPortHeader: "port-header",
|
2021-11-29 17:16:27 +00:00
|
|
|
Server: serverSettings{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
2022-04-02 16:10:48 +00:00
|
|
|
"-trusted-header", "header", "-enable-secure-headers",
|
2021-11-29 17:16:27 +00:00
|
|
|
},
|
|
|
|
settings{
|
|
|
|
GeodbPath: geodbPath{
|
|
|
|
City: "/city-path",
|
|
|
|
ASN: "/asn-path",
|
|
|
|
},
|
2022-04-02 16:10:48 +00:00
|
|
|
BindAddress: ":8080",
|
|
|
|
TrustedHeader: "header",
|
|
|
|
EnableSecureHeaders: true,
|
2021-11-29 17:16:27 +00:00
|
|
|
Server: serverSettings{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range flags {
|
|
|
|
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
|
|
|
|
_, err := Setup(tt.args)
|
2022-05-01 17:47:27 +00:00
|
|
|
require.Nil(t, err)
|
2021-11-29 17:16:27 +00:00
|
|
|
assert.True(t, reflect.DeepEqual(App, tt.conf))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFlagsUsage(t *testing.T) {
|
|
|
|
var usageArgs = []string{"-help", "-h", "--help"}
|
|
|
|
|
|
|
|
for _, arg := range usageArgs {
|
|
|
|
t.Run(arg, func(t *testing.T) {
|
|
|
|
output, err := Setup([]string{arg})
|
|
|
|
assert.ErrorIs(t, err, flag.ErrHelp)
|
|
|
|
assert.Contains(t, output, "Usage of")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFlagVersion(t *testing.T) {
|
|
|
|
output, err := Setup([]string{"-version"})
|
|
|
|
assert.ErrorIs(t, err, ErrVersion)
|
|
|
|
assert.Contains(t, output, "whatismyip version")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseFlagTemplate(t *testing.T) {
|
|
|
|
flags := []string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
|
|
|
"-template", "/template-path",
|
|
|
|
}
|
|
|
|
_, err := Setup(flags)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "no such file or directory")
|
|
|
|
|
|
|
|
flags = []string{
|
|
|
|
"-geoip2-city", "/city-path", "-geoip2-asn", "/asn-path",
|
|
|
|
"-template", "/",
|
|
|
|
}
|
|
|
|
_, err = Setup(flags)
|
2022-05-01 17:47:27 +00:00
|
|
|
require.Error(t, err)
|
2021-11-29 17:16:27 +00:00
|
|
|
assert.Contains(t, err.Error(), "must be a file")
|
|
|
|
}
|