summaryrefslogtreecommitdiff
path: root/service/http
diff options
context:
space:
mode:
Diffstat (limited to 'service/http')
-rw-r--r--service/http/config.go24
-rw-r--r--service/http/handler_test.go58
-rw-r--r--service/http/rpc.go4
-rw-r--r--service/http/rpc_test.go7
-rw-r--r--service/http/service.go4
-rw-r--r--service/http/service_test.go4
6 files changed, 78 insertions, 23 deletions
diff --git a/service/http/config.go b/service/http/config.go
index 20a247fb..5be42ae6 100644
--- a/service/http/config.go
+++ b/service/http/config.go
@@ -5,12 +5,11 @@ import (
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
"strings"
- "time"
)
// Config configures RoadRunner HTTP server.
type Config struct {
- // Enable enables http svc.
+ // Enable enables http service.
Enable bool
// Address and port to handle as http server.
@@ -32,25 +31,16 @@ func (c *Config) Hydrate(cfg service.Config) error {
return err
}
- if err := c.Valid(); err != nil {
- return err
+ if !c.Enable {
+ return nil
}
- if c.Workers.Relay == "" {
- c.Workers.Relay = "pipes"
- }
-
- if c.Workers.RelayTimeout < time.Microsecond {
- c.Workers.RelayTimeout = time.Second * time.Duration(c.Workers.RelayTimeout.Nanoseconds())
- }
-
- if c.Workers.Pool.AllocateTimeout < time.Microsecond {
- c.Workers.Pool.AllocateTimeout = time.Second * time.Duration(c.Workers.Pool.AllocateTimeout.Nanoseconds())
+ if err := c.Valid(); err != nil {
+ return err
}
- if c.Workers.Pool.DestroyTimeout < time.Microsecond {
- c.Workers.Pool.DestroyTimeout = time.Second * time.Duration(c.Workers.Pool.DestroyTimeout.Nanoseconds())
- }
+ c.Workers.SetDefaults()
+ c.Workers.UpscaleDurations()
return nil
}
diff --git a/service/http/handler_test.go b/service/http/handler_test.go
index 59a4c7c0..b82fc938 100644
--- a/service/http/handler_test.go
+++ b/service/http/handler_test.go
@@ -14,6 +14,7 @@ import (
"strings"
"testing"
"time"
+ "net/http/httptest"
)
// get request and return body
@@ -63,6 +64,63 @@ func TestServer_Echo(t *testing.T) {
assert.Equal(t, "WORLD", body)
}
+func Test_HandlerErrors(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ wr := httptest.NewRecorder()
+ rq := httptest.NewRequest("POST", "/", bytes.NewBuffer([]byte("data")))
+
+ st.ServeHTTP(wr, rq)
+ assert.Equal(t, 500, wr.Code)
+}
+
+func Test_Handler_JSON_error(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ wr := httptest.NewRecorder()
+ rq := httptest.NewRequest("POST", "/", bytes.NewBuffer([]byte("{sd")))
+ rq.Header.Add("Content-Type","application/json")
+ rq.Header.Add("Content-Size","3")
+
+ st.ServeHTTP(wr, rq)
+ assert.Equal(t, 500, wr.Code)
+}
+
+
func TestServer_Headers(t *testing.T) {
st := &Handler{
cfg: &Config{
diff --git a/service/http/rpc.go b/service/http/rpc.go
index aebc5903..9dfe718e 100644
--- a/service/http/rpc.go
+++ b/service/http/rpc.go
@@ -29,7 +29,7 @@ type Worker struct {
// Reset resets underlying RR worker pool and restarts all of it's workers.
func (rpc *rpcServer) Reset(reset bool, r *string) error {
- if rpc.svc.srv == nil {
+ if rpc.svc == nil || rpc.svc.srv == nil {
return errors.New("http server is not running")
}
@@ -39,7 +39,7 @@ func (rpc *rpcServer) Reset(reset bool, r *string) error {
// Workers returns list of active workers and their stats.
func (rpc *rpcServer) Workers(list bool, r *WorkerList) error {
- if rpc.svc.srv == nil {
+ if rpc.svc == nil || rpc.svc.srv == nil {
return errors.New("http server is not running")
}
diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go
index c392b060..32bb776c 100644
--- a/service/http/rpc_test.go
+++ b/service/http/rpc_test.go
@@ -177,3 +177,10 @@ func Test_Workers(t *testing.T) {
assert.Equal(t, *ss.rr.Workers()[0].Pid, r.Workers[0].Pid)
}
+
+func Test_Errors(t *testing.T) {
+ r := &rpcServer{nil}
+
+ assert.Error(t, r.Reset(true, nil))
+ assert.Error(t, r.Workers(true, nil))
+}
diff --git a/service/http/service.go b/service/http/service.go
index 9f62f5af..f988a843 100644
--- a/service/http/service.go
+++ b/service/http/service.go
@@ -20,7 +20,7 @@ type middleware func(f http.HandlerFunc) http.HandlerFunc
// Service manages rr, http servers.
type Service struct {
cfg *Config
- env env.Provider
+ env env.Environment
lsns []func(event int, ctx interface{})
mdws []middleware
mu sync.Mutex
@@ -42,7 +42,7 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) {
// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
-func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Provider) (bool, error) {
+func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) {
if !cfg.Enable {
return false, nil
}
diff --git a/service/http/service_test.go b/service/http/service_test.go
index 4e572335..8dab7cd4 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -6,6 +6,7 @@ import (
"github.com/sirupsen/logrus/hooks/test"
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
+ "github.com/spiral/roadrunner/service/env"
"github.com/spiral/roadrunner/service/rpc"
"github.com/stretchr/testify/assert"
"io/ioutil"
@@ -13,7 +14,6 @@ import (
"os"
"testing"
"time"
- "github.com/spiral/roadrunner/service/env"
)
type testCfg struct {
@@ -49,7 +49,7 @@ func Test_Service_NoConfig(t *testing.T) {
c := service.NewContainer(logger)
c.Register(ID, &Service{})
- assert.Error(t, c.Init(&testCfg{httpCfg: `{}`}))
+ assert.Error(t, c.Init(&testCfg{httpCfg: `{"Enable":true}`}))
s, st := c.Get(ID)
assert.NotNil(t, s)