-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvault_client_test.go
68 lines (54 loc) · 1.3 KB
/
vault_client_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
package main
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
type MockClient struct {
method string
body []byte
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
m.method = req.Method
m.body, _ = ioutil.ReadAll(req.Body)
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("{}")),
StatusCode: 200,
Status: "200 OK",
}, nil
}
func TestWrite(t *testing.T) {
muck := &MockClient{}
var client = &VaultClient{
http: muck,
}
data := map[string]int{"one": 1,}
client.Write("secret", data)
if muck.method != "PUT" {
t.Fatalf("Expected method to be PUT, but got %s", muck.method)
}
if string(muck.body) != `{"one":1}` {
t.Fatalf("Body is not expected, got %s", string(muck.body))
}
}
func TestWriteEmpty(t *testing.T) {
muck := &MockClient{}
var client = &VaultClient{
http: muck,
}
data := map[string]int{}
client.Write("secret", data)
if muck.method != "PUT" {
t.Fatalf("Expected method to be PUT, but got %s", muck.method)
}
if string(muck.body) != `{"":""}` {
t.Fatalf("Expected an empty body, but got %s", string(muck.body))
}
}
func TestNewVaultClient(t *testing.T) {
client := NewVaultClient("1.11.2.3:88", "")
if client.address != "http://1.11.2.3:88" {
t.Fatalf("Expected a vault address, but got %s", client.address)
}
}