test/all.rb in syro-1.0.0 vs test/all.rb in syro-1.1.0
- old
+ new
@@ -1,5 +1,7 @@
+require "json"
+
class RackApp
def call(env)
[200, {"Content-Type" => "text/html"}, ["GET /rack"]]
end
end
@@ -15,18 +17,48 @@
def default_headers
{ Rack::CONTENT_TYPE => "text/html" }
end
end
+class CustomRequestAndResponse < Syro::Deck
+ class JSONRequest < Rack::Request
+ def params
+ JSON.parse(body.read)
+ end
+ end
+
+ class JSONResponse < Syro::Response
+ def write(s)
+ super(JSON.generate(s))
+ end
+ end
+
+ def request_class
+ JSONRequest
+ end
+
+ def response_class
+ JSONResponse
+ end
+end
+
textual = Syro.new(TextualDeck) {
get {
text("GET /textual")
}
}
default_headers = Syro.new(DefaultHeaders) { }
+json = Syro.new(CustomRequestAndResponse) {
+ root {
+ params = req.params
+
+ res.write(params)
+ }
+}
+
admin = Syro.new {
get {
res.write("GET /admin")
}
}
@@ -153,10 +185,14 @@
}
on("headers") {
run(default_headers)
}
+
+ on("json") {
+ run(json)
+ }
}
setup do
Driver.new(app)
end
@@ -267,6 +303,14 @@
test "default headers" do |f|
f.get("/headers")
assert_equal "text/html", f.last_response.headers["Content-Type"]
+end
+
+test "custom request and response class" do |f|
+ params = JSON.generate(foo: "foo")
+
+ f.post("/json", params)
+
+ assert_equal params, f.last_response.body
end