TEST http upload with big files

This commit is contained in:
boyska 2018-01-06 00:12:33 +01:00
parent d36a0e9037
commit ed7e9a92dd

View file

@ -4,11 +4,13 @@ import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
@ -234,6 +236,90 @@ shares:
}
}
func TestUploadBig(t *testing.T) {
if !*doHTTP {
return
}
limit := 21
if testing.Short() {
limit = 2
}
// test with 1,5,9,13,17,21MB
for i := 1; i < limit; i += 4 {
t.Run(fmt.Sprintf("S=%d", i), testUploadBig)
}
}
func testUploadBig(t *testing.T) {
size, err := strconv.Atoi(strings.SplitN(t.Name(), "=", 2)[1])
if err != nil {
t.Fatalf("Cannot convert size %s to int: %s", t.Name(), err)
return
}
size *= 1024 * 1024
defer filet.CleanUp(t)
d := filet.TmpDir(t, "")
conf := `
shares:
- name: foo
dir: DIR
authorized: ["*"]
description: foo
`
conf = strings.Replace(conf, "DIR", d, 1)
ts := getServer(conf)
cl := ts.Client()
bodyBuf := bytes.Buffer{}
bodyWriter := multipart.NewWriter(&bodyBuf)
fileWriter, err := bodyWriter.CreateFormFile("file", "foo.txt")
if err != nil {
t.Fatal("error creating form file", err)
return
}
// write 3MB made of 'a'
for i := 0; i < size; i++ {
_, err = fileWriter.Write([]byte(`a`))
if err != nil {
t.Fatal("error writing on form file", err)
return
}
}
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
req := userRequest(ts.URL+"/api/upload/foo", "someone", t)
req.Method = "POST"
req.Header.Set("Content-Type", contentType)
req.ContentLength = int64(bodyBuf.Len())
req.Body = ioutil.NopCloser(&bodyBuf)
resp, err := cl.Do(req)
if err != nil {
t.Fatal("Error POSTing file", err)
return
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal("Error reading server response", err)
return
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
t.Error("Server status is not success:", resp.Status, respBody)
}
fname := d + "/" + string(respBody)
if !filet.Exists(t, fname) {
t.Error("File does not exist", fname)
} else {
info, err := os.Stat(fname)
if err != nil {
t.Fatal("error reading info on uploaded file", err)
return
}
if info.Size() != int64(size) {
t.Errorf("File exists but has wrong size: expected %d, found %d", size, info.Size())
}
}
}
func TestSurfUpload(t *testing.T) {
if !*doHTTP {
return