open-vault/http/testing.go
hghaf099 ad2ef412cc
Customizing HTTP headers in the config file (#12485)
* Customizing HTTP headers in the config file

* Add changelog, fix bad imports

* fixing some bugs

* fixing interaction of custom headers and /ui

* Defining a member in core to set custom response headers

* missing additional file

* Some refactoring

* Adding automated tests for the feature

* Changing some error messages based on some recommendations

* Incorporating custom response headers struct into the request context

* removing some unused references

* fixing a test

* changing some error messages, removing a default header value from /ui

* fixing a test

* wrapping ResponseWriter to set the custom headers

* adding a new test

* some cleanup

* removing some extra lines

* Addressing comments

* fixing some agent tests

* skipping custom headers from agent listener config,
removing two of the default headers as they cause issues with Vault in UI mode
Adding X-Content-Type-Options to the ui default headers
Let Content-Type be set as before

* Removing default custom headers, and renaming some function varibles

* some refacotring

* Refactoring and addressing comments

* removing a function and fixing comments
2021-10-13 11:06:33 -04:00

74 lines
1.9 KiB
Go

package http
import (
"fmt"
"net"
"net/http"
"testing"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/vault"
)
func TestListener(tb testing.TB) (net.Listener, string) {
fail := func(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
if tb != nil {
fail = tb.Fatalf
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
fail("err: %s", err)
}
addr := "http://" + ln.Addr().String()
return ln, addr
}
func TestServerWithListenerAndProperties(tb testing.TB, ln net.Listener, addr string, core *vault.Core, props *vault.HandlerProperties) {
// Create a muxer to handle our requests so that we can authenticate
// for tests.
mux := http.NewServeMux()
mux.Handle("/_test/auth", http.HandlerFunc(testHandleAuth))
mux.Handle("/", Handler(props))
server := &http.Server{
Addr: ln.Addr().String(),
Handler: mux,
ErrorLog: core.Logger().StandardLogger(nil),
}
go server.Serve(ln)
}
func TestServerWithListener(tb testing.TB, ln net.Listener, addr string, core *vault.Core) {
ip, _, _ := net.SplitHostPort(ln.Addr().String())
// Create a muxer to handle our requests so that we can authenticate
// for tests.
props := &vault.HandlerProperties{
Core: core,
// This is needed for testing custom response headers
ListenerConfig: &configutil.Listener {
Address: ip,
},
}
TestServerWithListenerAndProperties(tb, ln, addr, core, props)
}
func TestServer(tb testing.TB, core *vault.Core) (net.Listener, string) {
ln, addr := TestListener(tb)
TestServerWithListener(tb, ln, addr, core)
return ln, addr
}
func TestServerAuth(tb testing.TB, addr string, token string) {
if _, err := http.Get(addr + "/_test/auth?token=" + token); err != nil {
tb.Fatalf("error authenticating: %s", err)
}
}
func testHandleAuth(w http.ResponseWriter, req *http.Request) {
respondOk(w, nil)
}