Use header to send Consul token rather than query param.

pull/2233/head
Jeff Mitchell 2016-08-02 16:54:59 -04:00
parent 59d9623ea5
commit f1a67836d8
2 changed files with 10 additions and 7 deletions

View File

@ -330,6 +330,7 @@ type request struct {
url *url.URL url *url.URL
params url.Values params url.Values
body io.Reader body io.Reader
header http.Header
obj interface{} obj interface{}
} }
@ -355,7 +356,7 @@ func (r *request) setQueryOptions(q *QueryOptions) {
r.params.Set("wait", durToMsec(q.WaitTime)) r.params.Set("wait", durToMsec(q.WaitTime))
} }
if q.Token != "" { if q.Token != "" {
r.params.Set("token", q.Token) r.header.Set("X-Consul-Token", q.Token)
} }
if q.Near != "" { if q.Near != "" {
r.params.Set("near", q.Near) r.params.Set("near", q.Near)
@ -399,7 +400,7 @@ func (r *request) setWriteOptions(q *WriteOptions) {
r.params.Set("dc", q.Datacenter) r.params.Set("dc", q.Datacenter)
} }
if q.Token != "" { if q.Token != "" {
r.params.Set("token", q.Token) r.header.Set("X-Consul-Token", q.Token)
} }
} }
@ -426,6 +427,7 @@ func (r *request) toHTTP() (*http.Request, error) {
req.URL.Host = r.url.Host req.URL.Host = r.url.Host
req.URL.Scheme = r.url.Scheme req.URL.Scheme = r.url.Scheme
req.Host = r.url.Host req.Host = r.url.Host
req.Header = r.header
// Setup auth // Setup auth
if r.config.HttpAuth != nil { if r.config.HttpAuth != nil {
@ -446,6 +448,7 @@ func (c *Client) newRequest(method, path string) *request {
Path: path, Path: path,
}, },
params: make(map[string][]string), params: make(map[string][]string),
header: make(http.Header),
} }
if c.config.Datacenter != "" { if c.config.Datacenter != "" {
r.params.Set("dc", c.config.Datacenter) r.params.Set("dc", c.config.Datacenter)
@ -454,7 +457,7 @@ func (c *Client) newRequest(method, path string) *request {
r.params.Set("wait", durToMsec(r.config.WaitTime)) r.params.Set("wait", durToMsec(r.config.WaitTime))
} }
if c.config.Token != "" { if c.config.Token != "" {
r.params.Set("token", r.config.Token) r.header.Set("X-Consul-Token", r.config.Token)
} }
return r return r
} }

View File

@ -247,8 +247,8 @@ func TestSetQueryOptions(t *testing.T) {
if r.params.Get("wait") != "100000ms" { if r.params.Get("wait") != "100000ms" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.params)
} }
if r.params.Get("token") != "12345" { if r.header.Get("X-Consul-Token") != "12345" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.header)
} }
if r.params.Get("near") != "nodex" { if r.params.Get("near") != "nodex" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.params)
@ -270,8 +270,8 @@ func TestSetWriteOptions(t *testing.T) {
if r.params.Get("dc") != "foo" { if r.params.Get("dc") != "foo" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.params)
} }
if r.params.Get("token") != "23456" { if r.header.Get("X-Consul-Token") != "23456" {
t.Fatalf("bad: %v", r.params) t.Fatalf("bad: %v", r.header)
} }
} }