diff options
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/config_test.go | 2 | ||||
-rw-r--r-- | service/http/handler_test.go | 2 | ||||
-rw-r--r-- | service/http/request.go | 6 | ||||
-rw-r--r-- | service/http/service.go | 9 | ||||
-rw-r--r-- | service/http/service_test.go | 159 |
5 files changed, 172 insertions, 6 deletions
diff --git a/service/http/config_test.go b/service/http/config_test.go index 18e8ab3e..b806b79b 100644 --- a/service/http/config_test.go +++ b/service/http/config_test.go @@ -64,7 +64,7 @@ func Test_Config_NoWorkers(t *testing.T) { assert.Error(t, cfg.Valid()) } -func Test_Confi_InvalidAddress(t *testing.T) { +func Test_Config_InvalidAddress(t *testing.T) { cfg := &Config{ Enable: true, Address: "", diff --git a/service/http/handler_test.go b/service/http/handler_test.go index 0e236df0..98379117 100644 --- a/service/http/handler_test.go +++ b/service/http/handler_test.go @@ -58,7 +58,7 @@ func TestServer_Echo(t *testing.T) { body, r, err := get("http://localhost:8077/?hello=world") assert.NoError(t, err) - assert.Equal(t, 200, r.StatusCode) + assert.Equal(t, 201, r.StatusCode) assert.Equal(t, "WORLD", body) } diff --git a/service/http/request.go b/service/http/request.go index 80998b11..e02d3cdb 100644 --- a/service/http/request.go +++ b/service/http/request.go @@ -12,7 +12,7 @@ import ( const ( defaultMaxMemory = 32 << 20 // 32 MB contentNone = iota + 900 - contentUndefined + contentStream contentMultipart contentFormData ) @@ -66,7 +66,7 @@ func NewRequest(r *http.Request, cfg *UploadsConfig) (req *Request, err error) { case contentNone: return req, nil - case contentUndefined: + case contentStream: req.body, err = ioutil.ReadAll(r.Body) return req, err @@ -142,7 +142,7 @@ func (r *Request) contentType() int { return contentMultipart } - return contentUndefined + return contentStream } // uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled). diff --git a/service/http/service.go b/service/http/service.go index 881c862d..2b30cd1c 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -6,6 +6,7 @@ import ( "context" "github.com/spiral/roadrunner" "github.com/spiral/roadrunner/service/rpc" + "sync" ) // Name contains default svc name. @@ -18,8 +19,10 @@ type middleware func(w http.ResponseWriter, r *http.Request) bool type Service struct { cfg *Config lsns []func(event int, ctx interface{}) - rr *roadrunner.Server mdws []middleware + + mu sync.Mutex + rr *roadrunner.Server srv *Handler http *http.Server } @@ -63,6 +66,7 @@ func (s *Service) Init(cfg service.Config, c service.Container) (bool, error) { // Serve serves the svc. func (s *Service) Serve() error { + s.mu.Lock() rr := roadrunner.NewServer(s.cfg.Workers) s.rr = rr @@ -77,6 +81,7 @@ func (s *Service) Serve() error { } else { s.http.Handler = s } + s.mu.Unlock() if err := rr.Start(); err != nil { return err @@ -92,6 +97,8 @@ func (s *Service) Serve() error { // Stop stops the svc. func (s *Service) Stop() { + s.mu.Lock() + defer s.mu.Unlock() if s.http == nil { return } diff --git a/service/http/service_test.go b/service/http/service_test.go new file mode 100644 index 00000000..08dc8a5b --- /dev/null +++ b/service/http/service_test.go @@ -0,0 +1,159 @@ +package http + +import ( + "testing" + "github.com/spiral/roadrunner/service" + "github.com/sirupsen/logrus/hooks/test" + "github.com/sirupsen/logrus" + "encoding/json" + "github.com/stretchr/testify/assert" + "os" + "time" + "net/http" + "io/ioutil" +) + +type testCfg struct{ httpCfg string } + +func (cfg *testCfg) Get(name string) service.Config { + if name == Name { + return &testCfg{cfg.httpCfg} + } + return nil +} +func (cfg *testCfg) Unmarshal(out interface{}) error { + json.Unmarshal([]byte(cfg.httpCfg), out) + return nil +} + +func Test_Service_NoConfig(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(Name, &Service{}) + + assert.NoError(t, c.Init(&testCfg{`{}`})) + + s, st := c.Get(Name) + assert.NotNil(t, s) + assert.Equal(t, service.StatusRegistered, st) +} + +func Test_Service_Configure_Disable(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(Name, &Service{}) + + assert.NoError(t, c.Init(&testCfg{`{ + "enable": false, + "address": ":8070", + "maxRequest": 1024, + "uploads": { + "dir": ` + tmpDir() + `, + "forbid": [] + }, + "workers":{ + "command": "php ../../php-src/tests/http/client.php echo pipes", + "relay": "pipes", + "pool": { + "numWorkers": 1, + "allocateTimeout": 10000000, + "destroyTimeout": 10000000 + } + } + }`})) + + s, st := c.Get(Name) + assert.NotNil(t, s) + assert.Equal(t, service.StatusRegistered, st) +} + +func Test_Service_Configure_Enable(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(Name, &Service{}) + + assert.NoError(t, c.Init(&testCfg{`{ + "enable": true, + "address": ":8070", + "maxRequest": 1024, + "uploads": { + "dir": ` + tmpDir() + `, + "forbid": [] + }, + "workers":{ + "command": "php ../../php-src/tests/http/client.php echo pipes", + "relay": "pipes", + "pool": { + "numWorkers": 1, + "allocateTimeout": 10000000, + "destroyTimeout": 10000000 + } + } + }`})) + + s, st := c.Get(Name) + assert.NotNil(t, s) + assert.Equal(t, service.StatusConfigured, st) +} + +func Test_Service_Echo(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(Name, &Service{}) + + assert.NoError(t, c.Init(&testCfg{`{ + "enable": true, + "address": ":8070", + "maxRequest": 1024, + "uploads": { + "dir": ` + tmpDir() + `, + "forbid": [] + }, + "workers":{ + "command": "php ../../php-src/tests/http/client.php echo pipes", + "relay": "pipes", + "pool": { + "numWorkers": 1, + "allocateTimeout": 10000000, + "destroyTimeout": 10000000 + } + } + }`})) + + s, st := c.Get(Name) + assert.NotNil(t, s) + assert.Equal(t, service.StatusConfigured, st) + + go func() { c.Serve() }() + time.Sleep(time.Millisecond * 10) + defer c.Stop() + + req, err := http.NewRequest("GET", "http://localhost:8070?hello=world", nil) + assert.NoError(t, err) + + r, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + defer r.Body.Close() + + b, err := ioutil.ReadAll(r.Body) + assert.NoError(t, err) + + assert.NoError(t, err) + assert.Equal(t, 201, r.StatusCode) + assert.Equal(t, "WORLD", string(b)) +} + +func tmpDir() string { + p := os.TempDir() + r, _ := json.Marshal(p) + + return string(r) +} |