http: make TestServer public

This commit is contained in:
Mitchell Hashimoto 2015-03-13 11:13:33 -07:00
parent f43a0290cf
commit 1bd0772986
4 changed files with 32 additions and 26 deletions

View File

@ -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 {

View File

@ -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{}{

View File

@ -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{}{

25
http/testing.go Normal file
View File

@ -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
}