http: tests

This commit is contained in:
Mitchell Hashimoto 2015-03-12 10:46:45 -07:00
parent 718065c733
commit 562ef24d83
2 changed files with 98 additions and 0 deletions

66
http/http_test.go Normal file
View File

@ -0,0 +1,66 @@
package http
import (
"encoding/json"
"net"
"net/http"
"testing"
"github.com/hashicorp/vault/physical"
"github.com/hashicorp/vault/vault"
)
func testCore(t *testing.T) *vault.Core {
physicalBackend := physical.NewInmem()
c, err := vault.NewCore(&vault.CoreConfig{
Physical: physicalBackend,
})
if err != nil {
t.Fatalf("err: %s", err)
}
return c
}
func testCoreInit(t *testing.T, core *vault.Core) [][]byte {
result, err := core.Initialize(&vault.SealConfig{
SecretShares: 1,
SecretThreshold: 1,
})
if err != nil {
t.Fatalf("err: %s", err)
}
return result.SecretShares
}
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 testResponseStatus(t *testing.T, resp *http.Response, code int) {
if resp.StatusCode != code {
t.Fatalf("Expected status %d, got %d", code, resp.StatusCode)
}
}
func testResponseBody(t *testing.T, resp *http.Response, out interface{}) {
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(out); err != nil {
t.Fatalf("err: %s", err)
}
}

32
http/sys_seal_test.go Normal file
View File

@ -0,0 +1,32 @@
package http
import (
"net/http"
"reflect"
"testing"
)
func TestSysSealStatus(t *testing.T) {
core := testCore(t)
testCoreInit(t, core)
ln, addr := testServer(t, core)
defer ln.Close()
resp, err := http.Get(addr + "/sys/seal-status")
if err != nil {
t.Fatalf("err: %s", err)
}
var actual map[string]interface{}
expected := map[string]interface{}{
"sealed": true,
"t": float64(1),
"n": float64(1),
"progress": float64(0),
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}