pax_global_header00006660000000000000000000000064131255032130014505gustar00rootroot0000000000000052 comment=cc680c0869c48798d299fd16b6b21d1ecddcfcd1 ring-mock-0.3.1/000077500000000000000000000000001312550321300133745ustar00rootroot00000000000000ring-mock-0.3.1/.gitignore000066400000000000000000000001761312550321300153700ustar00rootroot00000000000000/target /lib /classes /checkouts /doc pom.xml pom.xml.asc *.jar *.class .lein-deps-sum .lein-failures .lein-plugins .lein-env ring-mock-0.3.1/.travis.yml000066400000000000000000000001071312550321300155030ustar00rootroot00000000000000language: clojure script: lein test-all jdk: - openjdk7 - openjdk6 ring-mock-0.3.1/README.md000066400000000000000000000016601312550321300146560ustar00rootroot00000000000000# Ring-Mock [![Build Status](https://travis-ci.org/ring-clojure/ring-mock.svg?branch=master)](https://travis-ci.org/ring-clojure/ring-mock) Ring-Mock is a library for creating [Ring][] request maps for testing purposes. [ring]: https://github.com/ring-clojure/ring ## Installation Add the following development dependency to your `project.clj` file: [ring/ring-mock "0.3.1"] ## Documentation * [API Documentation](https://ring-clojure.github.io/ring-mock/ring.mock.request.html) ## Example ```clojure (ns your-app.core-test (:require [clojure.test :refer :all] [your-app.core :refer :all] [ring.mock.request :as mock])) (deftest your-handler-test (is (= (your-handler (mock/request :get "/doc/10")) {:status 200 :headers {"content-type" "text/plain"} :body "Your expected result"}))) ``` ## License Copyright © 2017 James Reeves Distributed under the MIT License. ring-mock-0.3.1/project.clj000066400000000000000000000014061312550321300155350ustar00rootroot00000000000000(defproject ring/ring-mock "0.3.1" :description "A library for creating mock Ring request maps" :url "https://github.com/ring-clojure/ring-mock" :license {:name "The MIT License" :url "http://opensource.org/licenses/MIT"} :dependencies [[org.clojure/clojure "1.5.1"] [ring/ring-codec "1.0.1"]] :plugins [[codox "0.10.3"]] :codox {:project {:name "Ring-Mock"}} :aliases {"test-all" ["with-profile" "default:+1.6:+1.7:+1.8:+1.9" "test"]} :profiles {:1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} :1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]} :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]} :1.9 {:dependencies [[org.clojure/clojure "1.9.0-alpha17"] [ring/ring-spec "0.0.3"]]}}) ring-mock-0.3.1/src/000077500000000000000000000000001312550321300141635ustar00rootroot00000000000000ring-mock-0.3.1/src/ring/000077500000000000000000000000001312550321300151225ustar00rootroot00000000000000ring-mock-0.3.1/src/ring/mock/000077500000000000000000000000001312550321300160535ustar00rootroot00000000000000ring-mock-0.3.1/src/ring/mock/request.clj000066400000000000000000000072321312550321300202410ustar00rootroot00000000000000(ns ring.mock.request "Functions to create mock request maps." (:require [clojure.string :as string] [ring.util.codec :as codec])) (defn- encode-params "Turn a map of parameters into a urlencoded string." [params] (if params (codec/form-encode params))) (defn header "Add a HTTP header to the request map." [request header value] (let [header (string/lower-case (name header))] (assoc-in request [:headers header] (str value)))) (defn content-type "Set the content type of the request map." [request mime-type] (-> request (assoc :content-type mime-type) (header :content-type mime-type))) (defn content-length "Set the content length of the request map." [request length] (-> request (assoc :content-length length) (header :content-length length))) (defn- combined-query "Create a query string from a URI and a map of parameters." [request params] (let [query (:query-string request)] (if (or query params) (string/join "&" (remove string/blank? [query (encode-params params)]))))) (defn- merge-query "Merge the supplied parameters into the query string of the request." [request params] (if-let [qs (combined-query request params)] (assoc request :query-string qs) request)) (defn query-string "Set the query string of the request to a string or a map of parameters." [request params] (cond (nil? params) request (map? params) (assoc request :query-string (encode-params params)) :else (assoc request :query-string params))) (defmulti body "Set the body of the request. The supplied body value can be a string or a map of parameters to be url-encoded." {:arglists '([request body-value])} (fn [request x] (type x))) (defmethod body String [request ^String string] (body request (.getBytes string))) (defmethod body (class (byte-array 0)) [request bytes] (-> request (content-length (count bytes)) (assoc :body (java.io.ByteArrayInputStream. bytes)))) (defmethod body java.util.Map [request params] (-> request (content-type "application/x-www-form-urlencoded") (body (encode-params params)))) (defmethod body nil [request params] request) (def default-port "A map of the default ports for a scheme." {:http 80 :https 443}) (defn request "Create a minimal valid request map from a HTTP method keyword, a string containing a URI, and an optional map of parameters that will be added to the query string of the URI. The URI can be relative or absolute. Relative URIs are assumed to go to http://localhost." ([method uri] (request method uri nil)) ([method uri params] (let [uri (java.net.URI. uri) scheme (keyword (or (.getScheme uri) "http")) host (or (.getHost uri) "localhost") port (when (not= (.getPort uri) -1) (.getPort uri)) path (.getRawPath uri) query (.getRawQuery uri) request {:protocol "HTTP/1.1" :server-port (or port (default-port scheme)) :server-name host :remote-addr "localhost" :uri (if (string/blank? path) "/" path) :scheme scheme :request-method method :headers {"host" (if port (str host ":" port) host)}} request (if query (assoc request :query-string query) request)] (if (#{:get :head :delete} method) (merge-query request params) (body request params))))) ring-mock-0.3.1/test/000077500000000000000000000000001312550321300143535ustar00rootroot00000000000000ring-mock-0.3.1/test/ring/000077500000000000000000000000001312550321300153125ustar00rootroot00000000000000ring-mock-0.3.1/test/ring/mock/000077500000000000000000000000001312550321300162435ustar00rootroot00000000000000ring-mock-0.3.1/test/ring/mock/request_test.clj000066400000000000000000000127461312550321300214760ustar00rootroot00000000000000(ns ring.mock.request-test (:require [clojure.java.io :as io]) (:use clojure.test ring.mock.request)) (deftest test-request (testing "relative uri" (is (= (request :get "/foo") {:protocol "HTTP/1.1" :server-port 80 :server-name "localhost" :remote-addr "localhost" :uri "/foo" :scheme :http :request-method :get :headers {"host" "localhost"}}))) (testing "absolute uri" (let [request (request :post "https://example.com:8443/foo?bar=baz" {"quux" "zot"}) literal-request (dissoc request :body) body (:body request)] (is (= literal-request {:protocol "HTTP/1.1" :server-port 8443 :server-name "example.com" :remote-addr "localhost" :uri "/foo" :query-string "bar=baz" :scheme :https :request-method :post :content-type "application/x-www-form-urlencoded" :content-length 8 :headers {"host" "example.com:8443" "content-type" "application/x-www-form-urlencoded" "content-length" "8"}})) (is (= (slurp body) "quux=zot")))) (testing "absolute http uri with implicit port" (let [{:keys [headers server-port]} (request :get "http://example.test")] (is (= server-port 80)) (is (= (get headers "host") "example.test")))) (testing "absolute https uri with implicit port" (let [{:keys [headers server-port]} (request :get "https://example.test")] (is (= server-port 443)) (is (= (get headers "host") "example.test")))) (testing "nil path" (is (= (:uri (request :get "http://example.com")) "/"))) (testing "only params in :get" (is (= (:query-string (request :get "/?a=b")) "a=b"))) (testing "added params in :get" (is (= (:query-string (request :get "/" (array-map :x "y" :z "n"))) "x=y&z=n")) (is (= (:query-string (request :get "/?a=b" {:x "y"})) "a=b&x=y")) (is (= (:query-string (request :get "/?" {:x "y"})) "x=y")) (is (= (:query-string (request :get "/" {:x "a b"})) "x=a+b"))) (testing "added params in :delete" (is (= (:query-string (request :delete "/" (array-map :x "y" :z "n"))) "x=y&z=n"))) (testing "added params in :post" (let [req (request :post "/" (array-map :x "y" :z "n"))] (is (= (slurp (:body req)) "x=y&z=n")) (is (not (contains? req :query-string)))) (let [req (request :post "/?a=b" {:x "y"})] (is (= (slurp (:body req)) "x=y")) (is (= (:query-string req) "a=b"))) (let [req (request :post "/?" {:x "y"})] (is (= (slurp (:body req)) "x=y")) (is (= (:query-string req) ""))) (let [req (request :post "/" {:x "a b"})] (is (= (slurp (:body req)) "x=a+b")) (is (nil? (:query-string req)))) (let [req (request :post "/?a=b")] (is (nil? (:body req))) (is (= (:query-string req) "a=b")))) (testing "added params in :put" (let [req (request :put "/" (array-map :x "y" :z "n"))] (is (= (slurp (:body req)) "x=y&z=n"))))) (deftest test-header (is (= (header {} "X-Foo" "Bar") {:headers {"x-foo" "Bar"}})) (is (= (header {} :x-foo "Bar") {:headers {"x-foo" "Bar"}}))) (deftest test-content-type (is (= (content-type {} "text/html") {:content-type "text/html" :headers {"content-type" "text/html"}}))) (deftest test-content-length (is (= (content-length {} 10) {:content-length 10 :headers {"content-length" "10"}}))) (deftest test-query-string (testing "nil" (is (= (query-string {} nil) {}))) (testing "string" (is (= (query-string {} "a=b") {:query-string "a=b"}))) (testing "map of params" (is (= (query-string {} {:a "b"}) {:query-string "a=b"}))) (testing "overwriting" (is (= (-> {} (query-string {:a "b"}) (query-string {:c "d"})) {:query-string "c=d"})))) (deftest test-body (testing "string body" (let [resp (body {} "Hello World")] (is (instance? java.io.InputStream (:body resp))) (is (= (slurp (:body resp)) "Hello World")) (is (= (:content-length resp) 11)))) (testing "map body" (let [resp (body {} (array-map :foo "bar" :fi ["fi" "fo" "fum"]))] (is (instance? java.io.InputStream (:body resp))) (is (= (slurp (:body resp)) "foo=bar&fi=fi&fi=fo&fi=fum")) (is (= (:content-length resp) 26)) (is (= (:content-type resp) "application/x-www-form-urlencoded")))) (testing "bytes body" (let [resp (body {} (.getBytes "foo"))] (is (instance? java.io.InputStream (:body resp))) (is (= (slurp (:body resp)) "foo")) (is (= (:content-length resp) 3))))) (defmacro when-clojure-spec [& body] (when (try (require '[clojure.spec :as s]) true (catch Exception _ (binding [*out* *err*] (println "ring-mock: Skipping ring-spec tests.")) false)) `(do (require 'ring.core.spec) ~@body))) (deftest test-specs (when-clojure-spec (doseq [req [(-> (request :get "/foo/bar") (query-string nil)) (request :put "/foo/bar") (request :something "/foo" {:params true})]] (is (s/valid? :ring/request req) (s/explain-str :ring/request req)))))