Skip to content

Commit bbd8e10

Browse files
Initial commit
0 parents  commit bbd8e10

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pom.xml
2+
*jar
3+
/lib/
4+
/classes/
5+
.lein-deps-sum

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ring-request-diff
2+
3+
See how Ring middleware changes a request.
4+
5+
## Usage
6+
7+
`ring-request-diff` wraps other Ring middleware, allowing you to monitor how that middleware changes the values of specific keys of a request map.
8+
9+
(-> #'handler
10+
ring.middleware.nested-params/wrap-nested-params
11+
(wrap-request-diff [:params] ring.middleware.keyword-params/wrap-keyword-params)
12+
ring.middleware.params/wrap-params)
13+
14+
It takes a vector of request keys (keyword or string form; either one works), the middleware function it wraps, and any additional arguments required for that wrapped function.
15+
16+
The request is run through the wrapped middleware function. Whenever the value stored under one of the watched keys is changed, a diff of the value (pre- vs. post-middleware) is printed to `*out*`. The processed request is then sent down the rest of the middleware chain.
17+
18+
This comes in handy for debugging complex stacks of middleware, allowing you to easily see exactly what each function is doing, making sure the ordering of middleware is correct, etc.
19+
20+
## Install
21+
22+
Add the following dependency to your `project.clj` file:
23+
24+
[ring-request-diff "0.1.0"]
25+
26+
## License
27+
28+
Copyright (C) 2011 Christopher Maier
29+
30+
Distributed under the Eclipse Public License, the same as Clojure.

project.clj

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(defproject ring-request-diff "0.1.0"
2+
:description "Ring middleware to show how other middleware changes a request body"
3+
:dependencies [[org.clojure/clojure "1.2.0"]
4+
[org.clojure/clojure-contrib "1.2.0"]
5+
[difform "1.1.1"]])

src/ring/middleware/request_diff.clj

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(ns ring.middleware.request-diff
2+
"See how middleware changes a Ring request body"
3+
(:use (com.georgejahad (difform :only [difform]))))
4+
5+
(defn wrap-request-diff
6+
"Debug other middleware by wrapping them and printing out what they change in a Ring request body.
7+
8+
`keys` is a sequence of request keys (string or keyword) that you want to monitor changes in.
9+
`middleware-fn` is the middleware function being wrapped
10+
`args` (optional) are for `middleware-fn`, and will be passed through to it."
11+
[handler keys middleware-fn & args]
12+
(letfn [(omni-get [m k] ;; handle either keyword or string parameter names
13+
(or (get m (keyword k))
14+
(get m (name k))))]
15+
(fn [req]
16+
(let [processed-req ((apply middleware-fn identity args) req)]
17+
(doseq [k keys]
18+
(let [old-part (omni-get req k)
19+
new-part (omni-get processed-req k)
20+
uri (omni-get req :uri)]
21+
(if-not (= old-part new-part)
22+
(do (println "Diff" uri "for" k "with" middleware-fn)
23+
(difform old-part new-part))
24+
(println "No difference on" uri "for" k "with" middleware-fn))))
25+
(handler processed-req)))))

0 commit comments

Comments
 (0)