Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ring plugin tests #181

Closed
wants to merge 15 commits into from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
*.o
*.py[co]
*.class
*.jar

/uwsgi
/uwsgibuild.*

/t/ring/target
2 changes: 2 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ syntax: glob

*.o
*.pyc
*.class
*.jar
12 changes: 12 additions & 0 deletions t/ring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Ring Test Suite
================

how to build and run
---------------------

* cd UWSGIROOT
* cd t/ring
* lein uberjar
* cd ../..
* uwsgi t/ring/config.ini
* open http://localhost:9090 in your browser
9 changes: 9 additions & 0 deletions t/ring/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[uwsgi]
http = :9090
http-modifier1 = 8
http-modifier2 = 1

jvm-classpath = plugins/jvm/uwsgi.jar
jvm-classpath = t/ring/target/uwsgi-ring-tests-0.0.1-standalone.jar

ring-app = uwsgi.ring.tests.app:app
10 changes: 10 additions & 0 deletions t/ring/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(defproject unbit/uwsgi-ring-tests "0.0.1"
:description "uwsgi-ring-tests: test cases for uwsgi ring server"
:dependencies [[org.clojure/clojure "1.4.0"]
[compojure "1.1.3"]
[ring/ring "1.1.0"]]

:source-paths ["src"]

:plugins [[lein-ring "0.7.5"]]
:ring {:handler uwsgi.ring.tests.app/app})
29 changes: 29 additions & 0 deletions t/ring/src/uwsgi/ring/tests/app.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns uwsgi.ring.tests.app
(:use compojure.core)
(:use [ring.middleware params
keyword-params
nested-params
multipart-params])
(:require [compojure.route :as route]
[uwsgi.ring.tests.basic :as basic]
[uwsgi.ring.tests.body :as body]
[uwsgi.ring.tests.simple :as simple]
[uwsgi.ring.tests.upload :as upload])
(:gen-class
:main true))

(defn app-routes [req]
(if (= (get req :uri) "/")
(basic/index-page req)
((routes simple/app-routes body/app-routes upload/app-routes) req)))

(def app
(-> app-routes
wrap-keyword-params
wrap-nested-params
wrap-params
wrap-multipart-params))

(defn -main [& args]
(println "uwsgi ring tests app loaded"))

30 changes: 30 additions & 0 deletions t/ring/src/uwsgi/ring/tests/basic.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns uwsgi.ring.tests.basic)

(defn index-page [req] {:status 200
:headers { "Content-Type" "text/html" , "Server" "uWSGI" }
:body (str "<h1>Ring test suites</h1>"
"<h2>Simple tests</h2>"
"<ul>"
"<li><a href='/hello'>hello</a></li>"
"<li><a href='/echo?msg=abc'>echo</a></li>"
"<li><a href='/palindrome?msg=abc'>palindrome</a></li>"
"</ul>"
"<h2>Body type tests</h2>"
"<ul>"
"<li><a href='/sequence'>sequence</a></li>"
"<li><a href='/file'>file</a></li>"
"<li><a href='/stream'>stream</a></li>"
"</ul>"
"<h2>Upload tests</h2>"
"<form action='/upload' enctype='multipart/form-data' method='post'>"
"<p>"
"Please select a file<br>"
"<input type='file' name='file' size='40'>"
"</p>"
"<input type='submit' value='Upload'>"
"</form>"
"<h2>Other tests</h2>"
"<ul>"
"<li>...</li>"
"</ul>")})

20 changes: 20 additions & 0 deletions t/ring/src/uwsgi/ring/tests/body.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns uwsgi.ring.tests.body
(:use [compojure.core]))

; generating primary numbers
; http://clojuredocs.org/clojure_core/clojure.core/lazy-seq#example_1000
(defn sieve [s]
(cons (first s)
(lazy-seq (sieve (filter #(not= 0 (mod % (first s)))
(rest s))))))

(defn sequence [] (take 20 (sieve (iterate inc 2))))

(defn file [] (java.io.File. "CONTRIBUTORS"))

(defn stream [] (java.io.FileInputStream. (java.io.File. "CONTRIBUTORS")))

(defroutes app-routes
(GET "/sequence" [] (sequence))
(GET "/file" [] (file))
(GET "/stream" [] (stream)))
13 changes: 13 additions & 0 deletions t/ring/src/uwsgi/ring/tests/simple.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns uwsgi.ring.tests.simple
(:use [compojure.core]))

(defn hello [] "Hello, World!")

(defn echo [msg] msg)

(defn palindrome [msg] (clojure.string/reverse msg))

(defroutes app-routes
(GET "/hello" [] (hello))
(GET "/echo" [msg] (echo msg))
(GET "/palindrome" [msg] (palindrome msg)))
20 changes: 20 additions & 0 deletions t/ring/src/uwsgi/ring/tests/upload.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns uwsgi.ring.tests.upload
(:use [compojure.core]))

(defn upload-file [fname fsize fbody] {
:status 200
:headers { "Content-Type" "text/html" , "Server" "uWSGI" }
:body (str "<h1>Uploaded file</h1>"
"<ul>"
"<li>" fname "</li>"
"<li>" fsize "</li>"
"</ul>")})

(defroutes app-routes
(POST "/upload" {params :params}
(let [file (params :file)
file-name (file :filename)
file-size (file :size)
file-body (file :tempfile)]
(upload-file file-name file-size file-body))))