summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-11 14:57:47 +0300
committerWolfy-J <[email protected]>2018-06-11 14:57:47 +0300
commit846cec64f177a9cfef016a8225c4fae0faa29a0c (patch)
treea0dc1df41ab25793a2dd5ada80f3ca5de4615e7d /service
parent78fb74cfa5c0156f70c095a4f2cfee9851a9dc0f (diff)
more tests for http
Diffstat (limited to 'service')
-rw-r--r--service/http/config.go2
-rw-r--r--service/http/response.go1
-rw-r--r--service/http/response_test.go70
-rw-r--r--service/http/server.go2
-rw-r--r--service/http/server_test.go94
5 files changed, 166 insertions, 3 deletions
diff --git a/service/http/config.go b/service/http/config.go
index d92b4c60..3db172b1 100644
--- a/service/http/config.go
+++ b/service/http/config.go
@@ -12,7 +12,7 @@ type Config struct {
// Address and port to handle as http server.
Address string
- // MaxRequest specified max size for payload body in bytes, set 0 to unlimited.
+ // MaxRequest specified max size for payload body in megabytes, set 0 to unlimited.
MaxRequest int64
// Uploads configures uploads configuration.
diff --git a/service/http/response.go b/service/http/response.go
index dd092353..69bcf3e1 100644
--- a/service/http/response.go
+++ b/service/http/response.go
@@ -34,7 +34,6 @@ func (r *Response) Write(w http.ResponseWriter) error {
for k, v := range r.Headers {
for _, h := range v {
w.Header().Add(k, h)
-
}
}
diff --git a/service/http/response_test.go b/service/http/response_test.go
new file mode 100644
index 00000000..dfc08104
--- /dev/null
+++ b/service/http/response_test.go
@@ -0,0 +1,70 @@
+package http
+
+import (
+ "testing"
+ "github.com/spiral/roadrunner"
+ "github.com/stretchr/testify/assert"
+ "net/http"
+ "bytes"
+)
+
+type testWriter struct {
+ h http.Header
+ buf bytes.Buffer
+ wroteHeader bool
+ code int
+}
+
+func (tw *testWriter) Header() http.Header { return tw.h }
+
+func (tw *testWriter) Write(p []byte) (int, error) {
+ if !tw.wroteHeader {
+ tw.WriteHeader(http.StatusOK)
+ }
+
+ return tw.buf.Write(p)
+}
+
+func (tw *testWriter) WriteHeader(code int) { tw.wroteHeader = true; tw.code = code }
+
+func TestNewResponse_Error(t *testing.T) {
+ r, err := NewResponse(&roadrunner.Payload{Context: []byte(`invalid payload`)})
+ assert.Error(t, err)
+ assert.Nil(t, r)
+}
+
+func TestNewResponse_Write(t *testing.T) {
+ r, err := NewResponse(&roadrunner.Payload{
+ Context: []byte(`{"headers":{"key":["value"]},"status": 301}`),
+ Body: []byte(`sample body`),
+ })
+
+ assert.NoError(t, err)
+ assert.NotNil(t, r)
+
+ w := &testWriter{h: http.Header(make(map[string][]string))}
+ assert.NoError(t, r.Write(w))
+
+ assert.Equal(t, 301, w.code)
+ assert.Equal(t, "value", w.h.Get("key"))
+ assert.Equal(t, "sample body", w.buf.String())
+}
+
+func TestNewResponse_Stream(t *testing.T) {
+ r, err := NewResponse(&roadrunner.Payload{
+ Context: []byte(`{"headers":{"key":["value"]},"status": 301}`),
+ })
+
+ r.body = &bytes.Buffer{}
+ r.body.(*bytes.Buffer).WriteString("hello world")
+
+ assert.NoError(t, err)
+ assert.NotNil(t, r)
+
+ w := &testWriter{h: http.Header(make(map[string][]string))}
+ assert.NoError(t, r.Write(w))
+
+ assert.Equal(t, 301, w.code)
+ assert.Equal(t, "value", w.h.Get("key"))
+ assert.Equal(t, "hello world", w.buf.String())
+}
diff --git a/service/http/server.go b/service/http/server.go
index de414b08..4ac9b03c 100644
--- a/service/http/server.go
+++ b/service/http/server.go
@@ -51,7 +51,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if size, err := strconv.ParseInt(length, 10, 64); err != nil {
s.handleError(w, r, err)
return
- } else if size > s.cfg.MaxRequest {
+ } else if size > s.cfg.MaxRequest*1024*1024 {
s.handleError(w, r, errors.New("request body max size is exceeded"))
return
}
diff --git a/service/http/server_test.go b/service/http/server_test.go
new file mode 100644
index 00000000..07c5bbdc
--- /dev/null
+++ b/service/http/server_test.go
@@ -0,0 +1,94 @@
+package http
+
+import (
+ "testing"
+ "github.com/spiral/roadrunner"
+ "os"
+ "github.com/stretchr/testify/assert"
+ "net/http"
+ "context"
+ "bytes"
+ "io"
+)
+
+// get request and return body
+func get(url string) (string, *http.Response, error) {
+ r, err := http.Get(url)
+ if err != nil {
+ return "", nil, err
+ }
+ defer r.Body.Close()
+
+ buf := new(bytes.Buffer)
+ io.Copy(buf, r.Body)
+
+ return buf.String(), r, nil
+}
+
+func TestServer_Echo(t *testing.T) {
+ st := &Server{
+ 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,
+ },
+ }),
+ }
+
+ assert.NoError(t, st.rr.Start())
+ defer st.rr.Stop()
+
+ hs := &http.Server{Addr: ":8077", Handler: st,}
+ defer hs.Shutdown(context.Background())
+
+ go func() { hs.ListenAndServe() }()
+
+ body, r, err := get("http://localhost:8077/?hello=world")
+ assert.NoError(t, err)
+ assert.Equal(t, 200, r.StatusCode)
+ assert.Equal(t, "WORLD", body)
+}
+
+func TestServer_EchoHeaders(t *testing.T) {
+ st := &Server{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../php-src/tests/http/client.php header pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ assert.NoError(t, st.rr.Start())
+ defer st.rr.Stop()
+
+ hs := &http.Server{Addr: ":8077", Handler: st,}
+ defer hs.Shutdown(context.Background())
+
+ go func() { hs.ListenAndServe() }()
+
+ _, r, err := get("http://localhost:8077/?hello=world")
+ assert.NoError(t, err)
+ assert.Equal(t, 200, r.StatusCode)
+ assert.Equal(t, "world", r.Header.Get("Header"))
+}