test(e2e): test uploading randomly (#53)

This commit is contained in:
Hexxa 2021-05-13 17:06:00 +08:00 committed by GitHub
parent 36844eb2e0
commit 68548051d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 1 deletions

View file

@ -120,7 +120,6 @@ func TestFileHandlers(t *testing.T) {
return false
}
case 1:
if body[2:] != content { // body returned by gorequest contains the first CRLF
t.Errorf("body not equal got(%s) expect(%s)\n", body[2:], content)
return false
@ -509,4 +508,72 @@ func TestFileHandlers(t *testing.T) {
assetDownloadOK(t, filePath, content)
}
})
t.Run("test uploading APIs: Create and UploadChunk randomly)", func(t *testing.T) {
cl := client.NewFilesClient(addr)
files := map[string]string{
"uploadings/random/path1/f1": "12345678",
"uploadings/random/path1/f2": "87654321",
"uploadings/random/path1/f3": "17654321",
}
for filePath, content := range files {
fileSize := int64(len([]byte(content)))
res, _, errs := cl.Create(filePath, fileSize)
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
}
for filePath, content := range files {
fileSize := int64(len([]byte(content)))
chunks := [][]byte{
[]byte(content)[:fileSize/2],
[]byte(content)[fileSize/2:],
}
offset := int64(0)
for _, chunk := range chunks {
base64Content := base64.StdEncoding.EncodeToString(chunk)
res, _, errs := cl.UploadChunk(filePath, base64Content, offset)
offset += int64(len(chunk))
if len(errs) > 0 {
t.Fatal(errs)
} else if res.StatusCode != 200 {
t.Fatal(res.StatusCode)
}
err = fs.Close()
if err != nil {
t.Fatal(err)
}
}
err = fs.Sync()
if err != nil {
t.Fatal(err)
}
// metadata
_, mRes, errs := cl.Metadata(filePath)
if len(errs) > 0 {
t.Fatal(errs)
} else if mRes.Size != fileSize {
t.Fatal("incorrect uploaded size", mRes)
}
isEqual, err := compareFileContent(fs, filePath, content)
if err != nil {
t.Fatalf("err comparing content: %s", err)
} else if !isEqual {
t.Fatalf("file content not equal: %s", filePath)
}
assetDownloadOK(t, filePath, content)
}
})
}

View file

@ -1,10 +1,13 @@
package server
import (
"io/ioutil"
"path"
"time"
"github.com/ihexxa/gocfg"
"github.com/ihexxa/quickshare/src/client"
fspkg "github.com/ihexxa/quickshare/src/fs"
)
func startTestServer(config string) *Server {
@ -47,3 +50,17 @@ func waitForReady(addr string) bool {
return false
}
func compareFileContent(fs fspkg.ISimpleFS, filePath string, expectedContent string) (bool, error) {
reader, err := fs.GetFileReader(path.Join("files", filePath))
if err != nil {
return false, err
}
gotContent, err := ioutil.ReadAll(reader)
if err != nil {
return false, err
}
return string(gotContent) == expectedContent, nil
}