vault: testing raw responses

This commit is contained in:
Armon Dadgar 2015-05-27 14:19:12 -07:00
parent aa548cf901
commit bc262df2aa
2 changed files with 55 additions and 0 deletions

View file

@ -1,6 +1,8 @@
package http
import (
"bytes"
"io"
"net/http"
"reflect"
"testing"
@ -182,3 +184,34 @@ func TestLogical_CreateToken(t *testing.T) {
t.Fatalf("should not get cookies: %#v", cookies)
}
}
func TestLogical_RawHTTP(t *testing.T) {
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := TestServer(t, core)
defer ln.Close()
TestServerAuth(t, addr, token)
resp := testHttpPost(t, addr+"/v1/sys/mounts/foo", map[string]interface{}{
"type": "http",
})
testResponseStatus(t, resp, 204)
// Get the raw response
resp, err := http.Get(addr + "/v1/foo/raw")
if err != nil {
t.Fatalf("err: %s", err)
}
testResponseStatus(t, resp, 200)
// Test the headers
if resp.Header.Get("Content-Type") != "plain/text" {
t.Fatalf("Bad: %#v", resp.Header)
}
// Get the body
body := new(bytes.Buffer)
io.Copy(body, resp.Body)
if string(body.Bytes()) != "hello world" {
t.Fatalf("Bad: %s", body.Bytes())
}
}

View file

@ -1,6 +1,7 @@
package vault
import (
"log"
"testing"
"github.com/hashicorp/vault/audit"
@ -23,6 +24,9 @@ func TestCore(t *testing.T) *Core {
noopBackends["noop"] = func(map[string]string) (logical.Backend, error) {
return new(framework.Backend), nil
}
noopBackends["http"] = func(map[string]string) (logical.Backend, error) {
return new(rawHTTP), nil
}
physicalBackend := physical.NewInmem()
c, err := NewCore(&CoreConfig{
@ -89,3 +93,21 @@ func (n *noopAudit) LogRequest(a *logical.Auth, r *logical.Request) error {
func (n *noopAudit) LogResponse(a *logical.Auth, r *logical.Request, re *logical.Response, err error) error {
return nil
}
type rawHTTP struct{}
func (n *rawHTTP) HandleRequest(req *logical.Request) (*logical.Response, error) {
return &logical.Response{
Data: map[string]interface{}{
logical.HTTPStatusCode: 200,
logical.HTTPContentType: "plain/text",
logical.HTTPRawBody: []byte("hello world"),
},
}, nil
}
func (n *rawHTTP) SpecialPaths() *logical.Paths {
return &logical.Paths{Unauthenticated: []string{"*"}}
}
func (n *rawHTTP) SetLogger(l *log.Logger) {}