lib/http: rationalise names in test servers to be more consistent

This commit is contained in:
Tom Mombourquette 2022-11-08 08:03:26 -04:00 committed by Nick Craig-Wood
parent 6d62267227
commit 2a2fcf1012
7 changed files with 56 additions and 53 deletions

View File

@ -29,14 +29,14 @@ import (
// Options required for http server // Options required for http server
type Options struct { type Options struct {
Auth libhttp.AuthConfig Auth libhttp.AuthConfig
HTTP libhttp.HTTPConfig HTTP libhttp.Config
Template libhttp.TemplateConfig Template libhttp.TemplateConfig
} }
// DefaultOpt is the default values used for Options // DefaultOpt is the default values used for Options
var DefaultOpt = Options{ var DefaultOpt = Options{
Auth: libhttp.DefaultAuthCfg(), Auth: libhttp.DefaultAuthCfg(),
HTTP: libhttp.DefaultHTTPCfg(), HTTP: libhttp.DefaultCfg(),
Template: libhttp.DefaultTemplateCfg(), Template: libhttp.DefaultTemplateCfg(),
} }

View File

@ -34,7 +34,7 @@ func start(t *testing.T, f fs.Fs) {
ctx := context.Background() ctx := context.Background()
opts := Options{ opts := Options{
HTTP: libhttp.DefaultHTTPCfg(), HTTP: libhttp.DefaultCfg(),
Template: libhttp.TemplateConfig{ Template: libhttp.TemplateConfig{
Path: testTemplate, Path: testTemplate,
}, },

View File

@ -5,8 +5,7 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
// Help contains text describing the http authentication to add to the command // AuthHelp contains text describing the http authentication to add to the command help.
// help.
var AuthHelp = ` var AuthHelp = `
#### Authentication #### Authentication

View File

@ -33,6 +33,7 @@ func parseAuthorization(r *http.Request) (user, pass string, ok bool) {
return return
} }
// LoggedBasicAuth simply wraps the goauth.BasicAuth struct
type LoggedBasicAuth struct { type LoggedBasicAuth struct {
goauth.BasicAuth goauth.BasicAuth
} }

View File

@ -13,14 +13,14 @@ import (
func TestMiddlewareAuth(t *testing.T) { func TestMiddlewareAuth(t *testing.T) {
servers := []struct { servers := []struct {
name string name string
http HTTPConfig http Config
auth AuthConfig auth AuthConfig
user string user string
pass string pass string
}{ }{
{ {
name: "Basic", name: "Basic",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
auth: AuthConfig{ auth: AuthConfig{
@ -33,7 +33,7 @@ func TestMiddlewareAuth(t *testing.T) {
}, },
{ {
name: "Htpasswd/MD5", name: "Htpasswd/MD5",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
auth: AuthConfig{ auth: AuthConfig{
@ -45,7 +45,7 @@ func TestMiddlewareAuth(t *testing.T) {
}, },
{ {
name: "Htpasswd/SHA", name: "Htpasswd/SHA",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
auth: AuthConfig{ auth: AuthConfig{
@ -57,7 +57,7 @@ func TestMiddlewareAuth(t *testing.T) {
}, },
{ {
name: "Htpasswd/Bcrypt", name: "Htpasswd/Bcrypt",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
auth: AuthConfig{ auth: AuthConfig{
@ -69,7 +69,7 @@ func TestMiddlewareAuth(t *testing.T) {
}, },
{ {
name: "Custom", name: "Custom",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
auth: AuthConfig{ auth: AuthConfig{
@ -168,19 +168,19 @@ var _testCORSHeaderKeys = []string{
func TestMiddlewareCORS(t *testing.T) { func TestMiddlewareCORS(t *testing.T) {
servers := []struct { servers := []struct {
name string name string
http HTTPConfig http Config
origin string origin string
}{ }{
{ {
name: "EmptyOrigin", name: "EmptyOrigin",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
origin: "", origin: "",
}, },
{ {
name: "CustomOrigin", name: "CustomOrigin",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
}, },
origin: "http://test.rclone.org", origin: "http://test.rclone.org",

View File

@ -76,8 +76,8 @@ certificate authority certificate.
// Middleware function signature required by chi.Router.Use() // Middleware function signature required by chi.Router.Use()
type Middleware func(http.Handler) http.Handler type Middleware func(http.Handler) http.Handler
// HTTPConfig contains options for the http Server // Config contains options for the http Server
type HTTPConfig struct { type Config struct {
ListenAddr []string // Port to listen on ListenAddr []string // Port to listen on
BaseURL string // prefix to strip from URLs BaseURL string // prefix to strip from URLs
ServerReadTimeout time.Duration // Timeout for server reading data ServerReadTimeout time.Duration // Timeout for server reading data
@ -93,7 +93,7 @@ type HTTPConfig struct {
} }
// AddFlagsPrefix adds flags for the httplib // AddFlagsPrefix adds flags for the httplib
func (cfg *HTTPConfig) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) { func (cfg *Config) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) {
flags.StringArrayVarP(flagSet, &cfg.ListenAddr, prefix+"addr", "", cfg.ListenAddr, "IPaddress:Port or :Port to bind server to") flags.StringArrayVarP(flagSet, &cfg.ListenAddr, prefix+"addr", "", cfg.ListenAddr, "IPaddress:Port or :Port to bind server to")
flags.DurationVarP(flagSet, &cfg.ServerReadTimeout, prefix+"server-read-timeout", "", cfg.ServerReadTimeout, "Timeout for server reading data") flags.DurationVarP(flagSet, &cfg.ServerReadTimeout, prefix+"server-read-timeout", "", cfg.ServerReadTimeout, "Timeout for server reading data")
flags.DurationVarP(flagSet, &cfg.ServerWriteTimeout, prefix+"server-write-timeout", "", cfg.ServerWriteTimeout, "Timeout for server writing data") flags.DurationVarP(flagSet, &cfg.ServerWriteTimeout, prefix+"server-write-timeout", "", cfg.ServerWriteTimeout, "Timeout for server writing data")
@ -106,13 +106,13 @@ func (cfg *HTTPConfig) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) {
} }
// AddHTTPFlagsPrefix adds flags for the httplib // AddHTTPFlagsPrefix adds flags for the httplib
func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *HTTPConfig) { func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *Config) {
cfg.AddFlagsPrefix(flagSet, prefix) cfg.AddFlagsPrefix(flagSet, prefix)
} }
// DefaultHTTPCfg is the default values used for Config // DefaultCfg is the default values used for Config
func DefaultHTTPCfg() HTTPConfig { func DefaultCfg() Config {
return HTTPConfig{ return Config{
ListenAddr: []string{"127.0.0.1:8080"}, ListenAddr: []string{"127.0.0.1:8080"},
ServerReadTimeout: 1 * time.Hour, ServerReadTimeout: 1 * time.Hour,
ServerWriteTimeout: 1 * time.Hour, ServerWriteTimeout: 1 * time.Hour,
@ -151,7 +151,7 @@ type server struct {
tlsConfig *tls.Config tlsConfig *tls.Config
instances []instance instances []instance
auth AuthConfig auth AuthConfig
cfg HTTPConfig cfg Config
template *TemplateConfig template *TemplateConfig
htmlTemplate *template.Template htmlTemplate *template.Template
} }
@ -166,8 +166,8 @@ func WithAuth(cfg AuthConfig) Option {
} }
} }
// WithConfig option applies the HTTPConfig to the server, overriding defaults // WithConfig option applies the Config to the server, overriding defaults
func WithConfig(cfg HTTPConfig) Option { func WithConfig(cfg Config) Option {
return func(s *server) { return func(s *server) {
s.cfg = cfg s.cfg = cfg
} }
@ -187,7 +187,7 @@ func WithTemplate(cfg TemplateConfig) Option {
func NewServer(ctx context.Context, options ...Option) (*server, error) { func NewServer(ctx context.Context, options ...Option) (*server, error) {
s := &server{ s := &server{
mux: chi.NewRouter(), mux: chi.NewRouter(),
cfg: DefaultHTTPCfg(), cfg: DefaultCfg(),
} }
for _, opt := range options { for _, opt := range options {
@ -307,11 +307,14 @@ func (s *server) initTemplate() error {
} }
var ( var (
// hard coded errors, allowing for easier testing // ErrInvalidMinTLSVersion - hard coded errors, allowing for easier testing
ErrInvalidMinTLSVersion = errors.New("invalid value for --min-tls-version") ErrInvalidMinTLSVersion = errors.New("invalid value for --min-tls-version")
ErrTLSBodyMismatch = errors.New("need both TLSCertBody and TLSKeyBody to use TLS") // ErrTLSBodyMismatch - hard coded errors, allowing for easier testing
ErrTLSFileMismatch = errors.New("need both --cert and --key to use TLS") ErrTLSBodyMismatch = errors.New("need both TLSCertBody and TLSKeyBody to use TLS")
ErrTLSParseCA = errors.New("unable to parse client certificate authority") // ErrTLSFileMismatch - hard coded errors, allowing for easier testing
ErrTLSFileMismatch = errors.New("need both --cert and --key to use TLS")
// ErrTLSParseCA - hard coded errors, allowing for easier testing
ErrTLSParseCA = errors.New("unable to parse client certificate authority")
) )
func (s *server) initTLS() error { func (s *server) initTLS() error {

View File

@ -54,7 +54,7 @@ func TestNewServerUnix(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
path := filepath.Join(tempDir, "rclone.sock") path := filepath.Join(tempDir, "rclone.sock")
cfg := DefaultHTTPCfg() cfg := DefaultCfg()
cfg.ListenAddr = []string{path} cfg.ListenAddr = []string{path}
auth := AuthConfig{ auth := AuthConfig{
@ -97,7 +97,7 @@ func TestNewServerUnix(t *testing.T) {
func TestNewServerHTTP(t *testing.T) { func TestNewServerHTTP(t *testing.T) {
ctx := context.Background() ctx := context.Background()
cfg := DefaultHTTPCfg() cfg := DefaultCfg()
cfg.ListenAddr = []string{"127.0.0.1:0"} cfg.ListenAddr = []string{"127.0.0.1:0"}
auth := AuthConfig{ auth := AuthConfig{
@ -153,12 +153,12 @@ func TestNewServerHTTP(t *testing.T) {
func TestNewServerBaseURL(t *testing.T) { func TestNewServerBaseURL(t *testing.T) {
servers := []struct { servers := []struct {
name string name string
http HTTPConfig cfg Config
suffix string suffix string
}{ }{
{ {
name: "Empty", name: "Empty",
http: HTTPConfig{ cfg: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
BaseURL: "", BaseURL: "",
}, },
@ -166,7 +166,7 @@ func TestNewServerBaseURL(t *testing.T) {
}, },
{ {
name: "Single/NoTrailingSlash", name: "Single/NoTrailingSlash",
http: HTTPConfig{ cfg: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
BaseURL: "/rclone", BaseURL: "/rclone",
}, },
@ -174,7 +174,7 @@ func TestNewServerBaseURL(t *testing.T) {
}, },
{ {
name: "Single/TrailingSlash", name: "Single/TrailingSlash",
http: HTTPConfig{ cfg: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
BaseURL: "/rclone/", BaseURL: "/rclone/",
}, },
@ -182,7 +182,7 @@ func TestNewServerBaseURL(t *testing.T) {
}, },
{ {
name: "Multi/NoTrailingSlash", name: "Multi/NoTrailingSlash",
http: HTTPConfig{ cfg: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
BaseURL: "/rclone/test/base/url", BaseURL: "/rclone/test/base/url",
}, },
@ -190,7 +190,7 @@ func TestNewServerBaseURL(t *testing.T) {
}, },
{ {
name: "Multi/TrailingSlash", name: "Multi/TrailingSlash",
http: HTTPConfig{ cfg: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
BaseURL: "/rclone/test/base/url/", BaseURL: "/rclone/test/base/url/",
}, },
@ -200,7 +200,7 @@ func TestNewServerBaseURL(t *testing.T) {
for _, ss := range servers { for _, ss := range servers {
t.Run(ss.name, func(t *testing.T) { t.Run(ss.name, func(t *testing.T) {
s, err := NewServer(context.Background(), WithConfig(ss.http)) s, err := NewServer(context.Background(), WithConfig(ss.cfg))
require.NoError(t, err) require.NoError(t, err)
defer func() { defer func() {
require.NoError(t, s.Shutdown()) require.NoError(t, s.Shutdown())
@ -246,11 +246,11 @@ func TestNewServerTLS(t *testing.T) {
name string name string
wantErr bool wantErr bool
err error err error
http HTTPConfig http Config
}{ }{
{ {
name: "FromFile/Valid", name: "FromFile/Valid",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCert: "./testdata/local.crt", TLSCert: "./testdata/local.crt",
TLSKey: "./testdata/local.key", TLSKey: "./testdata/local.key",
@ -261,7 +261,7 @@ func TestNewServerTLS(t *testing.T) {
name: "FromFile/NoCert", name: "FromFile/NoCert",
wantErr: true, wantErr: true,
err: ErrTLSFileMismatch, err: ErrTLSFileMismatch,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCert: "", TLSCert: "",
TLSKey: "./testdata/local.key", TLSKey: "./testdata/local.key",
@ -271,7 +271,7 @@ func TestNewServerTLS(t *testing.T) {
{ {
name: "FromFile/InvalidCert", name: "FromFile/InvalidCert",
wantErr: true, wantErr: true,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCert: "./testdata/local.crt.invalid", TLSCert: "./testdata/local.crt.invalid",
TLSKey: "./testdata/local.key", TLSKey: "./testdata/local.key",
@ -282,7 +282,7 @@ func TestNewServerTLS(t *testing.T) {
name: "FromFile/NoKey", name: "FromFile/NoKey",
wantErr: true, wantErr: true,
err: ErrTLSFileMismatch, err: ErrTLSFileMismatch,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCert: "./testdata/local.crt", TLSCert: "./testdata/local.crt",
TLSKey: "", TLSKey: "",
@ -292,7 +292,7 @@ func TestNewServerTLS(t *testing.T) {
{ {
name: "FromFile/InvalidKey", name: "FromFile/InvalidKey",
wantErr: true, wantErr: true,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCert: "./testdata/local.crt", TLSCert: "./testdata/local.crt",
TLSKey: "./testdata/local.key.invalid", TLSKey: "./testdata/local.key.invalid",
@ -301,7 +301,7 @@ func TestNewServerTLS(t *testing.T) {
}, },
{ {
name: "FromBody/Valid", name: "FromBody/Valid",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,
@ -312,7 +312,7 @@ func TestNewServerTLS(t *testing.T) {
name: "FromBody/NoCert", name: "FromBody/NoCert",
wantErr: true, wantErr: true,
err: ErrTLSBodyMismatch, err: ErrTLSBodyMismatch,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: nil, TLSCertBody: nil,
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,
@ -322,7 +322,7 @@ func TestNewServerTLS(t *testing.T) {
{ {
name: "FromBody/InvalidCert", name: "FromBody/InvalidCert",
wantErr: true, wantErr: true,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: []byte("JUNK DATA"), TLSCertBody: []byte("JUNK DATA"),
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,
@ -333,7 +333,7 @@ func TestNewServerTLS(t *testing.T) {
name: "FromBody/NoKey", name: "FromBody/NoKey",
wantErr: true, wantErr: true,
err: ErrTLSBodyMismatch, err: ErrTLSBodyMismatch,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: nil, TLSKeyBody: nil,
@ -343,7 +343,7 @@ func TestNewServerTLS(t *testing.T) {
{ {
name: "FromBody/InvalidKey", name: "FromBody/InvalidKey",
wantErr: true, wantErr: true,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: []byte("JUNK DATA"), TLSKeyBody: []byte("JUNK DATA"),
@ -352,7 +352,7 @@ func TestNewServerTLS(t *testing.T) {
}, },
{ {
name: "MinTLSVersion/Valid/1.1", name: "MinTLSVersion/Valid/1.1",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,
@ -361,7 +361,7 @@ func TestNewServerTLS(t *testing.T) {
}, },
{ {
name: "MinTLSVersion/Valid/1.2", name: "MinTLSVersion/Valid/1.2",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,
@ -370,7 +370,7 @@ func TestNewServerTLS(t *testing.T) {
}, },
{ {
name: "MinTLSVersion/Valid/1.3", name: "MinTLSVersion/Valid/1.3",
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,
@ -381,7 +381,7 @@ func TestNewServerTLS(t *testing.T) {
name: "MinTLSVersion/Invalid", name: "MinTLSVersion/Invalid",
wantErr: true, wantErr: true,
err: ErrInvalidMinTLSVersion, err: ErrInvalidMinTLSVersion,
http: HTTPConfig{ http: Config{
ListenAddr: []string{"127.0.0.1:0"}, ListenAddr: []string{"127.0.0.1:0"},
TLSCertBody: certBytes, TLSCertBody: certBytes,
TLSKeyBody: keyBytes, TLSKeyBody: keyBytes,