forked from quic-go/quic-go
Merge pull request #326 from lucas-clemente/fix-287
add an integration uploading a lot of small files
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/tebeka/selenium"
|
"github.com/tebeka/selenium"
|
||||||
|
|
||||||
@@ -148,9 +149,9 @@ var _ = Describe("Chrome tests", func() {
|
|||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
copyFileToDocker(tmpfn)
|
copyFileToDocker(tmpfn)
|
||||||
|
|
||||||
err = wd.Get("https://quic.clemente.io/uploadform")
|
err = wd.Get("https://quic.clemente.io/uploadform?num=1")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
elem, err := wd.FindElement(selenium.ByCSSSelector, "#upload")
|
elem, err := wd.FindElement(selenium.ByCSSSelector, "#upload_0")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
err = elem.SendKeys("/home/seluser/data.dat")
|
err = elem.SendKeys("/home/seluser/data.dat")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
@@ -172,9 +173,9 @@ var _ = Describe("Chrome tests", func() {
|
|||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
copyFileToDocker(tmpfn)
|
copyFileToDocker(tmpfn)
|
||||||
|
|
||||||
err = wd.Get("https://quic.clemente.io/uploadform")
|
err = wd.Get("https://quic.clemente.io/uploadform?num=1")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
elem, err := wd.FindElement(selenium.ByCSSSelector, "#upload")
|
elem, err := wd.FindElement(selenium.ByCSSSelector, "#upload_0")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
err = elem.SendKeys("/home/seluser/data.dat")
|
err = elem.SendKeys("/home/seluser/data.dat")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
@@ -184,6 +185,50 @@ var _ = Describe("Chrome tests", func() {
|
|||||||
Expect(getFileSize(file)).To(Equal(dataLongLen))
|
Expect(getFileSize(file)).To(Equal(dataLongLen))
|
||||||
Expect(getFileMD5(file)).To(Equal(dataMan.GetMD5()))
|
Expect(getFileMD5(file)).To(Equal(dataMan.GetMD5()))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// this test takes a long time because it copies every file into the docker container one by one
|
||||||
|
// unfortunately, docker doesn't support copying multiple files at once
|
||||||
|
// see https://github.com/docker/docker/issues/7710
|
||||||
|
It("uploads many small files", func() {
|
||||||
|
num := protocol.MaxStreamsPerConnection + 20
|
||||||
|
|
||||||
|
dir, err := ioutil.TempDir("", "quic-upload-src")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
var md5s [][]byte
|
||||||
|
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
dataMan.GenerateData(dataLen)
|
||||||
|
data := dataMan.GetData()
|
||||||
|
md5s = append(md5s, dataMan.GetMD5())
|
||||||
|
tmpfn := filepath.Join(dir, "data_"+strconv.Itoa(i)+".dat")
|
||||||
|
err = ioutil.WriteFile(tmpfn, data, 0777)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
copyFileToDocker(tmpfn)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = wd.Get("https://quic.clemente.io/uploadform?num=" + strconv.Itoa(num))
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
var elem selenium.WebElement
|
||||||
|
elem, err = wd.FindElement(selenium.ByCSSSelector, "#upload_"+strconv.Itoa(i))
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
err = elem.SendKeys("/home/seluser/data_" + strconv.Itoa(i) + ".dat")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
}
|
||||||
|
|
||||||
|
elem, err := wd.FindElement(selenium.ByCSSSelector, "#form")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
Eventually(func() error { return elem.Submit() }, 30, 0.1).ShouldNot(HaveOccurred())
|
||||||
|
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
file := filepath.Join(uploadDir, "data_"+strconv.Itoa(i)+".dat")
|
||||||
|
Expect(getFileSize(file)).To(Equal(dataLen))
|
||||||
|
Expect(getFileMD5(file)).To(Equal(md5s[i]))
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime/multipart"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -103,13 +104,18 @@ func setupHTTPHandlers() {
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// requires the num GET parameter, e.g. /uploadform?num=2
|
||||||
|
// will create num input fields for uploading files
|
||||||
http.HandleFunc("/uploadform", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/uploadform", func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer GinkgoRecover()
|
defer GinkgoRecover()
|
||||||
_, err := io.WriteString(w, `<html><body>
|
num, err := strconv.Atoi(r.URL.Query().Get("num"))
|
||||||
<form id="form" action="https://quic.clemente.io/uploadhandler" method="post" enctype="multipart/form-data">
|
Expect(err).ToNot(HaveOccurred())
|
||||||
<input type="file" id="upload" name="uploadfile" />
|
response := "<html><body>\n<form id='form' action='https://quic.clemente.io/uploadhandler' method='post' enctype='multipart/form-data'>"
|
||||||
</form>
|
for i := 0; i < num; i++ {
|
||||||
<body></html>`)
|
response += "<input type='file' id='upload_" + strconv.Itoa(i) + "' name='uploadfile_" + strconv.Itoa(i) + "' />"
|
||||||
|
}
|
||||||
|
response += "</form><body></html>"
|
||||||
|
_, err = io.WriteString(w, response)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -118,13 +124,23 @@ func setupHTTPHandlers() {
|
|||||||
|
|
||||||
err := r.ParseMultipartForm(100 * (1 << 20)) // max. 100 MB
|
err := r.ParseMultipartForm(100 * (1 << 20)) // max. 100 MB
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
file, handler, err := r.FormFile("uploadfile")
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
count := 0
|
||||||
defer file.Close()
|
for {
|
||||||
f, err := os.OpenFile(path.Join(uploadDir, handler.Filename), os.O_WRONLY|os.O_CREATE, 0666)
|
var file multipart.File
|
||||||
Expect(err).ToNot(HaveOccurred())
|
var handler *multipart.FileHeader
|
||||||
defer f.Close()
|
file, handler, err = r.FormFile("uploadfile_" + strconv.Itoa(count))
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
f, err2 := os.OpenFile(path.Join(uploadDir, handler.Filename), os.O_WRONLY|os.O_CREATE, 0666)
|
||||||
|
Expect(err2).ToNot(HaveOccurred())
|
||||||
io.Copy(f, file)
|
io.Copy(f, file)
|
||||||
|
f.Close()
|
||||||
|
file.Close()
|
||||||
|
}
|
||||||
|
Expect(count).ToNot(BeZero()) // there have been at least one uploaded file in this request
|
||||||
|
|
||||||
_, err = io.WriteString(w, "")
|
_, err = io.WriteString(w, "")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|||||||
Reference in New Issue
Block a user