Skip to content

allow header and set header in client #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type client struct {
httpClient *http.Client
ua string
apiPrefix string
headers map[string]string
fallback cmds.Executor
}

Expand All @@ -40,6 +41,16 @@ func ClientWithUserAgent(ua string) ClientOpt {
}
}

// ClientWithHeaders specifies the HTTP header for the client.
func ClientWithHeaders(key, value string) ClientOpt {
Comment on lines +44 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
// ClientWithHeaders specifies the HTTP header for the client.
func ClientWithHeaders(key, value string) ClientOpt {
// ClientWithHeader adds an HTTP header to the client.
func ClientWithHeader(key, value string) ClientOpt {

return func(c *client) {
if c.headers == nil {
c.headers = map[string]string{}
}
c.headers[key] = value
}
}

// ClientWithHTTPClient specifies a custom http.Client. Defaults to
// http.DefaultClient.
func ClientWithHTTPClient(hc *http.Client) ClientOpt {
Expand Down Expand Up @@ -173,6 +184,10 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
}
httpReq.Header.Set(uaHeader, c.ua)

for key, val := range c.headers {
httpReq.Header.Set(key, val)
}

httpReq = httpReq.WithContext(req.Context)
httpReq.Close = true

Expand Down
44 changes: 44 additions & 0 deletions http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,47 @@ func TestClientAPIPrefix(t *testing.T) {
}
}
}

func TestClientHeader(t *testing.T) {
type testcase struct {
host string
header string
value string
path []string
}

tcs := []testcase{
{header: "Authorization", value: "Bearer sdneijfnejvzfregfwe", path: []string{"version"}},
{header: "Content-Type", value: "text/plain", path: []string{"version"}},
}

for _, tc := range tcs {
var called bool

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
t.Log(r)

if token := r.Header.Get(tc.header); token != tc.value {
t.Errorf("expected authorization %q, got %q", tc.value, token)
}

expPath := "/" + strings.Join(tc.path, "/")
if path := r.URL.Path; path != expPath {
t.Errorf("expected path %q, got %q", expPath, path)
}

w.WriteHeader(http.StatusOK)
}))
testClient := s.Client()
tc.host = s.URL
r := &cmds.Request{Path: tc.path, Command: &cmds.Command{}, Root: &cmds.Command{}}
c := NewClient(tc.host, ClientWithHeaders(tc.header, tc.value)).(*client)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c := NewClient(tc.host, ClientWithHeaders(tc.header, tc.value)).(*client)
c := NewClient(tc.host, ClientWithHeader(tc.header, tc.value)).(*client)

c.httpClient = testClient
c.send(r)

if !called {
t.Error("handler has not been called")
}
}
}
6 changes: 6 additions & 0 deletions http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func (cfg *ServerConfig) SetAllowCredentials(flag bool) {
cfg.corsOpts.AllowCredentials = flag
}

func (cfg *ServerConfig) AddAllowedHeaders(headers ...string) {
cfg.corsOptsRWMutex.Lock()
defer cfg.corsOptsRWMutex.Unlock()
cfg.corsOpts.AllowedHeaders = append(cfg.corsOpts.AllowedHeaders, headers...)
}

// allowOrigin just stops the request if the origin is not allowed.
// the CORS middleware apparently does not do this for us...
func allowOrigin(r *http.Request, cfg *ServerConfig) bool {
Expand Down