forked from quic-go/quic-go
add handlers to support uploading multiple files in integration tests
ref #287
This commit is contained in:
@@ -148,9 +148,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 +172,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())
|
||||||
|
|||||||
@@ -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