consul/agent/http_oss_test.go

156 lines
4.1 KiB
Go
Raw Normal View History

2017-11-29 00:06:26 +00:00
package agent
import (
"fmt"
"net"
2017-11-29 00:06:26 +00:00
"net/http"
"net/http/httptest"
2017-11-29 00:06:26 +00:00
"strings"
"testing"
"time"
2017-11-29 00:06:26 +00:00
"github.com/hashicorp/consul/logger"
)
2018-03-03 19:19:18 +00:00
// extra endpoints that should be tested, and their allowed methods
var extraTestEndpoints = map[string][]string{
"/v1/query": []string{"GET", "POST"},
"/v1/query/": []string{"GET", "PUT", "DELETE"},
"/v1/query/xxx/execute": []string{"GET"},
"/v1/query/xxx/explain": []string{"GET"},
}
2018-03-16 14:36:57 +00:00
// These endpoints are ignored in unit testing for response codes
2018-03-27 20:31:27 +00:00
var ignoredEndpoints = []string{"/v1/status/peers", "/v1/agent/monitor", "/v1/agent/reload"}
2018-03-16 14:36:57 +00:00
// These have custom logic
var customEndpoints = []string{"/v1/query", "/v1/query/"}
// includePathInTest returns whether this path should be ignored for the purpose of testing its response code
2018-03-03 19:19:18 +00:00
func includePathInTest(path string) bool {
2018-03-16 14:36:57 +00:00
ignored := false
for _, p := range ignoredEndpoints {
if p == path {
ignored = true
break
}
}
for _, p := range customEndpoints {
if p == path {
ignored = true
break
}
}
return !ignored
}
func newHttpClient(timeout time.Duration) *http.Client {
return &http.Client{
Timeout: timeout,
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: timeout,
}).Dial,
TLSHandshakeTimeout: timeout,
},
}
}
2017-11-29 00:06:26 +00:00
func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`)
a.Agent.LogWriter = logger.NewLogWriter(512)
defer a.Shutdown()
all := []string{"GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS"}
const testTimeout = 10 * time.Second
fastClient := newHttpClient(10 * time.Second)
slowClient := newHttpClient(30 * time.Second)
2017-11-29 00:06:26 +00:00
2018-03-03 19:19:18 +00:00
testMethodNotAllowed := func(method string, path string, allowedMethods []string) {
t.Run(method+" "+path, func(t *testing.T) {
client := fastClient
if path == "/v1/agent/leave" {
// there are actual sleeps in this code that should take longer
client = slowClient
t.Logf("Using slow http client for leave tests")
}
2018-03-03 19:19:18 +00:00
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
req, _ := http.NewRequest(method, uri, nil)
resp, err := client.Do(req)
if err != nil {
t.Fatal("client.Do failed: ", err)
}
defer resp.Body.Close()
2017-11-29 00:06:26 +00:00
2018-03-03 19:19:18 +00:00
allowed := method == "OPTIONS"
for _, allowedMethod := range allowedMethods {
if allowedMethod == method {
allowed = true
break
2017-11-29 00:06:26 +00:00
}
2018-03-03 19:19:18 +00:00
}
if allowed && resp.StatusCode == http.StatusMethodNotAllowed {
t.Fatalf("method allowed: got status code %d want any other code", resp.StatusCode)
}
if !allowed && resp.StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("method not allowed: got status code %d want %d", resp.StatusCode, http.StatusMethodNotAllowed)
}
})
}
for path, methods := range extraTestEndpoints {
for _, method := range all {
testMethodNotAllowed(method, path, methods)
}
}
for path, methods := range allowedMethods {
if includePathInTest(path) {
for _, method := range all {
testMethodNotAllowed(method, path, methods)
}
2017-11-29 00:06:26 +00:00
}
}
}
func TestHTTPAPI_OptionMethod_OSS(t *testing.T) {
a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`)
a.Agent.LogWriter = logger.NewLogWriter(512)
defer a.Shutdown()
2018-03-03 19:19:18 +00:00
testOptionMethod := func(path string, methods []string) {
t.Run("OPTIONS "+path, func(t *testing.T) {
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
req, _ := http.NewRequest("OPTIONS", uri, nil)
resp := httptest.NewRecorder()
a.srv.Handler.ServeHTTP(resp, req)
2018-03-03 19:19:18 +00:00
allMethods := append([]string{"OPTIONS"}, methods...)
if resp.Code != http.StatusOK {
t.Fatalf("options request: got status code %d want %d", resp.Code, http.StatusOK)
}
optionsStr := resp.Header().Get("Allow")
if optionsStr == "" {
t.Fatalf("options request: got empty 'Allow' header")
2018-03-03 19:19:18 +00:00
} else if optionsStr != strings.Join(allMethods, ",") {
t.Fatalf("options request: got 'Allow' header value of %s want %s", optionsStr, allMethods)
}
})
}
2018-03-03 19:19:18 +00:00
for path, methods := range extraTestEndpoints {
testOptionMethod(path, methods)
}
for path, methods := range allowedMethods {
if includePathInTest(path) {
testOptionMethod(path, methods)
}
}
}