open-vault/http/testing.go

34 lines
558 B
Go
Raw Normal View History

2015-03-13 18:13:33 +00:00
package http
import (
"fmt"
2015-03-13 18:13:33 +00:00
"net"
"net/http"
"testing"
"github.com/hashicorp/vault/vault"
)
func TestServer(t *testing.T, core *vault.Core) (net.Listener, string) {
fail := func(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
if t != nil {
fail = t.Fatalf
}
2015-03-13 18:13:33 +00:00
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fail("err: %s", err)
2015-03-13 18:13:33 +00:00
}
addr := "http://" + ln.Addr().String()
server := &http.Server{
Addr: ln.Addr().String(),
Handler: Handler(core),
}
go server.Serve(ln)
return ln, addr
}