-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.go
87 lines (72 loc) · 1.99 KB
/
test_helpers.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package handlers
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func PerformRequest(r http.Handler, method, relativePath, body string) *httptest.ResponseRecorder {
return PerformRequestFull(r, method, "/"+viper.GetString("apiVersion")+relativePath, body)
}
func PerformRequestFull(r http.Handler, method, fullpath, body string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, fullpath, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set(viper.GetString("auth.apiKey.key"), viper.GetString("auth.apiKey.value"))
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func UploadFile(r http.Handler, method, p string, localPath string, remotePath string) (*httptest.ResponseRecorder, error) {
webpath := path.Join("/", viper.GetString("apiVersion"), p)
var b bytes.Buffer
w := multipart.NewWriter(&b)
f, err := os.Open(path.Join("..", localPath))
if err != nil {
return nil, err
}
defer f.Close()
fw, err := w.CreateFormFile("file", filepath.Base(localPath))
if err != nil {
return nil, err
}
_, err = io.Copy(fw, f)
if err != nil {
return nil, err
}
fw, err = w.CreateFormField("path0")
if err != nil {
return nil, err
}
_, err = fw.Write([]byte(remotePath))
if err != nil {
return nil, err
}
w.Close()
req, err := http.NewRequest(method, webpath, &b)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set(viper.GetString("auth.apiKey.key"), viper.GetString("auth.apiKey.value"))
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
return recorder, nil
}
func AssertFileExists(t *testing.T, p string) {
info, err := os.Stat(p)
assert.Nil(t, err)
assert.False(t, info.IsDir())
}
func AssertDirExists(t *testing.T, p string) {
info, err := os.Stat(p)
assert.Nil(t, err)
assert.True(t, info.IsDir())
}