-
Notifications
You must be signed in to change notification settings - Fork 3
/
teedytest.go
75 lines (56 loc) · 1.62 KB
/
teedytest.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
package teedytest
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/MattHodge/go-teedy/teedy"
"github.com/jarcoal/httpmock"
)
const (
TeedyUsername = "admin"
TeedyPassword = "admin"
SkippingIntegrationMessage = "skipping integration test"
)
func SetupClient(t *testing.T) *teedy.Client {
teedyUrl := os.Getenv("TEEDY_URL")
if teedyUrl == "" {
t.Fatalf("cannot run test because TEEDY_URL environment variable not set")
}
client, err := teedy.NewClient(teedyUrl, TeedyUsername, TeedyPassword)
if err != nil {
t.Fatalf("cannot run test because unable to get a new client: %v", err)
}
return client
}
// NewJsonResponder returns a JSON responder for httpmock.
func NewJsonResponder(s int, c string) httpmock.Responder {
resp := httpmock.NewStringResponse(s, c)
resp.Header.Set("Content-Type", "application/json")
return httpmock.ResponderFromResponse(resp)
}
func LoadFile(t *testing.T, path string) *os.File {
file, err := os.Open(path)
if err != nil {
t.Skipf("skipping test because unable unable to open file '%s': %v", path, err)
}
return file
}
func GetFileContents(t *testing.T, file string) []byte {
b, err := ioutil.ReadFile(file)
if err != nil {
t.Skipf("skipping test because unable unable to read file: %s", err)
}
return b
}
func WriteToFile(t *testing.T, path string, content string) {
basePath := filepath.Dir(path)
err := os.MkdirAll(basePath, 0700)
if err != nil {
t.Skipf("cannot create directory %s: %s", basePath, err)
}
err = ioutil.WriteFile(path, []byte(content), 0644)
if err != nil {
t.Skipf("cannot write file %s: %s", path, err)
}
}