Skip to content

Commit

Permalink
Merge pull request #226 from ipfs/feat/support_set_header
Browse files Browse the repository at this point in the history
allow header and set header in client
  • Loading branch information
Stebalien committed Feb 12, 2022
2 parents f42cf82 + 330a10b commit 40b8fdd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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 {
}
}

// 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, 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

0 comments on commit 40b8fdd

Please sign in to comment.