Improve support for URI connections
This commit is contained in:
parent
0da0a696f2
commit
6e7c29ba8b
2 changed files with 17 additions and 4 deletions
|
|
@ -176,7 +176,7 @@
|
||||||
(defn connect-via-uri!
|
(defn connect-via-uri!
|
||||||
"Connects to MongoDB using a URI, sets up default connection and database. Commonly used for PaaS-based applications,
|
"Connects to MongoDB using a URI, sets up default connection and database. Commonly used for PaaS-based applications,
|
||||||
for example, running on Heroku. If username and password are provided, performs authentication."
|
for example, running on Heroku. If username and password are provided, performs authentication."
|
||||||
[{ :keys [uri] }]
|
[uri]
|
||||||
(let [uri (MongoURI. uri)
|
(let [uri (MongoURI. uri)
|
||||||
;; yes, you are not hallucinating. A class named MongoURI has a method called connectDB.
|
;; yes, you are not hallucinating. A class named MongoURI has a method called connectDB.
|
||||||
;; I call it "college OOP". Or maybe "don't give a shit" OOP.
|
;; I call it "college OOP". Or maybe "don't give a shit" OOP.
|
||||||
|
|
@ -192,7 +192,8 @@
|
||||||
(when db
|
(when db
|
||||||
(set-db! db))
|
(set-db! db))
|
||||||
(when (and user pwd)
|
(when (and user pwd)
|
||||||
(authenticate db user pwd))
|
(when-not (authenticate (.getName db) user pwd)
|
||||||
|
(throw (IllegalArgumentException. "Could not authenticate. Either database name or credentials are invalid."))))
|
||||||
conn))
|
conn))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,20 @@
|
||||||
(is (instance? com.mongodb.Mongo connection))))
|
(is (instance? com.mongodb.Mongo connection))))
|
||||||
|
|
||||||
(deftest connect-to-mongo-via-uri-without-credentials
|
(deftest connect-to-mongo-via-uri-without-credentials
|
||||||
(let [connection (monger.core/connect "mongodb://127.0.0.1")]
|
(let [connection (monger.core/connect-via-uri! "mongodb://127.0.0.1/monger-test4")]
|
||||||
(is (instance? com.mongodb.Mongo connection))))
|
(is (= (-> connection .getAddress (.sameHost "127.0.0.1")))))
|
||||||
|
;; reconnect using regular host
|
||||||
|
(helper/connect!))
|
||||||
|
|
||||||
|
(deftest connect-to-mongo-via-uri-with-valid-credentials
|
||||||
|
(let [connection (monger.core/connect-via-uri! "mongodb://clojurewerkz/monger!:monger!@127.0.0.1/monger-test4")]
|
||||||
|
(is (= (-> connection .getAddress (.sameHost "127.0.0.1")))))
|
||||||
|
;; reconnect using regular host
|
||||||
|
(helper/connect!))
|
||||||
|
|
||||||
|
(deftest connect-to-mongo-via-uri-with-invalid-credentials
|
||||||
|
(is (thrown? IllegalArgumentException
|
||||||
|
(monger.core/connect-via-uri! "mongodb://clojurewerkz/monger!:ahsidaysd78jahsdi8@127.0.0.1/monger-test4"))))
|
||||||
|
|
||||||
|
|
||||||
(deftest test-mongo-options-builder
|
(deftest test-mongo-options-builder
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue