summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-13 14:02:14 +0300
committerWolfy-J <[email protected]>2018-06-13 14:02:14 +0300
commitb58bbaa615f38916860e06db27e2ab31b0eb2d08 (patch)
tree8e446b203dd3c2ab47147a6efd864416a88af841
parent8b24183daa620bb12edf78f6e81697532cbc92d8 (diff)
more tests for files!
-rw-r--r--php-src/PSR7Client.php3
-rw-r--r--service/http/handler.go10
-rw-r--r--service/http/handler_test.go62
-rw-r--r--service/http/uploads.go12
-rw-r--r--service/http/uploads_test.go116
5 files changed, 143 insertions, 60 deletions
diff --git a/php-src/PSR7Client.php b/php-src/PSR7Client.php
index c3cdf844..f8913a8d 100644
--- a/php-src/PSR7Client.php
+++ b/php-src/PSR7Client.php
@@ -117,13 +117,12 @@ class PSR7Client
continue;
}
-
$result[$index] = new Diactoros\UploadedFile(
$file['tmpName'],
$file['size'],
$file['error'],
$file['name'],
- $file['type']
+ $file['mime']
);
}
diff --git a/service/http/handler.go b/service/http/handler.go
index 2d749207..0124a2a4 100644
--- a/service/http/handler.go
+++ b/service/http/handler.go
@@ -97,11 +97,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resp.Write(w)
}
-// handleResponse triggers response event.
-func (h *Handler) handleResponse(req *Request, resp *Response) {
- h.throw(EventResponse, &Event{Method: req.Method, Uri: req.Uri, Status: resp.Status})
-}
-
// handleError sends error.
func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) {
h.throw(EventError, &Event{Method: r.Method, Uri: uri(r), Status: 500, Error: err})
@@ -110,6 +105,11 @@ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error)
w.Write([]byte(err.Error()))
}
+// handleResponse triggers response event.
+func (h *Handler) handleResponse(req *Request, resp *Response) {
+ h.throw(EventResponse, &Event{Method: req.Method, Uri: req.Uri, Status: resp.Status})
+}
+
// throw invokes event srv if any.
func (h *Handler) throw(event int, ctx interface{}) {
h.mul.Lock()
diff --git a/service/http/handler_test.go b/service/http/handler_test.go
index 560b9c1b..6cc9aeb1 100644
--- a/service/http/handler_test.go
+++ b/service/http/handler_test.go
@@ -229,16 +229,12 @@ func TestServer_JsonPayload_PUT(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8081", Handler: st,}
+ hs := &http.Server{Addr: ":8081", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
- req, err := http.NewRequest(
- "PUT",
- "http://localhost"+hs.Addr,
- bytes.NewBufferString(`{"key":"value"}`),
- )
+ req, err := http.NewRequest("PUT", "http://localhost"+hs.Addr, bytes.NewBufferString(`{"key":"value"}`))
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/json")
@@ -278,17 +274,13 @@ func TestServer_JsonPayload_PATCH(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8082", Handler: st,}
+ hs := &http.Server{Addr: ":8082", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
time.Sleep(time.Millisecond * 10)
- req, err := http.NewRequest(
- "PATCH",
- "http://localhost"+hs.Addr,
- bytes.NewBufferString(`{"key":"value"}`),
- )
+ req, err := http.NewRequest("PATCH", "http://localhost"+hs.Addr, bytes.NewBufferString(`{"key":"value"}`))
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/json")
@@ -345,11 +337,7 @@ func TestServer_FormData_POST(t *testing.T) {
form.Add("arr[c]p", "l")
form.Add("arr[c]z", "")
- req, err := http.NewRequest(
- "POST",
- "http://localhost"+hs.Addr,
- strings.NewReader(form.Encode()),
- )
+ req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, strings.NewReader(form.Encode()))
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
@@ -390,7 +378,7 @@ func TestServer_FormData_PUT(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8084", Handler: st,}
+ hs := &http.Server{Addr: ":8084", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
@@ -407,11 +395,7 @@ func TestServer_FormData_PUT(t *testing.T) {
form.Add("arr[c]p", "l")
form.Add("arr[c]z", "")
- req, err := http.NewRequest(
- "PUT",
- "http://localhost"+hs.Addr,
- strings.NewReader(form.Encode()),
- )
+ req, err := http.NewRequest("PUT", "http://localhost"+hs.Addr, strings.NewReader(form.Encode()))
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
@@ -452,7 +436,7 @@ func TestServer_FormData_PATCH(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8085", Handler: st,}
+ hs := &http.Server{Addr: ":8085", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
@@ -469,11 +453,7 @@ func TestServer_FormData_PATCH(t *testing.T) {
form.Add("arr[c]p", "l")
form.Add("arr[c]z", "")
- req, err := http.NewRequest(
- "PATCH",
- "http://localhost"+hs.Addr,
- strings.NewReader(form.Encode()),
- )
+ req, err := http.NewRequest("PATCH", "http://localhost"+hs.Addr, strings.NewReader(form.Encode()))
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
@@ -514,7 +494,7 @@ func TestServer_Multipart_POST(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8019", Handler: st,}
+ hs := &http.Server{Addr: ":8019", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
@@ -535,11 +515,7 @@ func TestServer_Multipart_POST(t *testing.T) {
w.Close()
- req, err := http.NewRequest(
- "POST",
- "http://localhost"+hs.Addr,
- &mb,
- )
+ req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
req.Header.Set("Content-Type", w.FormDataContentType())
@@ -580,7 +556,7 @@ func TestServer_Multipart_PUT(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8020", Handler: st,}
+ hs := &http.Server{Addr: ":8020", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
@@ -601,11 +577,7 @@ func TestServer_Multipart_PUT(t *testing.T) {
w.Close()
- req, err := http.NewRequest(
- "PUT",
- "http://localhost"+hs.Addr,
- &mb,
- )
+ req, err := http.NewRequest("PUT", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
req.Header.Set("Content-Type", w.FormDataContentType())
@@ -646,7 +618,7 @@ func TestServer_Multipart_PATCH(t *testing.T) {
assert.NoError(t, st.rr.Start())
defer st.rr.Stop()
- hs := &http.Server{Addr: ":8021", Handler: st,}
+ hs := &http.Server{Addr: ":8021", Handler: st}
defer hs.Shutdown(context.Background())
go func() { hs.ListenAndServe() }()
@@ -667,11 +639,7 @@ func TestServer_Multipart_PATCH(t *testing.T) {
w.Close()
- req, err := http.NewRequest(
- "PATCH",
- "http://localhost"+hs.Addr,
- &mb,
- )
+ req, err := http.NewRequest("PATCH", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
req.Header.Set("Content-Type", w.FormDataContentType())
diff --git a/service/http/uploads.go b/service/http/uploads.go
index 1f3060c0..99484f4b 100644
--- a/service/http/uploads.go
+++ b/service/http/uploads.go
@@ -73,8 +73,8 @@ type FileUpload struct {
// Name contains filename specified by the client.
Name string `json:"name"`
- // MimeType contains mime-type provided by the client.
- MimeType string `json:"type"`
+ // Mime contains mime-type provided by the client.
+ Mime string `json:"mime"`
// Size of the uploaded file.
Size int64 `json:"size"`
@@ -92,10 +92,10 @@ type FileUpload struct {
// NewUpload wraps net/http upload into PRS-7 compatible structure.
func NewUpload(f *multipart.FileHeader) *FileUpload {
return &FileUpload{
- Name: f.Filename,
- MimeType: f.Header.Get("Content-Type"),
- Error: UploadErrorOK,
- header: f,
+ Name: f.Filename,
+ Mime: f.Header.Get("Content-Type"),
+ Error: UploadErrorOK,
+ header: f,
}
}
diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go
new file mode 100644
index 00000000..80599884
--- /dev/null
+++ b/service/http/uploads_test.go
@@ -0,0 +1,116 @@
+package http
+
+import (
+ "github.com/spiral/roadrunner"
+ "github.com/stretchr/testify/assert"
+ "net/http"
+ "time"
+ "bytes"
+ "mime/multipart"
+ "io/ioutil"
+ "testing"
+ "os"
+ "context"
+ "io"
+ "encoding/json"
+ "crypto/md5"
+ "encoding/hex"
+)
+
+func TestServer_Upload_File(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 upload 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: ":8021", Handler: st,}
+ defer hs.Shutdown(context.Background())
+
+ go func() { hs.ListenAndServe() }()
+ time.Sleep(time.Millisecond * 10)
+
+ var mb bytes.Buffer
+ w := multipart.NewWriter(&mb)
+
+ f := mustOpen("uploads_test.go")
+ defer f.Close()
+ fw, err := w.CreateFormFile("upload", f.Name())
+ assert.NotNil(t, fw)
+ assert.NoError(t, err)
+ io.Copy(fw, f)
+
+ w.Close()
+
+ req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
+ assert.NoError(t, err)
+
+ req.Header.Set("Content-Type", w.FormDataContentType())
+
+ 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)
+
+ fs := fileString("uploads_test.go", 0, "application/octet-stream")
+
+ assert.Equal(t, `{"upload":`+fs+`}`, string(b))
+}
+
+func mustOpen(f string) *os.File {
+ r, err := os.Open(f)
+ if err != nil {
+ panic(err)
+ }
+ return r
+}
+
+type fInfo struct {
+ Name string `json:"name"`
+ Size int64 `json:"size"`
+ Mime string `json:"mime"`
+ Error int `json:"error"`
+ MD5 string `json:"md5"`
+}
+
+func fileString(f string, err int, mime string) string {
+ s, _ := os.Stat(f)
+
+ ff, _ := os.Open(f)
+ defer ff.Close()
+ h := md5.New()
+ io.Copy(h, ff)
+
+ v := &fInfo{
+ Name: s.Name(),
+ Size: s.Size(),
+ Error: err,
+ Mime: mime,
+ MD5: hex.EncodeToString(h.Sum(nil)),
+ }
+
+ r, _ := json.Marshal(v)
+ return string(r)
+
+}