open-vault/http/http_test.go

121 lines
3.4 KiB
Go
Raw Normal View History

2015-03-12 17:46:45 +00:00
package http
import (
2015-03-12 18:12:44 +00:00
"bytes"
2015-03-12 17:46:45 +00:00
"encoding/json"
2015-08-22 00:36:19 +00:00
"fmt"
2015-03-12 18:12:44 +00:00
"io"
2015-03-12 17:46:45 +00:00
"net/http"
2017-06-17 04:04:55 +00:00
"regexp"
2017-01-17 20:43:10 +00:00
"strings"
2015-03-12 17:46:45 +00:00
"testing"
"time"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/vault/helper/jsonutil"
2015-03-12 17:46:45 +00:00
)
2015-08-22 00:36:19 +00:00
func testHttpGet(t *testing.T, token string, addr string) *http.Response {
t.Logf("Token is %s", token)
2017-01-17 20:43:10 +00:00
return testHttpData(t, "GET", token, addr, nil, false)
2015-03-16 17:41:08 +00:00
}
2015-08-22 00:36:19 +00:00
func testHttpDelete(t *testing.T, token string, addr string) *http.Response {
2017-01-17 20:43:10 +00:00
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)
2015-03-16 17:36:29 +00:00
}
2015-08-22 00:36:19 +00:00
func testHttpPost(t *testing.T, token string, addr string, body interface{}) *http.Response {
2017-01-17 20:43:10 +00:00
return testHttpData(t, "POST", token, addr, body, false)
2015-03-16 17:36:29 +00:00
}
2015-08-22 00:36:19 +00:00
func testHttpPut(t *testing.T, token string, addr string, body interface{}) *http.Response {
2017-01-17 20:43:10 +00:00
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)
2015-08-22 00:36:19 +00:00
}
2017-01-17 20:43:10 +00:00
func testHttpData(t *testing.T, method string, token string, addr string, body interface{}, disableRedirect bool) *http.Response {
2015-03-12 18:12:44 +00:00
bodyReader := new(bytes.Buffer)
if body != nil {
enc := json.NewEncoder(bodyReader)
if err := enc.Encode(body); err != nil {
t.Fatalf("err:%s", err)
}
}
2015-03-16 17:36:29 +00:00
req, err := http.NewRequest(method, addr, bodyReader)
2015-03-12 18:12:44 +00:00
if err != nil {
t.Fatalf("err: %s", err)
}
2017-06-17 04:04:55 +00:00
// Get the address of the local listener in order to attach it to an Origin header.
// This will allow for the testing of requests that require CORS, without using a browser.
hostURLRegexp, _ := regexp.Compile("http[s]?://.+:[0-9]+")
req.Header.Set("Origin", hostURLRegexp.FindString(addr))
2015-03-12 18:12:44 +00:00
req.Header.Set("Content-Type", "application/json")
2015-08-22 00:36:19 +00:00
if len(token) != 0 {
req.Header.Set("X-Vault-Token", token)
}
client := cleanhttp.DefaultClient()
client.Timeout = 60 * time.Second
2015-08-22 00:36:19 +00:00
// From https://github.com/michiwend/gomusicbrainz/pull/4/files
defaultRedirectLimit := 30
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
2017-01-17 20:43:10 +00:00
if disableRedirect {
return fmt.Errorf("checkRedirect disabled for test")
}
2015-08-22 00:36:19 +00:00
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)
2017-01-17 20:43:10 +00:00
if err != nil && !strings.Contains(err.Error(), "checkRedirect disabled for test") {
2015-03-12 18:12:44 +00:00
t.Fatalf("err: %s", err)
}
return resp
}
2015-03-12 17:46:45 +00:00
func testResponseStatus(t *testing.T, resp *http.Response, code int) {
if resp.StatusCode != code {
2015-03-12 18:12:44 +00:00
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())
2015-03-12 17:46:45 +00:00
}
}
func testResponseBody(t *testing.T, resp *http.Response, out interface{}) {
defer resp.Body.Close()
if err := jsonutil.DecodeJSONFromReader(resp.Body, out); err != nil {
2015-03-12 17:46:45 +00:00
t.Fatalf("err: %s", err)
}
}