mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
api: allow query parameters in query/write/delete
This commit is contained in:
11
api/api.go
11
api/api.go
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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®ion=foo" {
|
||||
t.Fatalf("bad uri: %q", uri)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user