awesome-patterns/playground/http/upload_test.go

62 lines
1.2 KiB
Go
Raw Normal View History

2018-09-19 09:52:44 +03:00
package http
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"path/filepath"
"testing"
)
func TestUploadFormFile(t *testing.T) {
var filePath = "/Users/bruce/Desktop/HVAC-CoolMasterNet.yml"
var url = "http://localhost:48081/api/v1/deviceprofile/uploadfile"
2018-09-19 10:19:35 +03:00
// Retch file
fmt.Println("Read file: ", filepath.Base(filePath))
yamlFile, err := ioutil.ReadFile(filePath)
2018-09-19 09:52:44 +03:00
if err != nil {
t.Fatal(err)
}
// create form data
var body bytes.Buffer
writer := multipart.NewWriter(&body)
2018-09-19 10:19:35 +03:00
formFile, err := writer.CreateFormFile("file", filepath.Base(filePath))
2018-09-19 09:52:44 +03:00
if err != nil {
t.Fatal(err)
}
2018-09-19 10:19:35 +03:00
if _, err = io.Copy(formFile, bytes.NewReader(yamlFile)); err != nil {
2018-09-19 09:52:44 +03:00
t.Fatal(err)
}
writer.Close()
// create http post request
req, err := http.NewRequest(http.MethodPost, url, &body)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", writer.FormDataContentType())
// submit request
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
// check response
fmt.Println("== upload finish ==")
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
fmt.Println(res.StatusCode)
fmt.Println(res.Header)
res.Body.Close()
fmt.Println(string(resBody))
}