diff options
author | Wolfy-J <[email protected]> | 2018-06-13 14:27:07 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-13 14:27:07 +0300 |
commit | 5a1a3844cbd23dac33f96059c686d9136656a02b (patch) | |
tree | 5e10e2bdfa8d5cd84007d4a2dfe2ed7281ec8a50 /service | |
parent | fe01e08efcb154ad0f5d169eb4cae514b5507259 (diff) |
config validation
Diffstat (limited to 'service')
-rw-r--r-- | service/http/config.go | 14 | ||||
-rw-r--r-- | service/http/config_test.go | 88 |
2 files changed, 102 insertions, 0 deletions
diff --git a/service/http/config.go b/service/http/config.go index 3db172b1..2bc5f845 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -2,6 +2,8 @@ package http import ( "github.com/spiral/roadrunner" + "errors" + "strings" ) // Configures RoadRunner HTTP server. @@ -24,5 +26,17 @@ type Config struct { // Valid validates the configuration. func (cfg *Config) Valid() error { + if cfg.Uploads == nil { + return errors.New("mailformed uploads config") + } + + if cfg.Workers == nil { + return errors.New("mailformed workers config") + } + + if !strings.Contains(cfg.Address, ":") { + return errors.New("mailformed server address") + } + return nil } diff --git a/service/http/config_test.go b/service/http/config_test.go new file mode 100644 index 00000000..18e8ab3e --- /dev/null +++ b/service/http/config_test.go @@ -0,0 +1,88 @@ +package http + +import ( + "testing" + "os" + "github.com/stretchr/testify/assert" + "github.com/spiral/roadrunner" + "time" +) + +func Test_Config_Valid(t *testing.T) { + cfg := &Config{ + Enable: true, + Address: ":8080", + MaxRequest: 1024, + Uploads: &UploadsConfig{ + Dir: os.TempDir(), + Forbid: []string{".go"}, + }, + Workers: &roadrunner.ServerConfig{ + Command: "php php-src/tests/client.php echo pipes", + Relay: "pipes", + Pool: &roadrunner.Config{ + NumWorkers: 1, + AllocateTimeout: time.Second, + DestroyTimeout: time.Second, + }, + }, + } + + assert.NoError(t, cfg.Valid()) +} + +func Test_Config_NoUploads(t *testing.T) { + cfg := &Config{ + Enable: true, + Address: ":8080", + MaxRequest: 1024, + Workers: &roadrunner.ServerConfig{ + Command: "php php-src/tests/client.php echo pipes", + Relay: "pipes", + Pool: &roadrunner.Config{ + NumWorkers: 1, + AllocateTimeout: time.Second, + DestroyTimeout: time.Second, + }, + }, + } + + assert.Error(t, cfg.Valid()) +} + +func Test_Config_NoWorkers(t *testing.T) { + cfg := &Config{ + Enable: true, + Address: ":8080", + MaxRequest: 1024, + Uploads: &UploadsConfig{ + Dir: os.TempDir(), + Forbid: []string{".go"}, + }, + } + + assert.Error(t, cfg.Valid()) +} + +func Test_Confi_InvalidAddress(t *testing.T) { + cfg := &Config{ + Enable: true, + Address: "", + MaxRequest: 1024, + Uploads: &UploadsConfig{ + Dir: os.TempDir(), + Forbid: []string{".go"}, + }, + Workers: &roadrunner.ServerConfig{ + Command: "php php-src/tests/client.php echo pipes", + Relay: "pipes", + Pool: &roadrunner.Config{ + NumWorkers: 1, + AllocateTimeout: time.Second, + DestroyTimeout: time.Second, + }, + }, + } + + assert.Error(t, cfg.Valid()) +} |