http: make TestServer public
This commit is contained in:
parent
f43a0290cf
commit
1bd0772986
|
@ -4,29 +4,10 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/vault/vault"
|
||||
)
|
||||
|
||||
func testServer(t *testing.T, core *vault.Core) (net.Listener, string) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
addr := "http://" + ln.Addr().String()
|
||||
|
||||
server := &http.Server{
|
||||
Addr: ln.Addr().String(),
|
||||
Handler: Handler(core),
|
||||
}
|
||||
go server.Serve(ln)
|
||||
|
||||
return ln, addr
|
||||
}
|
||||
|
||||
func testHttpPut(t *testing.T, addr string, body interface{}) *http.Response {
|
||||
bodyReader := new(bytes.Buffer)
|
||||
if body != nil {
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
func TestSysInit_get(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
{
|
||||
|
@ -55,7 +55,7 @@ func TestSysInit_get(t *testing.T) {
|
|||
|
||||
func TestSysInit_put(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
resp := testHttpPut(t, addr+"/v1/sys/init", map[string]interface{}{
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
func TestSysSealStatus(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
vault.TestCoreInit(t, core)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
resp, err := http.Get(addr + "/v1/sys/seal-status")
|
||||
|
@ -37,7 +37,7 @@ func TestSysSealStatus(t *testing.T) {
|
|||
func TestSysSeal(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
vault.TestCoreInit(t, core)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
resp := testHttpPut(t, addr+"/v1/sys/seal", nil)
|
||||
|
@ -54,7 +54,7 @@ func TestSysSeal(t *testing.T) {
|
|||
|
||||
func TestSysSeal_unsealed(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
keys := vault.TestCoreInit(t, core)
|
||||
|
@ -77,7 +77,7 @@ func TestSysSeal_unsealed(t *testing.T) {
|
|||
func TestSysUnseal(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
keys := vault.TestCoreInit(t, core)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
resp := testHttpPut(t, addr+"/v1/sys/unseal", map[string]interface{}{
|
||||
|
@ -101,7 +101,7 @@ func TestSysUnseal(t *testing.T) {
|
|||
func TestSysUnseal_badKey(t *testing.T) {
|
||||
core := vault.TestCore(t)
|
||||
vault.TestCoreInit(t, core)
|
||||
ln, addr := testServer(t, core)
|
||||
ln, addr := TestServer(t, core)
|
||||
defer ln.Close()
|
||||
|
||||
resp := testHttpPut(t, addr+"/v1/sys/unseal", map[string]interface{}{
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/vault/vault"
|
||||
)
|
||||
|
||||
func TestServer(t *testing.T, core *vault.Core) (net.Listener, string) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
addr := "http://" + ln.Addr().String()
|
||||
|
||||
server := &http.Server{
|
||||
Addr: ln.Addr().String(),
|
||||
Handler: Handler(core),
|
||||
}
|
||||
go server.Serve(ln)
|
||||
|
||||
return ln, addr
|
||||
}
|
Loading…
Reference in New Issue