Skip to content

Commit 40b8fdd

Browse files
authored
Merge pull request #226 from ipfs/feat/support_set_header
allow header and set header in client
2 parents f42cf82 + 330a10b commit 40b8fdd

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

http/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type client struct {
2727
httpClient *http.Client
2828
ua string
2929
apiPrefix string
30+
headers map[string]string
3031
fallback cmds.Executor
3132
}
3233

@@ -40,6 +41,16 @@ func ClientWithUserAgent(ua string) ClientOpt {
4041
}
4142
}
4243

44+
// ClientWithHeader adds an HTTP header to the client.
45+
func ClientWithHeader(key, value string) ClientOpt {
46+
return func(c *client) {
47+
if c.headers == nil {
48+
c.headers = map[string]string{}
49+
}
50+
c.headers[key] = value
51+
}
52+
}
53+
4354
// ClientWithHTTPClient specifies a custom http.Client. Defaults to
4455
// http.DefaultClient.
4556
func ClientWithHTTPClient(hc *http.Client) ClientOpt {
@@ -173,6 +184,10 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
173184
}
174185
httpReq.Header.Set(uaHeader, c.ua)
175186

187+
for key, val := range c.headers {
188+
httpReq.Header.Set(key, val)
189+
}
190+
176191
httpReq = httpReq.WithContext(req.Context)
177192
httpReq.Close = true
178193

http/client_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,47 @@ func TestClientAPIPrefix(t *testing.T) {
9191
}
9292
}
9393
}
94+
95+
func TestClientHeader(t *testing.T) {
96+
type testcase struct {
97+
host string
98+
header string
99+
value string
100+
path []string
101+
}
102+
103+
tcs := []testcase{
104+
{header: "Authorization", value: "Bearer sdneijfnejvzfregfwe", path: []string{"version"}},
105+
{header: "Content-Type", value: "text/plain", path: []string{"version"}},
106+
}
107+
108+
for _, tc := range tcs {
109+
var called bool
110+
111+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
112+
called = true
113+
t.Log(r)
114+
115+
if token := r.Header.Get(tc.header); token != tc.value {
116+
t.Errorf("expected authorization %q, got %q", tc.value, token)
117+
}
118+
119+
expPath := "/" + strings.Join(tc.path, "/")
120+
if path := r.URL.Path; path != expPath {
121+
t.Errorf("expected path %q, got %q", expPath, path)
122+
}
123+
124+
w.WriteHeader(http.StatusOK)
125+
}))
126+
testClient := s.Client()
127+
tc.host = s.URL
128+
r := &cmds.Request{Path: tc.path, Command: &cmds.Command{}, Root: &cmds.Command{}}
129+
c := NewClient(tc.host, ClientWithHeader(tc.header, tc.value)).(*client)
130+
c.httpClient = testClient
131+
c.send(r)
132+
133+
if !called {
134+
t.Error("handler has not been called")
135+
}
136+
}
137+
}

http/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ func (cfg *ServerConfig) SetAllowCredentials(flag bool) {
8585
cfg.corsOpts.AllowCredentials = flag
8686
}
8787

88+
func (cfg *ServerConfig) AddAllowedHeaders(headers ...string) {
89+
cfg.corsOptsRWMutex.Lock()
90+
defer cfg.corsOptsRWMutex.Unlock()
91+
cfg.corsOpts.AllowedHeaders = append(cfg.corsOpts.AllowedHeaders, headers...)
92+
}
93+
8894
// allowOrigin just stops the request if the origin is not allowed.
8995
// the CORS middleware apparently does not do this for us...
9096
func allowOrigin(r *http.Request, cfg *ServerConfig) bool {

0 commit comments

Comments
 (0)