api: allow query parameters in query/write/delete

This commit is contained in:
Ryan Uber
2015-09-08 14:26:26 -07:00
parent f6f1813aa1
commit e2e1e4c3fb
2 changed files with 29 additions and 1 deletions

View File

@@ -195,13 +195,14 @@ func (r *request) toHTTP() (*http.Request, error) {
// newRequest is used to create a new request
func (c *Client) newRequest(method, path string) *request {
base, _ := url.Parse(c.config.URL)
u, _ := url.Parse(path)
r := &request{
config: &c.config,
method: method,
url: &url.URL{
Scheme: base.Scheme,
Host: base.Host,
Path: path,
Path: u.Path,
},
params: make(map[string][]string),
}
@@ -211,6 +212,14 @@ func (c *Client) newRequest(method, path string) *request {
if c.config.WaitTime != 0 {
r.params.Set("wait", durToMsec(r.config.WaitTime))
}
// Add in the query parameters, if any
for key, values := range u.Query() {
for _, value := range values {
r.params.Add(key, value)
}
}
return r
}

View File

@@ -155,3 +155,22 @@ func TestParseWriteMeta(t *testing.T) {
t.Fatalf("Bad: %v", wm)
}
}
func TestQueryString(t *testing.T) {
// TODO t.Parallel()
c, s := makeClient(t, nil, nil)
defer s.Stop()
r := c.newRequest("PUT", "/v1/abc?foo=bar&baz=zip")
q := &WriteOptions{Region: "foo"}
r.setWriteOptions(q)
req, err := r.toHTTP()
if err != nil {
t.Fatalf("err: %s", err)
}
if uri := req.URL.RequestURI(); uri != "/v1/abc?baz=zip&foo=bar&region=foo" {
t.Fatalf("bad uri: %q", uri)
}
}