Skip to content

An integration test framework for ring web applications.

License

Notifications You must be signed in to change notification settings

rreas/ring-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ring-test

An integration test framework for Ring web applications. Currently supports sessions and cookies. Does not yet support file uploads. Inspired by Ruby's Rack::Test.

Simple usage

The function run-ring-app accepts a Ring application and one or more requests (optionally constructed with ring-mock) and returns the response map from the final request. The session and cookies carry through all requests passed to a single call to run-ring-app.

(ns ring-test.session-test
  (:require [clojure.test :refer :all]
            [ring-test.core :refer [run-ring-app]]
            [ring.middleware.session :refer [wrap-session]]
            [ring.mock.request :refer [request]]))

(defn handler [{session :session}]
  (let [n (:n session 1)]
    {:body (str n) :session {:n (inc n)}}))

(def app (wrap-session handler))

(deftest session-carries-over
  (let [response
        (run-ring-app
          app
          (request :get "/")
          (request :get "/"))]
    (is (= "2" (:body response)))))

Accessing the last response

In the example above only request maps are passed to run-ring-app. If the next request depends on the previous request then pass a function accepting two arguments: the previous request and response. The function should return a new request map. For example:

(defn handler [{params :params}]
  (response (str (params "s"))))

(def app (-> handler wrap-params))

(deftest doubles-previous-response
  (let [resp (run-ring-app
               app
               (request :get "/" {:s "b"}) ; Response body is "b".
               (fn [{body :body} _]
                 (request :get "/" {:s (str body body)})))]
         (is (= "bb" (:body resp)))))

Installation

Add the following to your project.clj:

[ring-test "0.1.3"]

License

Copyright © 2013 Russell Reas and released under an MIT license.

About

An integration test framework for ring web applications.

Resources

License

Stars

Watchers

Forks

Packages

No packages published