diff --git a/http/logical_test.go b/http/logical_test.go index 402d7903be..1c454e96bd 100644 --- a/http/logical_test.go +++ b/http/logical_test.go @@ -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()) + } +} diff --git a/vault/testing.go b/vault/testing.go index 1a650db956..5bb4946eab 100644 --- a/vault/testing.go +++ b/vault/testing.go @@ -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) {}