forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_test.go
114 lines (93 loc) · 3.07 KB
/
http_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
package http
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/vault/helper/jsonutil"
)
func testHttpGet(t *testing.T, token string, addr string) *http.Response {
t.Logf("Token is %s", token)
return testHttpData(t, "GET", token, addr, nil, false)
}
func testHttpDelete(t *testing.T, token string, addr string) *http.Response {
return testHttpData(t, "DELETE", token, addr, nil, false)
}
// Go 1.8+ clients redirect automatically which breaks our 307 standby testing
func testHttpDeleteDisableRedirect(t *testing.T, token string, addr string) *http.Response {
return testHttpData(t, "DELETE", token, addr, nil, true)
}
func testHttpPost(t *testing.T, token string, addr string, body interface{}) *http.Response {
return testHttpData(t, "POST", token, addr, body, false)
}
func testHttpPut(t *testing.T, token string, addr string, body interface{}) *http.Response {
return testHttpData(t, "PUT", token, addr, body, false)
}
// Go 1.8+ clients redirect automatically which breaks our 307 standby testing
func testHttpPutDisableRedirect(t *testing.T, token string, addr string, body interface{}) *http.Response {
return testHttpData(t, "PUT", token, addr, body, true)
}
func testHttpData(t *testing.T, method string, token string, addr string, body interface{}, disableRedirect bool) *http.Response {
bodyReader := new(bytes.Buffer)
if body != nil {
enc := json.NewEncoder(bodyReader)
if err := enc.Encode(body); err != nil {
t.Fatalf("err:%s", err)
}
}
req, err := http.NewRequest(method, addr, bodyReader)
if err != nil {
t.Fatalf("err: %s", err)
}
req.Header.Set("Content-Type", "application/json")
if len(token) != 0 {
req.Header.Set("X-Vault-Token", token)
}
client := cleanhttp.DefaultClient()
client.Timeout = 60 * time.Second
// From https://github.com/michiwend/gomusicbrainz/pull/4/files
defaultRedirectLimit := 30
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if disableRedirect {
return fmt.Errorf("checkRedirect disabled for test")
}
if len(via) > defaultRedirectLimit {
return fmt.Errorf("%d consecutive requests(redirects)", len(via))
}
if len(via) == 0 {
// No redirects
return nil
}
// mutate the subsequent redirect requests with the first Header
if token := via[0].Header.Get("X-Vault-Token"); len(token) != 0 {
req.Header.Set("X-Vault-Token", token)
}
return nil
}
resp, err := client.Do(req)
if err != nil && !strings.Contains(err.Error(), "checkRedirect disabled for test") {
t.Fatalf("err: %s", err)
}
return resp
}
func testResponseStatus(t *testing.T, resp *http.Response, code int) {
if resp.StatusCode != code {
body := new(bytes.Buffer)
io.Copy(body, resp.Body)
resp.Body.Close()
t.Fatalf(
"Expected status %d, got %d. Body:\n\n%s",
code, resp.StatusCode, body.String())
}
}
func testResponseBody(t *testing.T, resp *http.Response, out interface{}) {
defer resp.Body.Close()
if err := jsonutil.DecodeJSONFromReader(resp.Body, out); err != nil {
t.Fatalf("err: %s", err)
}
}