forked from mch1307/vaultlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
169 lines (162 loc) · 4.16 KB
/
request_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package vaultlib
import (
"encoding/json"
"net/http"
"net/url"
"os"
"reflect"
"testing"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)
func Test_newRequest(t *testing.T) {
cli, _ := NewClient(nil)
testURL, _ := url.Parse("http://localhost:8200")
emptyReq := new(request)
type args struct {
method string
token string
url *url.URL
}
tests := []struct {
name string
args args
want *request
wantErr bool
}{
{"err", args{"@", "", testURL}, emptyReq, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := cli.newRequest(tt.args.method, tt.args.url.String())
if (err != nil) != tt.wantErr {
t.Errorf("newRequest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("newRequest() = %v, want %v", got, tt.want)
}
})
}
}
func Test_request_execute(t *testing.T) {
nReq, _ := http.NewRequest("GET", "hppT:/localhost.18200", nil)
htcli := cleanhttp.DefaultPooledClient()
var rsp vaultResponse
type fields struct {
Req *http.Request
HTTPClient *http.Client
Headers http.Header
Token string
}
tests := []struct {
name string
fields fields
want vaultResponse
wantErr bool
}{
{"err", fields{nReq, htcli, nil, ""}, rsp, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &request{
Req: tt.fields.Req,
HTTPClient: tt.fields.HTTPClient,
Headers: tt.fields.Headers,
Token: tt.fields.Token,
}
got, err := r.execute()
if (err != nil) != tt.wantErr {
t.Errorf("request.execute() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("request.execute() = %v, want %v", got, tt.want)
}
})
}
}
func Test_request_setJSONBody(t *testing.T) {
var cred AppRoleCredentials
cli, _ := NewClient(nil)
cred.RoleID = "aa"
cred.SecretID = "bb"
htCli := new(http.Client)
url, _ := url.Parse("http://localhot:8200")
req, _ := cli.newRequest("GET", url.String())
ch := make(chan int)
type fields struct {
Req *http.Request
HTTPClient *http.Client
Headers http.Header
Token string
}
type args struct {
val interface{}
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{"noJSON", fields{req.Req, htCli, nil, ""}, args{ch}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &request{
Req: tt.fields.Req,
HTTPClient: tt.fields.HTTPClient,
Headers: tt.fields.Headers,
Token: tt.fields.Token,
}
if err := r.setJSONBody(tt.args.val); (err != nil) != tt.wantErr {
t.Errorf("request.setJSONBody() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestClient_RawRequest(t *testing.T) {
_ = os.Unsetenv("VAULT_TOKEN")
conf := NewConfig()
conf.AppRoleCredentials.RoleID = vaultRoleID
conf.AppRoleCredentials.SecretID = vaultSecretID
vc, err := NewClient(conf)
if err != nil {
t.Errorf("Failed to get vault cli %v", err)
}
vc.token.ID = "my-dev-root-vault-token"
ch := make(chan int)
type args struct {
method string
path string
payload interface{}
}
tests := []struct {
name string
cli *Client
args args
wantResult json.RawMessage
wantErr bool
}{
{"initEndpoint", vc, args{"GET", "/v1/sys/init", nil}, []byte(`{"initialized":true}
`), false},
{"badEndpoint", vc, args{"GET", "/v1/wrong/path", nil}, []byte(`{"errors":["no handler for route 'wrong/path'"]}
`), true},
{"invalidBody", vc, args{"GET", "/v1/sys/init", ch}, nil, true},
{"noMethod", vc, args{"", "/v1/sys/init", ch}, nil, true},
{"invalidMethod", vc, args{"@", "/v1/sys/init", ch}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.cli
gotResult, err := c.RawRequest(tt.args.method, tt.args.path, tt.args.payload)
if (err != nil) != tt.wantErr {
t.Errorf("Client.RawRequest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(string(gotResult), string(tt.wantResult)) {
t.Errorf("Client.RawRequest() = %v, want %v", string(gotResult), string(tt.wantResult))
}
})
}
}