summaryrefslogtreecommitdiff
path: root/service/http/uploads_test.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-12-23 14:43:47 +0300
committerWolfy-J <[email protected]>2019-12-23 14:43:47 +0300
commitc7b0a4a81827284f7565c56aa476eea34fb6382f (patch)
treeee30e93169c37fb058fbe55af6b3f954eabd9646 /service/http/uploads_test.go
parent7f694966730f6dac09d0d0ea3bf51276b8e4dfe4 (diff)
- test fixes
Diffstat (limited to 'service/http/uploads_test.go')
-rw-r--r--service/http/uploads_test.go196
1 files changed, 160 insertions, 36 deletions
diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go
index 0fbf0e14..1890c02b 100644
--- a/service/http/uploads_test.go
+++ b/service/http/uploads_test.go
@@ -6,6 +6,7 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
+ "fmt"
"github.com/spiral/roadrunner"
"github.com/stretchr/testify/assert"
"io"
@@ -41,22 +42,43 @@ func TestHandler_Upload_File(t *testing.T) {
defer h.rr.Stop()
hs := &http.Server{Addr: ":8021", Handler: h}
- defer hs.Shutdown(context.Background())
-
- go func() { hs.ListenAndServe() }()
+ defer func() {
+ err := hs.Shutdown(context.Background())
+ if err != nil {
+ t.Errorf("error during the shutdown: error %v", err)
+ }
+ }()
+
+ go func() {
+ err := hs.ListenAndServe()
+ if err != nil && err != http.ErrServerClosed {
+ t.Errorf("error listening the interface: error %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 10)
var mb bytes.Buffer
w := multipart.NewWriter(&mb)
f := mustOpen("uploads_test.go")
- defer f.Close()
+ defer func() {
+ err := f.Close()
+ if err != nil {
+ t.Errorf("failed to close a file: error %v", err)
+ }
+ }()
fw, err := w.CreateFormFile("upload", f.Name())
assert.NotNil(t, fw)
assert.NoError(t, err)
- io.Copy(fw, f)
+ _, err = io.Copy(fw, f)
+ if err != nil {
+ t.Errorf("error copying the file: error %v", err)
+ }
- w.Close()
+ err = w.Close()
+ if err != nil {
+ t.Errorf("error closing the file: error %v", err)
+ }
req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
@@ -65,7 +87,12 @@ func TestHandler_Upload_File(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ defer func() {
+ err := r.Body.Close()
+ if err != nil {
+ t.Errorf("error closing the Body: error %v", err)
+ }
+ }()
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
@@ -102,22 +129,43 @@ func TestHandler_Upload_NestedFile(t *testing.T) {
defer h.rr.Stop()
hs := &http.Server{Addr: ":8021", Handler: h}
- defer hs.Shutdown(context.Background())
-
- go func() { hs.ListenAndServe() }()
+ defer func() {
+ err := hs.Shutdown(context.Background())
+ if err != nil {
+ t.Errorf("error during the shutdown: error %v", err)
+ }
+ }()
+
+ go func() {
+ err := hs.ListenAndServe()
+ if err != nil && err != http.ErrServerClosed {
+ t.Errorf("error listening the interface: error %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 10)
var mb bytes.Buffer
w := multipart.NewWriter(&mb)
f := mustOpen("uploads_test.go")
- defer f.Close()
+ defer func() {
+ err := f.Close()
+ if err != nil {
+ t.Errorf("failed to close a file: error %v", err)
+ }
+ }()
fw, err := w.CreateFormFile("upload[x][y][z][]", f.Name())
assert.NotNil(t, fw)
assert.NoError(t, err)
- io.Copy(fw, f)
+ _, err = io.Copy(fw, f)
+ if err != nil {
+ t.Errorf("error copying the file: error %v", err)
+ }
- w.Close()
+ err = w.Close()
+ if err != nil {
+ t.Errorf("error closing the file: error %v", err)
+ }
req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
@@ -126,7 +174,12 @@ func TestHandler_Upload_NestedFile(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ defer func() {
+ err := r.Body.Close()
+ if err != nil {
+ t.Errorf("error closing the Body: error %v", err)
+ }
+ }()
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
@@ -163,22 +216,43 @@ func TestHandler_Upload_File_NoTmpDir(t *testing.T) {
defer h.rr.Stop()
hs := &http.Server{Addr: ":8021", Handler: h}
- defer hs.Shutdown(context.Background())
-
- go func() { hs.ListenAndServe() }()
+ defer func() {
+ err := hs.Shutdown(context.Background())
+ if err != nil {
+ t.Errorf("error during the shutdown: error %v", err)
+ }
+ }()
+
+ go func() {
+ err := hs.ListenAndServe()
+ if err != nil && err != http.ErrServerClosed {
+ t.Errorf("error listening the interface: error %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 10)
var mb bytes.Buffer
w := multipart.NewWriter(&mb)
f := mustOpen("uploads_test.go")
- defer f.Close()
+ defer func() {
+ err := f.Close()
+ if err != nil {
+ t.Errorf("failed to close a file: error %v", err)
+ }
+ }()
fw, err := w.CreateFormFile("upload", f.Name())
assert.NotNil(t, fw)
assert.NoError(t, err)
- io.Copy(fw, f)
+ _, err = io.Copy(fw, f)
+ if err != nil {
+ t.Errorf("error copying the file: error %v", err)
+ }
- w.Close()
+ err = w.Close()
+ if err != nil {
+ t.Errorf("error closing the file: error %v", err)
+ }
req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
@@ -187,7 +261,12 @@ func TestHandler_Upload_File_NoTmpDir(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ defer func() {
+ err := r.Body.Close()
+ if err != nil {
+ t.Errorf("error closing the Body: error %v", err)
+ }
+ }()
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
@@ -224,22 +303,43 @@ func TestHandler_Upload_File_Forbids(t *testing.T) {
defer h.rr.Stop()
hs := &http.Server{Addr: ":8021", Handler: h}
- defer hs.Shutdown(context.Background())
-
- go func() { hs.ListenAndServe() }()
+ defer func() {
+ err := hs.Shutdown(context.Background())
+ if err != nil {
+ t.Errorf("error during the shutdown: error %v", err)
+ }
+ }()
+
+ go func() {
+ err := hs.ListenAndServe()
+ if err != nil && err != http.ErrServerClosed {
+ t.Errorf("error listening the interface: error %v", err)
+ }
+ }()
time.Sleep(time.Millisecond * 10)
var mb bytes.Buffer
w := multipart.NewWriter(&mb)
f := mustOpen("uploads_test.go")
- defer f.Close()
+ defer func() {
+ err := f.Close()
+ if err != nil {
+ t.Errorf("failed to close a file: error %v", err)
+ }
+ }()
fw, err := w.CreateFormFile("upload", f.Name())
assert.NotNil(t, fw)
assert.NoError(t, err)
- io.Copy(fw, f)
+ _, err = io.Copy(fw, f)
+ if err != nil {
+ t.Errorf("error copying the file: error %v", err)
+ }
- w.Close()
+ err = w.Close()
+ if err != nil {
+ t.Errorf("error closing the file: error %v", err)
+ }
req, err := http.NewRequest("POST", "http://localhost"+hs.Addr, &mb)
assert.NoError(t, err)
@@ -248,7 +348,12 @@ func TestHandler_Upload_File_Forbids(t *testing.T) {
r, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
- defer r.Body.Close()
+ defer func() {
+ err := r.Body.Close()
+ if err != nil {
+ t.Errorf("error closing the Body: error %v", err)
+ }
+ }()
b, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
@@ -282,28 +387,47 @@ type fInfo struct {
MD5 string `json:"md5,omitempty"`
}
-func fileString(f string, err int, mime string) string {
- s, _ := os.Stat(f)
+func fileString(f string, errNo int, mime string) string {
+ s, err := os.Stat(f)
+ if err != nil {
+ fmt.Println(fmt.Errorf("error stat the file, error: %v", err))
+ }
+
+ ff, err := os.Open(f)
+ if err != nil {
+ fmt.Println(fmt.Errorf("error opening the file, error: %v", err))
+ }
+
+ defer func() {
+ er := ff.Close()
+ if er != nil {
+ fmt.Println(fmt.Errorf("error closing the file, error: %v", er))
+ }
+ }()
- ff, _ := os.Open(f)
- defer ff.Close()
h := md5.New()
- io.Copy(h, ff)
+ _, err = io.Copy(h, ff)
+ if err != nil {
+ fmt.Println(fmt.Errorf("error copying the file, error: %v", err))
+ }
v := &fInfo{
Name: s.Name(),
Size: s.Size(),
- Error: err,
+ Error: errNo,
Mime: mime,
MD5: hex.EncodeToString(h.Sum(nil)),
}
- if err != 0 {
+ if errNo != 0 {
v.MD5 = ""
v.Size = 0
}
- r, _ := json.Marshal(v)
+ r, err := json.Marshal(v)
+ if err != nil {
+ fmt.Println(fmt.Errorf("error marshalling fInfo, error: %v", err))
+ }
return string(r)
}