Skip to content

Commit

Permalink
Merge pull request #9721 from Mongey/cm-headers
Browse files Browse the repository at this point in the history
Allow setting of headers in api client
  • Loading branch information
Mahmood Ali committed Jan 26, 2021
2 parents 0385b5d + 79a4895 commit efdcd34
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ type Config struct {
//
// TLSConfig is ignored if HttpClient is set.
TLSConfig *TLSConfig

Headers http.Header
}

// ClientConfig copies the configuration with a new client address, region, and
Expand Down Expand Up @@ -527,6 +529,7 @@ type request struct {
body io.Reader
obj interface{}
ctx context.Context
header http.Header
}

// setQueryOptions is used to annotate the request with
Expand Down Expand Up @@ -612,6 +615,8 @@ func (r *request) toHTTP() (*http.Request, error) {
return nil, err
}

req.Header = r.header

// Optionally configure HTTP basic authentication
if r.url.User != nil {
username := r.url.User.Username()
Expand Down Expand Up @@ -649,6 +654,7 @@ func (c *Client) newRequest(method, path string) (*request, error) {
Path: u.Path,
RawPath: u.RawPath,
},
header: make(http.Header),
params: make(map[string][]string),
}
if c.config.Region != "" {
Expand All @@ -671,6 +677,10 @@ func (c *Client) newRequest(method, path string) (*request, error) {
}
}

if c.config.Headers != nil {
r.header = c.config.Headers
}

return r, nil
}

Expand Down
16 changes: 16 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ func TestParseWriteMeta(t *testing.T) {
}
}

func TestClientHeader(t *testing.T) {
t.Parallel()
c, s := makeClient(t, func(c *Config) {
c.Headers = http.Header{
"Hello": []string{"World"},
}
}, nil)
defer s.Stop()

r, _ := c.newRequest("GET", "/v1/jobs")

if r.header.Get("Hello") != "World" {
t.Fatalf("bad: %v", r.header)
}
}

func TestQueryString(t *testing.T) {
t.Parallel()
c, s := makeClient(t, nil, nil)
Expand Down
10 changes: 10 additions & 0 deletions vendor/github.com/hashicorp/nomad/api/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit efdcd34

Please sign in to comment.