TEST http upload with net/http.Client

This commit is contained in:
boyska 2018-01-05 23:47:03 +01:00
parent 3768f92f30
commit 24074467cf

View file

@ -1,9 +1,11 @@
package megauploader
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
@ -174,6 +176,56 @@ shares:
}
}
func TestUpload(t *testing.T) {
if !*doHTTP {
return
}
ts := getServer(`
global:
excluded:
- john@doe.us
- foo bar
shares:
- name: foo
authorized: ["*"]
description: foo
`)
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
}
_, err = fileWriter.Write([]byte(`example content`))
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)
}
}
func TestSurfUpload(t *testing.T) {
if !*doHTTP {
return