Skip to content
Snippets Groups Projects
Unverified Commit ad6d6a5e authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Merge pull request #8880 from metabase/http-test-client-improvements

Better-organized HTTP client used for tests and add full-response option
parents f9513ba9 351fb57f
No related branches found
No related tags found
No related merge requests found
...@@ -120,14 +120,28 @@ ...@@ -120,14 +120,28 @@
url (build-url url url-param-kwargs) url (build-url url url-param-kwargs)
method-name (s/upper-case (name method)) method-name (s/upper-case (name method))
;; Now perform the HTTP request ;; Now perform the HTTP request
{:keys [status body]} (try (request-fn url request-map) {:keys [status body] :as resp} (try (request-fn url request-map)
(catch clojure.lang.ExceptionInfo e (catch clojure.lang.ExceptionInfo e
(log/debug method-name url) (log/debug method-name url)
(:object (ex-data e))))] (:object (ex-data e))))]
(log/debug method-name url status) (log/debug method-name url status)
(check-status-code method-name url body expected-status status) (check-status-code method-name url body expected-status status)
(parse-response body))) (update resp :body parse-response)))
(defn- parse-http-client-args
"Parse the list of required and optional `args` into the various separated params that `-client` requires"
[args]
(let [[credentials [method & args]] (u/optional #(or (map? %) (string? %)) args)
[expected-status [url & args]] (u/optional integer? args)
[{:keys [request-options]} args] (u/optional (every-pred map? :request-options) args {:request-options {}})
[body [& {:as url-param-kwargs}]] (u/optional map? args)]
[credentials method expected-status url body url-param-kwargs request-options]))
(defn client-full-response
"Identical to `client` except returns the full HTTP response map, not just the body of the response"
{:arglists '([credentials? method expected-status-code? url request-options? http-body-map? & url-kwargs])}
[& args]
(apply -client (parse-http-client-args args)))
(defn client (defn client
"Perform an API call and return the response (for test purposes). "Perform an API call and return the response (for test purposes).
...@@ -142,7 +156,8 @@ ...@@ -142,7 +156,8 @@
Args: Args:
* CREDENTIALS Optional map of `:username` and `:password` or `X-METABASE-SESSION` token of a User who we should perform the request as * CREDENTIALS Optional map of `:username` and `:password` or `X-METABASE-SESSION` token of a User who we
should perform the request as
* METHOD `:get`, `:post`, `:delete`, or `:put` * METHOD `:get`, `:post`, `:delete`, or `:put`
* EXPECTED-STATUS-CODE When passed, throw an exception if the response has a different status code. * EXPECTED-STATUS-CODE When passed, throw an exception if the response has a different status code.
* URL Base URL of the request, which will be appended to `*url-prefix*`. e.g. `card/1/favorite` * URL Base URL of the request, which will be appended to `*url-prefix*`. e.g. `card/1/favorite`
...@@ -150,8 +165,4 @@ ...@@ -150,8 +165,4 @@
* URL-KWARGS key-value pairs that will be encoded and added to the URL as GET params" * URL-KWARGS key-value pairs that will be encoded and added to the URL as GET params"
{:arglists '([credentials? method expected-status-code? url request-options? http-body-map? & url-kwargs])} {:arglists '([credentials? method expected-status-code? url request-options? http-body-map? & url-kwargs])}
[& args] [& args]
(let [[credentials [method & args]] (u/optional #(or (map? %) (string? %)) args) (:body (apply client-full-response args)))
[expected-status [url & args]] (u/optional integer? args)
[{:keys [request-options]} args] (u/optional (every-pred map? :request-options) args {:request-options {}})
[body [& {:as url-param-kwargs}]] (u/optional map? args)]
(-client credentials method expected-status url body url-param-kwargs request-options)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment