d229d7d5b0
* Redo the API client quite a bit to make the behavior of NewClient more predictable and add locking to make it safer to use with Clone() and if multiple goroutines for some reason decide to change things. Along the way I discovered that currently, the x/net/http2 package is broke with the built-in h2 support in released Go. For those using DefaultConfig (the vast majority of cases) this will be a non-event. Others can manually call http2.ConfigureTransport as needed. We should keep an eye on commits on that repo and consider more updates before release. Alternately we could go back revisions but miss out on bug fixes; my theory is that this is not a purposeful break and I'll be following up on this in the Go issue tracker. In a few tests that don't use NewTestCluster, either for legacy or other reasons, ensure that http2.ConfigureTransport is called. * Use tls config cloning * Don't http2.ConfigureServer anymore as current Go seems to work properly without requiring the http2 package * Address feedback
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
)
|
|
|
|
func TestListener(t *testing.T) (net.Listener, string) {
|
|
fail := func(format string, args ...interface{}) {
|
|
panic(fmt.Sprintf(format, args...))
|
|
}
|
|
if t != nil {
|
|
fail = t.Fatalf
|
|
}
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
fail("err: %s", err)
|
|
}
|
|
addr := "http://" + ln.Addr().String()
|
|
return ln, addr
|
|
}
|
|
|
|
func TestServerWithListener(t *testing.T, ln net.Listener, addr string, core *vault.Core) {
|
|
// Create a muxer to handle our requests so that we can authenticate
|
|
// for tests.
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/_test/auth", http.HandlerFunc(testHandleAuth))
|
|
mux.Handle("/", Handler(core))
|
|
|
|
server := &http.Server{
|
|
Addr: ln.Addr().String(),
|
|
Handler: mux,
|
|
}
|
|
go server.Serve(ln)
|
|
}
|
|
|
|
func TestServer(t *testing.T, core *vault.Core) (net.Listener, string) {
|
|
ln, addr := TestListener(t)
|
|
TestServerWithListener(t, ln, addr, core)
|
|
return ln, addr
|
|
}
|
|
|
|
func TestServerAuth(t *testing.T, addr string, token string) {
|
|
if _, err := http.Get(addr + "/_test/auth?token=" + token); err != nil {
|
|
t.Fatalf("error authenticating: %s", err)
|
|
}
|
|
}
|
|
|
|
func testHandleAuth(w http.ResponseWriter, req *http.Request) {
|
|
respondOk(w, nil)
|
|
}
|