Coder Social home page Coder Social logo

ring-test's Introduction

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.

ring-test's People

Contributors

rreas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.