Skip to content

Commit 3421954

Browse files
Allow to override Authorization and Content-Type request headers
1 parent f4d0566 commit 3421954

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/clj_github/httpkit_client.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
(assoc :method method)
2323
(assoc-some :body (and body (cheshire/generate-string body)))
2424
(assoc :url (append-url-path github-url path))
25-
(assoc-in [:headers "Content-Type"] "application/json")
26-
(assoc-in [:headers "Authorization"] (str "Bearer " (token-fn)))))
25+
;; TODO http headers are case insensitive, also this could be keywords
26+
(update-in [:headers "Content-Type"] #(or % "application/json"))
27+
(update-in [:headers "Authorization"] #(or % (str "Bearer " (token-fn))))))
2728

2829
(defn- parse-body [content-type body]
2930
(if (and content-type (re-find #"application/json" content-type))
@@ -43,7 +44,8 @@
4344
{:response (select-keys response [:status :body])}
4445
(:error response))))))
4546

46-
(defn new-client [{:keys [app-id private-key token org] :as opts}]
47+
(defn new-client [{:keys [app-id private-key token org token-fn] :as opts}]
48+
{:pre [(or token app-id token-fn)]}
4749
(cond
4850
token
4951
{:token-fn (constantly token)}

test/clj_github/httpkit_client_test.clj

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,28 @@
4747
(with-fake-http [{} {:error cause :status nil}]
4848
(let [e (try (sut/request client {}) (catch Exception e e))]
4949
(is (re-matches #"(?i)Request to GitHub failed" (.getMessage e)))
50-
(is (= cause (.getCause e)))))))))
50+
(is (= cause (.getCause e)))))))
51+
(testing "request headers"
52+
(testing "Can use custom headers"
53+
(with-fake-http [(fn [req]
54+
(is (= "test" (get-in req [:headers "Test-Header"]))))
55+
{:status 200}]
56+
;; assertion is in fake http callback
57+
(sut/request client {:headers {"Test-Header" "test"}})))
58+
(testing "Can override authentication"
59+
;; clj-github sets authorization header unless specified. Beware that the
60+
;; implementation here is case sensitive whereas HTTP headers are not.
61+
(with-fake-http [(fn [req]
62+
(is (= "Test SomeValue" (get-in req [:headers "Authorization"]))))
63+
{:status 200}]
64+
;; assertion is in fake http callback
65+
(sut/request client {:headers {"Authorization" "Test SomeValue"}})))
66+
(testing "Can override content-type"
67+
;; clj-github sets the content-type header unless specified. Beware that the
68+
;; implementation here is case sensitive whereas HTTP headers are not.
69+
(with-fake-http [(fn [req]
70+
(is (= "test/test" (get-in req [:headers "Content-Type"]))))
71+
{:status 200}]
72+
;; assertion is in fake http callback
73+
(sut/request client {:headers {"Content-Type" "test/test"}}))))))
74+

0 commit comments

Comments
 (0)