diff options
Diffstat (limited to 'service/gzip')
-rw-r--r-- | service/gzip/config.go | 22 | ||||
-rw-r--r-- | service/gzip/config_test.go | 47 | ||||
-rw-r--r-- | service/gzip/service.go | 38 | ||||
-rw-r--r-- | service/gzip/service_test.go | 73 |
4 files changed, 0 insertions, 180 deletions
diff --git a/service/gzip/config.go b/service/gzip/config.go deleted file mode 100644 index 00ac559d..00000000 --- a/service/gzip/config.go +++ /dev/null @@ -1,22 +0,0 @@ -package gzip - -import ( - "github.com/spiral/roadrunner/service" -) - -// Config describes file location and controls access to them. -type Config struct { - Enable bool -} - -// Hydrate must populate Config values using given Config source. Must return error if Config is not valid. -func (c *Config) Hydrate(cfg service.Config) error { - return cfg.Unmarshal(c) -} - -// InitDefaults sets missing values to their default values. -func (c *Config) InitDefaults() error { - c.Enable = true - - return nil -} diff --git a/service/gzip/config_test.go b/service/gzip/config_test.go deleted file mode 100644 index 8d03aecf..00000000 --- a/service/gzip/config_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package gzip - -import ( - "testing" - - json "github.com/json-iterator/go" - "github.com/spiral/roadrunner/service" - "github.com/stretchr/testify/assert" -) - -type mockCfg struct{ cfg string } - -func (cfg *mockCfg) Get(name string) service.Config { return nil } -func (cfg *mockCfg) Unmarshal(out interface{}) error { - j := json.ConfigCompatibleWithStandardLibrary - return j.Unmarshal([]byte(cfg.cfg), out) -} - -func Test_Config_Hydrate(t *testing.T) { - cfg := &mockCfg{`{"enable": true}`} - c := &Config{} - - assert.NoError(t, c.Hydrate(cfg)) -} - -func Test_Config_Hydrate_Error(t *testing.T) { - cfg := &mockCfg{`{"enable": "invalid"}`} - c := &Config{} - - assert.Error(t, c.Hydrate(cfg)) -} - -func Test_Config_Hydrate_Error2(t *testing.T) { - cfg := &mockCfg{`{"enable": 1}`} - c := &Config{} - - assert.Error(t, c.Hydrate(cfg)) -} - -func Test_Config_Defaults(t *testing.T) { - c := &Config{} - err := c.InitDefaults() - if err != nil { - t.Errorf("error during the InitDefaults: error %v", err) - } - assert.Equal(t, true, c.Enable) -} diff --git a/service/gzip/service.go b/service/gzip/service.go deleted file mode 100644 index 2ba95158..00000000 --- a/service/gzip/service.go +++ /dev/null @@ -1,38 +0,0 @@ -package gzip - -import ( - "errors" - "net/http" - - "github.com/NYTimes/gziphandler" - rrhttp "github.com/spiral/roadrunner/service/http" -) - -// ID contains default service name. -const ID = "gzip" - -var httpNotInitialized = errors.New("http service should be defined properly in config to use gzip") - -type Service struct { - cfg *Config -} - -func (s *Service) Init(cfg *Config, r *rrhttp.Service) (bool, error) { - s.cfg = cfg - if !s.cfg.Enable { - return false, nil - } - if r == nil { - return false, httpNotInitialized - } - - r.AddMiddleware(s.middleware) - - return true, nil -} - -func (s *Service) middleware(f http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - gziphandler.GzipHandler(f).ServeHTTP(w, r) - } -} diff --git a/service/gzip/service_test.go b/service/gzip/service_test.go deleted file mode 100644 index d886a339..00000000 --- a/service/gzip/service_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package gzip - -import ( - "testing" - - json "github.com/json-iterator/go" - "github.com/sirupsen/logrus" - "github.com/sirupsen/logrus/hooks/test" - "github.com/spiral/roadrunner/service" - rrhttp "github.com/spiral/roadrunner/service/http" - "github.com/stretchr/testify/assert" -) - -type testCfg struct { - gzip string - httpCfg string - target string -} - -func (cfg *testCfg) Get(name string) service.Config { - if name == rrhttp.ID { - return &testCfg{target: cfg.httpCfg} - } - - if name == ID { - return &testCfg{target: cfg.gzip} - } - return nil -} -func (cfg *testCfg) Unmarshal(out interface{}) error { - j := json.ConfigCompatibleWithStandardLibrary - return j.Unmarshal([]byte(cfg.target), out) -} - -func Test_Disabled(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(ID, &Service{cfg: &Config{Enable: true}}) - - assert.NoError(t, c.Init(&testCfg{ - httpCfg: `{ - "address": ":6029", - "workers":{ - "command": "php ../../tests/http/client.php echo pipes", - } - }`, - gzip: `{"enable":false}`, - })) - - s, st := c.Get(ID) - assert.NotNil(t, s) - assert.Equal(t, service.StatusInactive, st) -} - -// TEST bug #275 -func Test_Bug275(t *testing.T) { - logger, _ := test.NewNullLogger() - logger.SetLevel(logrus.DebugLevel) - - c := service.NewContainer(logger) - c.Register(ID, &Service{}) - - assert.Error(t, c.Init(&testCfg{ - httpCfg: "", - gzip: `{"enable":true}`, - })) - - s, st := c.Get(ID) - assert.NotNil(t, s) - assert.Equal(t, service.StatusInactive, st) -} |