aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-07-21 12:43:09 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-07-21 12:43:09 +0200
commit87f9bdb8688ceafa804eb49d566bdc38dfb9fd5e (patch)
treef6f3c6860002cceab85f394556f3824b8b8fb85e
parent0f42bbec240714ea93feff8e86c707f97e19f6f7 (diff)
dashboard/dashapi: add customizable Qeury function
Query is useful for tests and other contexts where non-standard request creation/sending is necessary.
-rw-r--r--dashboard/dashapi/dashapi.go22
1 files changed, 16 insertions, 6 deletions
diff --git a/dashboard/dashapi/dashapi.go b/dashboard/dashapi/dashapi.go
index 9565402c6..a60b1eee6 100644
--- a/dashboard/dashapi/dashapi.go
+++ b/dashboard/dashapi/dashapi.go
@@ -142,9 +142,19 @@ const (
)
func (dash *Dashboard) query(method string, req, reply interface{}) error {
+ return Query(dash.Client, dash.Addr, dash.Key, method,
+ http.NewRequest, http.DefaultClient.Do, req, reply)
+}
+
+type (
+ RequestCtor func(method, url string, body io.Reader) (*http.Request, error)
+ RequestDoer func(req *http.Request) (*http.Response, error)
+)
+
+func Query(client, addr, key, method string, ctor RequestCtor, doer RequestDoer, req, reply interface{}) error {
values := make(url.Values)
- values.Add("client", dash.Client)
- values.Add("key", dash.Key)
+ values.Add("client", client)
+ values.Add("key", key)
values.Add("method", method)
var body io.Reader
gzipped := false
@@ -153,7 +163,7 @@ func (dash *Dashboard) query(method string, req, reply interface{}) error {
if err != nil {
return fmt.Errorf("failed to marshal request: %v", err)
}
- if len(data) < 100 || strings.HasPrefix(dash.Addr, "http://localhost:") {
+ if len(data) < 100 || addr == "" || strings.HasPrefix(addr, "http://localhost:") {
// Don't bother compressing tiny requests.
// Don't compress for dev_appserver which does not support gzip.
body = bytes.NewReader(data)
@@ -170,8 +180,8 @@ func (dash *Dashboard) query(method string, req, reply interface{}) error {
gzipped = true
}
}
- url := fmt.Sprintf("%v/api?%v", dash.Addr, values.Encode())
- r, err := http.NewRequest("POST", url, body)
+ url := fmt.Sprintf("%v/api?%v", addr, values.Encode())
+ r, err := ctor("POST", url, body)
if err != nil {
return err
}
@@ -181,7 +191,7 @@ func (dash *Dashboard) query(method string, req, reply interface{}) error {
r.Header.Set("Content-Encoding", "gzip")
}
}
- resp, err := http.DefaultClient.Do(r)
+ resp, err := doer(r)
if err != nil {
return fmt.Errorf("http request failed: %v", err)
}