Allow setting arbitrary headers in API client

This commit is contained in:
Conor Mongey 2021-01-04 23:46:00 +00:00
parent 13706b9be4
commit 9064930f08
No known key found for this signature in database
GPG Key ID: 5C886ACC44EB17C0
2 changed files with 22 additions and 0 deletions

View File

@ -314,6 +314,8 @@ type Config struct {
Namespace string
TLSConfig TLSConfig
Headers map[string]string
}
// TLSConfig is used to generate a TLSClientConfig that's useful for talking to
@ -855,6 +857,10 @@ func (c *Client) newRequest(method, path string) *request {
params: make(map[string][]string),
header: make(http.Header),
}
for k, v := range c.config.Headers {
r.header.Set(k, v)
}
if c.config.Datacenter != "" {
r.params.Set("dc", c.config.Datacenter)
}

View File

@ -808,6 +808,22 @@ func TestAPI_SetWriteOptions(t *testing.T) {
}
}
func TestAPI_Headers(t *testing.T) {
t.Parallel()
c, s := makeClientWithConfig(t, func(c *Config) {
c.Headers = map[string]string{
"Hello": "World",
}
}, nil)
defer s.Stop()
r := c.newRequest("GET", "/v1/kv/foo")
if r.header.Get("Hello") != "World" {
t.Fatalf("bad: %v", r.header)
}
}
func TestAPI_RequestToHTTP(t *testing.T) {
t.Parallel()
c, s := makeClient(t)