-
Notifications
You must be signed in to change notification settings - Fork 22
/
api_test.go
70 lines (61 loc) · 1.83 KB
/
api_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
. "github.com/smartystreets/goconvey/convey"
"github.com/Lupino/huabot-brain/models"
"testing"
"net/http"
"os"
"bytes"
"path/filepath"
"mime/multipart"
"io"
"encoding/json"
)
var host = "http://127.0.0.1:3000"
// Creates a new file upload http request with optional extra params
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
return req, err
}
func TestPostDataset(t *testing.T) {
Convey("POST /api/datasets/", t, func() {
path, _ := os.Getwd()
path += "/public/favicon.ico"
extraParams := map[string]string{
"tag": "test 1",
}
request, err := newfileUploadRequest(host + API + "/datasets", extraParams, "file", path)
So(err, ShouldBeNil)
client := new(http.Client)
resp, err := client.Do(request)
So(err, ShouldBeNil)
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
So(err, ShouldBeNil)
resp.Body.Close()
var data = make(map[string]models.Dataset)
json.Unmarshal(body.Bytes(), &data)
var dataset = data["dataset"]
So(dataset.Tag.Name, ShouldEqual, "test 1")
})
}