Improve support for URI connections

This commit is contained in:
Michael S. Klishin 2012-04-11 00:20:07 +04:00
parent 0da0a696f2
commit 6e7c29ba8b
2 changed files with 17 additions and 4 deletions

View file

@ -176,7 +176,7 @@
(defn connect-via-uri!
"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."
[{ :keys [uri] }]
[uri]
(let [uri (MongoURI. uri)
;; 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.
@ -192,7 +192,8 @@
(when db
(set-db! db))
(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))

View file

@ -24,8 +24,20 @@
(is (instance? com.mongodb.Mongo connection))))
(deftest connect-to-mongo-via-uri-without-credentials
(let [connection (monger.core/connect "mongodb://127.0.0.1")]
(is (instance? com.mongodb.Mongo connection))))
(let [connection (monger.core/connect-via-uri! "mongodb://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-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