2015-03-13 18:13:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2015-03-15 23:52:19 +00:00
|
|
|
"fmt"
|
2015-03-13 18:13:33 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-13 15:06:33 +00:00
|
|
|
"github.com/hashicorp/vault/internalshared/configutil"
|
2015-03-13 18:13:33 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2017-09-05 03:51:48 +00:00
|
|
|
func TestListener(tb testing.TB) (net.Listener, string) {
|
2015-03-15 23:52:19 +00:00
|
|
|
fail := func(format string, args ...interface{}) {
|
|
|
|
panic(fmt.Sprintf(format, args...))
|
|
|
|
}
|
2017-09-05 03:51:48 +00:00
|
|
|
if tb != nil {
|
|
|
|
fail = tb.Fatalf
|
2015-03-15 23:52:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 18:13:33 +00:00
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
2015-03-15 23:52:19 +00:00
|
|
|
fail("err: %s", err)
|
2015-03-13 18:13:33 +00:00
|
|
|
}
|
|
|
|
addr := "http://" + ln.Addr().String()
|
2015-04-19 20:17:25 +00:00
|
|
|
return ln, addr
|
|
|
|
}
|
2015-03-13 18:13:33 +00:00
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
func TestServerWithListenerAndProperties(tb testing.TB, ln net.Listener, addr string, core *vault.Core, props *vault.HandlerProperties) {
|
2015-03-29 23:14:54 +00:00
|
|
|
// Create a muxer to handle our requests so that we can authenticate
|
|
|
|
// for tests.
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/_test/auth", http.HandlerFunc(testHandleAuth))
|
2018-07-12 20:29:36 +00:00
|
|
|
mux.Handle("/", Handler(props))
|
2015-03-29 23:14:54 +00:00
|
|
|
|
2015-03-13 18:13:33 +00:00
|
|
|
server := &http.Server{
|
2018-08-21 15:23:18 +00:00
|
|
|
Addr: ln.Addr().String(),
|
|
|
|
Handler: mux,
|
|
|
|
ErrorLog: core.Logger().StandardLogger(nil),
|
2015-03-13 18:13:33 +00:00
|
|
|
}
|
|
|
|
go server.Serve(ln)
|
2015-04-19 20:17:25 +00:00
|
|
|
}
|
2015-03-13 18:13:33 +00:00
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
func TestServerWithListener(tb testing.TB, ln net.Listener, addr string, core *vault.Core) {
|
2021-10-13 15:06:33 +00:00
|
|
|
ip, _, _ := net.SplitHostPort(ln.Addr().String())
|
|
|
|
|
2018-07-12 20:29:36 +00:00
|
|
|
// Create a muxer to handle our requests so that we can authenticate
|
|
|
|
// for tests.
|
|
|
|
props := &vault.HandlerProperties{
|
2020-05-14 13:19:27 +00:00
|
|
|
Core: core,
|
2021-10-13 15:06:33 +00:00
|
|
|
// This is needed for testing custom response headers
|
|
|
|
ListenerConfig: &configutil.Listener {
|
|
|
|
Address: ip,
|
|
|
|
},
|
2018-07-12 20:29:36 +00:00
|
|
|
}
|
|
|
|
TestServerWithListenerAndProperties(tb, ln, addr, core, props)
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:51:48 +00:00
|
|
|
func TestServer(tb testing.TB, core *vault.Core) (net.Listener, string) {
|
|
|
|
ln, addr := TestListener(tb)
|
|
|
|
TestServerWithListener(tb, ln, addr, core)
|
2015-03-13 18:13:33 +00:00
|
|
|
return ln, addr
|
|
|
|
}
|
2015-03-29 23:14:54 +00:00
|
|
|
|
2017-09-05 03:51:48 +00:00
|
|
|
func TestServerAuth(tb testing.TB, addr string, token string) {
|
2015-03-29 23:14:54 +00:00
|
|
|
if _, err := http.Get(addr + "/_test/auth?token=" + token); err != nil {
|
2017-09-05 03:51:48 +00:00
|
|
|
tb.Fatalf("error authenticating: %s", err)
|
2015-03-29 23:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testHandleAuth(w http.ResponseWriter, req *http.Request) {
|
|
|
|
respondOk(w, nil)
|
|
|
|
}
|