diff options
author | Wolfy-J <[email protected]> | 2018-06-12 18:30:54 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-12 18:30:54 +0300 |
commit | 9980f8a1a4f8c2aebd8e40a8e892bda4bf734eb0 (patch) | |
tree | 6c6956e96498b8ba0fe78014117ee15fcbf5db07 /service | |
parent | 592bda7d2b2c58ce466979e567158202f07bda7e (diff) |
tests!
Diffstat (limited to 'service')
-rw-r--r-- | service/http/parse.go | 20 | ||||
-rw-r--r-- | service/http/response_test.go | 172 | ||||
-rw-r--r-- | service/http/server_test.go | 688 | ||||
-rw-r--r-- | service/http/uploads_config_test.go | 38 |
4 files changed, 462 insertions, 456 deletions
diff --git a/service/http/parse.go b/service/http/parse.go index 3b084eb8..96ffcdcc 100644 --- a/service/http/parse.go +++ b/service/http/parse.go @@ -1,10 +1,8 @@ package http import ( - "strings" "net/http" "os" - "log" ) // MaxLevel defines maximum tree depth for incoming request data and files. @@ -38,11 +36,7 @@ func (d dataTree) push(k string, v []string) { return } - indexes := make([]string, 0) - for _, index := range strings.Split(strings.Replace(k, "]", "", MaxLevel), "[") { - indexes = append(indexes, index) - } - + indexes := fetchIndexes(k) if len(indexes) <= MaxLevel { d.mount(indexes, v) } @@ -50,8 +44,6 @@ func (d dataTree) push(k string, v []string) { // mount mounts data tree recursively. func (d dataTree) mount(i []string, v []string) { - log.Println(i, ">", v) - if len(v) == 0 { return } @@ -70,6 +62,7 @@ func (d dataTree) mount(i []string, v []string) { if p, ok := d[i[0]]; ok { p.(dataTree).mount(i[1:], v) + return } d[i[0]] = make(dataTree) @@ -118,11 +111,7 @@ func (d fileTree) push(k string, v []*FileUpload) { return } - indexes := make([]string, 0) - for _, index := range strings.Split(k, "[") { - indexes = append(indexes, strings.Trim(index, "]")) - } - + indexes := fetchIndexes(k) if len(indexes) <= MaxLevel { d.mount(indexes, v) } @@ -148,6 +137,7 @@ func (d fileTree) mount(i []string, v []*FileUpload) { if p, ok := d[i[0]]; ok { p.(fileTree).mount(i[1:], v) + return } d[i[0]] = make(fileTree) @@ -177,7 +167,7 @@ func fetchIndexes(s string) []string { } pos = 2 default: - if pos > 0 { + if pos == 1 || pos == 2 { keys = append(keys, "") } diff --git a/service/http/response_test.go b/service/http/response_test.go index 157333a1..e45f5349 100644 --- a/service/http/response_test.go +++ b/service/http/response_test.go @@ -1,84 +1,92 @@ package http -// -//type testWriter struct { -// h http.Header -// buf bytes.Buffer -// wroteHeader bool -// code int -// err error -//} -// -//func (tw *testWriter) Header() http.Header { return tw.h } -// -//func (tw *testWriter) Write(p []byte) (int, error) { -// if !tw.wroteHeader { -// tw.WriteHeader(http.StatusOK) -// } -// -// n, e := tw.buf.Write(p) -// if e == nil { -// e = tw.err -// } -// -// return n, e -//} -// -//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()) -//} -// -//func TestNewResponse_StreamError(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)), err: errors.New("error")} -// assert.Error(t, r.Write(w)) -//} +import ( + "net/http" + "bytes" + "testing" + "github.com/spiral/roadrunner" + "github.com/stretchr/testify/assert" + "errors" +) + +type testWriter struct { + h http.Header + buf bytes.Buffer + wroteHeader bool + code int + err error +} + +func (tw *testWriter) Header() http.Header { return tw.h } + +func (tw *testWriter) Write(p []byte) (int, error) { + if !tw.wroteHeader { + tw.WriteHeader(http.StatusOK) + } + + n, e := tw.buf.Write(p) + if e == nil { + e = tw.err + } + + return n, e +} + +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()) +} + +func TestNewResponse_StreamError(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)), err: errors.New("error")} + assert.Error(t, r.Write(w)) +} diff --git a/service/http/server_test.go b/service/http/server_test.go index cde28ea2..ed238c58 100644 --- a/service/http/server_test.go +++ b/service/http/server_test.go @@ -3,6 +3,14 @@ package http import ( "net/http" "io/ioutil" + "github.com/spiral/roadrunner" + "testing" + "os" + "github.com/stretchr/testify/assert" + "net/url" + "strings" + "context" + "bytes" ) // get request and return body @@ -16,346 +24,340 @@ func get(url string) (string, *http.Response, error) { b, err := ioutil.ReadAll(r.Body) return string(b), r, err } -// -//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_Headers(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() }() -// -// req, err := http.NewRequest("GET", "http://localhost:8077?hello=world", nil) -// assert.NoError(t, err) -// -// req.Header.Add("input", "sample") -// -// 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, 200, r.StatusCode) -// assert.Equal(t, "world", r.Header.Get("Header")) -// assert.Equal(t, "SAMPLE", string(b)) -//} -// -//func TestServer_Cookies(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 cookie 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() }() -// -// req, err := http.NewRequest("GET", "http://localhost:8077", nil) -// assert.NoError(t, err) -// -// req.AddCookie(&http.Cookie{Name: "input", Value: "input-value"}) -// -// 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, 200, r.StatusCode) -// assert.Equal(t, "INPUT-VALUE", string(b)) -// -// for _, c := range r.Cookies() { -// assert.Equal(t, "output", c.Name) -// assert.Equal(t, "cookie-output", c.Value) -// } -//} -// -//func TestServer_JsonPayload_POST(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 payload 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() }() -// -// req, err := http.NewRequest( -// "POST", -// "http://localhost:8077", -// bytes.NewBufferString(`{"key":"value"}`), -// ) -// assert.NoError(t, err) -// -// req.Header.Add("Content-Type", "application/json") -// -// 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, 200, r.StatusCode) -// assert.Equal(t, `{"value":"key"}`, string(b)) -//} -// -//func TestServer_JsonPayload_PUT(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 payload 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() }() -// -// req, err := http.NewRequest( -// "PUT", -// "http://localhost:8077", -// bytes.NewBufferString(`{"key":"value"}`), -// ) -// assert.NoError(t, err) -// -// req.Header.Add("Content-Type", "application/json") -// -// 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, 200, r.StatusCode) -// assert.Equal(t, `{"value":"key"}`, string(b)) -//} -// -//func TestServer_JsonPayload_PATCH(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 payload 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() }() -// -// req, err := http.NewRequest( -// "PATCH", -// "http://localhost:8077", -// bytes.NewBufferString(`{"key":"value"}`), -// ) -// assert.NoError(t, err) -// -// req.Header.Add("Content-Type", "application/json") -// -// 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, 200, r.StatusCode) -// assert.Equal(t, `{"value":"key"}`, string(b)) -//} -// -//func TestServer_FormData_POST(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 data 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() }() -// -// form := url.Values{} -// -// form.Add("key", "value") -// form.Add("name[]", "name1") -// form.Add("name[]", "name2") -// form.Add("name[]", "name3") -// form.Add("arr[x][y][z]", "y") -// form.Add("arr[x][y][e]", "f") -// form.Add("arr[c]p", "l") -// -// req, err := http.NewRequest( -// "POST", -// "http://localhost:8077", -// strings.NewReader(form.Encode()), -// ) -// assert.NoError(t, err) -// -// req.Header.Add("Content-Type", "application/x-www-form-urlencoded") -// -// 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, 200, r.StatusCode) -// -// log.Println(`{"arr":{"c":{"p":"l"},"x":{"y":{"e":"f","z":"y"}}},"key":"value","name":["name1","name2","name3"]}`) -// log.Println(string(b)) -// -// -// -// -// assert.Equal(t, `{"arr":{"c":{"p":"l"},"x":{"y":{"e":"f","z":"y"}}},"key":"value","name":["name1","name2","name3"]}`, string(b)) -//} + +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_Headers(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() }() + + req, err := http.NewRequest("GET", "http://localhost:8077?hello=world", nil) + assert.NoError(t, err) + + req.Header.Add("input", "sample") + + 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, 200, r.StatusCode) + assert.Equal(t, "world", r.Header.Get("Header")) + assert.Equal(t, "SAMPLE", string(b)) +} + +func TestServer_Cookies(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 cookie 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() }() + + req, err := http.NewRequest("GET", "http://localhost:8077", nil) + assert.NoError(t, err) + + req.AddCookie(&http.Cookie{Name: "input", Value: "input-value"}) + + 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, 200, r.StatusCode) + assert.Equal(t, "INPUT-VALUE", string(b)) + + for _, c := range r.Cookies() { + assert.Equal(t, "output", c.Name) + assert.Equal(t, "cookie-output", c.Value) + } +} + +func TestServer_JsonPayload_POST(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 payload 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() }() + + req, err := http.NewRequest( + "POST", + "http://localhost:8077", + bytes.NewBufferString(`{"key":"value"}`), + ) + assert.NoError(t, err) + + req.Header.Add("Content-Type", "application/json") + + 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, 200, r.StatusCode) + assert.Equal(t, `{"value":"key"}`, string(b)) +} + +func TestServer_JsonPayload_PUT(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 payload 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() }() + + req, err := http.NewRequest( + "PUT", + "http://localhost:8077", + bytes.NewBufferString(`{"key":"value"}`), + ) + assert.NoError(t, err) + + req.Header.Add("Content-Type", "application/json") + + 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, 200, r.StatusCode) + assert.Equal(t, `{"value":"key"}`, string(b)) +} + +func TestServer_JsonPayload_PATCH(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 payload 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() }() + + req, err := http.NewRequest( + "PATCH", + "http://localhost:8077", + bytes.NewBufferString(`{"key":"value"}`), + ) + assert.NoError(t, err) + + req.Header.Add("Content-Type", "application/json") + + 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, 200, r.StatusCode) + assert.Equal(t, `{"value":"key"}`, string(b)) +} + +func TestServer_FormData_POST(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 data 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() }() + + form := url.Values{} + + form.Add("key", "value") + form.Add("name[]", "name1") + form.Add("name[]", "name2") + form.Add("name[]", "name3") + form.Add("arr[x][y][z]", "y") + form.Add("arr[x][y][e]", "f") + form.Add("arr[c]p", "l") + + req, err := http.NewRequest( + "POST", + "http://localhost:8077", + strings.NewReader(form.Encode()), + ) + assert.NoError(t, err) + + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + 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, 200, r.StatusCode) + + assert.Equal(t, `{"arr":{"c":{"p":"l"},"x":{"y":{"e":"f","z":"y"}}},"key":"value","name":["name1","name2","name3"]}`, string(b)) +} diff --git a/service/http/uploads_config_test.go b/service/http/uploads_config_test.go index 4fa8a74f..7704a486 100644 --- a/service/http/uploads_config_test.go +++ b/service/http/uploads_config_test.go @@ -1,18 +1,24 @@ package http -//func TestFsConfig_Forbids(t *testing.T) { -// cfg := UploadsConfig{Forbid: []string{".php"}} -// -// assert.True(t, cfg.Forbids("index.php")) -// assert.True(t, cfg.Forbids("index.PHP")) -// assert.True(t, cfg.Forbids("phpadmin/index.bak.php")) -// assert.False(t, cfg.Forbids("index.html")) -//} -// -//func TestFsConfig_TmpFallback(t *testing.T) { -// cfg := UploadsConfig{Dir: "test"} -// assert.Equal(t, "test", cfg.TmpDir()) -// -// cfg = UploadsConfig{Dir: ""} -// assert.Equal(t, os.TempDir(), cfg.TmpDir()) -//} +import ( + "testing" + "github.com/stretchr/testify/assert" + "os" +) + +func TestFsConfig_Forbids(t *testing.T) { + cfg := UploadsConfig{Forbid: []string{".php"}} + + assert.True(t, cfg.Forbids("index.php")) + assert.True(t, cfg.Forbids("index.PHP")) + assert.True(t, cfg.Forbids("phpadmin/index.bak.php")) + assert.False(t, cfg.Forbids("index.html")) +} + +func TestFsConfig_TmpFallback(t *testing.T) { + cfg := UploadsConfig{Dir: "test"} + assert.Equal(t, "test", cfg.TmpDir()) + + cfg = UploadsConfig{Dir: ""} + assert.Equal(t, os.TempDir(), cfg.TmpDir()) +} |