Skip to content

Commit 9c9c23d

Browse files
committed
add test
1 parent f018bab commit 9c9c23d

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

http/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func ClientWithUserAgent(ua string) ClientOpt {
4141
}
4242
}
4343

44-
// ClientWithUserAgent specifies the HTTP user agent for the client.
44+
// ClientWithHeaders specifies the HTTP header for the client.
4545
func ClientWithHeaders(key, value string) ClientOpt {
4646
return func(c *client) {
4747
if c.headers == nil {

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, ClientWithHeaders(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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (cfg *ServerConfig) SetAllowCredentials(flag bool) {
8585
cfg.corsOpts.AllowCredentials = flag
8686
}
8787

88-
func (cfg *ServerConfig) SetAllowHeaders(headers ...string) {
88+
func (cfg *ServerConfig) AddAllowedHeaders(headers ...string) {
8989
cfg.corsOptsRWMutex.Lock()
9090
defer cfg.corsOptsRWMutex.Unlock()
9191
cfg.corsOpts.AllowedHeaders = append(cfg.corsOpts.AllowedHeaders, headers...)

0 commit comments

Comments
 (0)