Skip to content

Commit 697705f

Browse files
authored
User info from URL now applied to basic auth (#37)
Also moved wrap-oauth to match clj-http wrapper ordering for easier ongoing comparison. Fixes #34
1 parent 5aa67f5 commit 697705f

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### Unreleased
22

33
- If specified, request's body encoding is now applied, else defaults to UTF-8 ([#18](https://github.com/clj-commons/clj-http-lite/issues/18)) ([@lread](https://github.com/lread))
4+
- User info from request URL now applied to basic auth ([#34](https://github.com/clj-commons/clj-http-lite/issues/34)) ([@lread](https://github.com/lread))
45
- Quality
56
- Automated CI testing added for Windows ([#21](https://github.com/clj-commons/clj-http-lite/issues/21)) ([@lread](https://github.com/lread))
67

src/clj_http/lite/client.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,26 @@
236236
"Returns a battaries-included HTTP request function coresponding to the given
237237
core client. See client/client."
238238
[request]
239+
;; note to the uninitiated: wrapper behaviour is applied to requests in order listed here but
240+
;; from last to first
239241
(-> request
240242
wrap-query-params
243+
wrap-basic-auth
244+
wrap-oauth
241245
wrap-user-info
242246
wrap-url
243247
wrap-redirects
244248
wrap-decompression
245249
wrap-input-coercion
246250
wrap-output-coercion
247251
wrap-exceptions
248-
wrap-basic-auth
249252
wrap-accept
250253
wrap-accept-encoding
251254
wrap-content-type
252255
wrap-form-params
253256
wrap-method
254257
wrap-links
255-
wrap-unknown-host
256-
wrap-oauth))
258+
wrap-unknown-host))
257259

258260
(def #^{:doc
259261
"Executes the HTTP request corresponding to the given map and returns

test/clj_http/test/client.clj

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns clj-http.test.client
22
(:require [clj-http.lite.client :as client]
3-
[clj-http.test.core :refer [base-req with-server]]
3+
[clj-http.test.core :refer [base-req with-server current-port]]
44
[clj-http.lite.util :as util]
55
[clojure.test :refer [deftest is testing use-fixtures]])
66
(:import (java.net UnknownHostException)))
@@ -19,6 +19,41 @@
1919
(is (= 200 (:status resp)))
2020
(is (= "get" (:body resp)))))
2121

22+
(deftest ^{:integration true} basic-auth-no-creds
23+
(let [resp (client/request (merge (base-req) {:method :get
24+
:uri "/basic-auth"
25+
:throw-exceptions false}))]
26+
(is (= 401 (:status resp)))
27+
(is (= "denied" (:body resp)))))
28+
29+
(deftest ^{:integration true} basic-auth-bad-creds
30+
(let [resp (client/request (merge (base-req) {:method :get
31+
:uri "/basic-auth"
32+
:throw-exceptions false
33+
:basic-auth "username:nope"}))]
34+
(is (= 401 (:status resp)))
35+
(is (= "denied" (:body resp)))))
36+
37+
(deftest ^{:integration true} basic-auth-creds-as-basic-auth
38+
(let [resp (client/request (merge (base-req) {:method :get
39+
:uri "/basic-auth"
40+
:basic-auth "username:password"}))]
41+
(is (= 200 (:status resp)))
42+
(is (= "welcome" (:body resp)))))
43+
44+
(deftest ^{:integration true} basic-auth-creds-as-user-info
45+
(let [resp (client/request (merge (base-req) {:method :get
46+
:uri "/basic-auth"
47+
:user-info "username:password"}))]
48+
(is (= 200 (:status resp)))
49+
(is (= "welcome" (:body resp)))))
50+
51+
(deftest ^{:integration true} basic-auth-creds-from-url
52+
(let [resp (client/request {:method :get
53+
:url (format "http://username:password@localhost:%d/basic-auth" (current-port))})]
54+
(is (= 200 (:status resp)))
55+
(is (= "welcome" (:body resp)))))
56+
2257
(defn is-passed [middleware req]
2358
(let [client (middleware identity)]
2459
(is (= req (client req)))))

test/clj_http/test/core.clj

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44
[clojure.test :refer [deftest is use-fixtures]]
55
[clojure.string :as str]
66
[ring.adapter.jetty :as ring])
7-
(:import (org.eclipse.jetty.server Server ServerConnector)))
7+
(:import (org.eclipse.jetty.server Server ServerConnector)
8+
(java.util Base64)))
89

910
(set! *warn-on-reflection* true)
1011

12+
(defn b64-decode [^String s]
13+
(when s
14+
(-> (Base64/getDecoder)
15+
(.decode s)
16+
util/utf8-string)))
17+
1118
(defn handler [req]
1219
(condp = [(:request-method req) (:uri req)]
1320
[:get "/get"]
@@ -20,15 +27,25 @@
2027
{:status 200 :body (get-in req [:headers "x-my-header"])}
2128
[:post "/post"]
2229
{:status 200 :body (slurp (:body req))}
23-
[:get "/redirect"] {:status 302 :headers {"Location" "/get"} }
30+
[:get "/redirect"] {:status 302 :headers {"Location" "/get"}}
2431
[:get "/error"]
2532
{:status 500 :body "o noes"}
2633
[:get "/timeout"]
2734
(do
2835
(Thread/sleep 10)
2936
{:status 200 :body "timeout"})
3037
[:delete "/delete-with-body"]
31-
{:status 200 :body "delete-with-body"}))
38+
{:status 200 :body "delete-with-body"}
39+
;; minimal to support testing
40+
[:get "/basic-auth"]
41+
(let [cred (some->> (get (:headers req) "authorization")
42+
(re-find #"^Basic (.*)$")
43+
last
44+
b64-decode)
45+
[user pass] (and cred (str/split cred #":"))]
46+
(if (and (= "username" user) (= "password" pass))
47+
{:status 200 :body "welcome"}
48+
{:status 401 :body "denied"}))))
3249

3350
(defn make-server ^Server []
3451
(ring/run-jetty handler {:port 0 ;; Use a free port
@@ -204,3 +221,4 @@
204221
(let [stream (:body (request {:request-method :get :uri "/get" :as :stream}))
205222
body (slurp stream)]
206223
(is (= "get" body))))
224+

0 commit comments

Comments
 (0)