test/all.rb in syro-2.2.0 vs test/all.rb in syro-3.0.0

- old
+ new

@@ -4,13 +4,13 @@ def call(env) [200, {"Content-Type" => "text/html"}, ["GET /rack"]] end end -class TextualDeck < Syro::Deck - def text(str) - res[Rack::CONTENT_TYPE] = "text/plain" +class MarkdownDeck < Syro::Deck + def markdown(str) + res[Rack::CONTENT_TYPE] = "text/markdown" res.write(str) end end class DefaultHeaders < Syro::Deck @@ -39,13 +39,13 @@ def response_class JSONResponse end end -textual = Syro.new(TextualDeck) do +markdown = Syro.new(MarkdownDeck) do get do - text("GET /textual") + markdown("GET /markdown") end end default_headers = Syro.new(DefaultHeaders) do end @@ -180,26 +180,42 @@ post do res.redirect("/one") end end - on "textual" do - run(textual) + on "markdown" do + run(markdown) end on "headers" do run(default_headers) end - on "json" do + on "custom" do run(json) end on "private" do res.status = 401 res.write("Unauthorized") end + + on "write" do + res.write "nil!" + end + + on "text" do + res.text "plain!" + end + + on "html" do + res.html "html!" + end + + on "json" do + res.json "json!" + end end setup do Driver.new(app) end @@ -207,10 +223,14 @@ test "path + verb" do |f| f.get("/foo/bar") assert_equal 200, f.last_response.status assert_equal "GET /foo/bar", f.last_response.body + f.get("/bar/baz") + assert_equal 404, f.last_response.status + assert_equal "", f.last_response.body + f.put("/foo/bar") assert_equal 200, f.last_response.status assert_equal "PUT /foo/bar", f.last_response.body f.head("/foo/bar") @@ -265,11 +285,13 @@ end test "captures" do |f| f.get("/users/42") assert_equal "GET /users/42", f.last_response.body - assert_equal 200, f.last_response.status + + # As the verb was not mached, the status is 404. + assert_equal 404, f.last_response.status end test "post values" do |f| f.post("/", "user" => { "username" => "foo" }) assert_equal "POST / (user)", f.last_response.body @@ -304,13 +326,13 @@ assert_equal "1", f.last_response.body assert_equal 200, f.last_response.status end test "custom deck" do |f| - f.get("/textual") - assert_equal "GET /textual", f.last_response.body - assert_equal "text/plain", f.last_response.headers["Content-Type"] + f.get("/markdown") + assert_equal "GET /markdown", f.last_response.body + assert_equal "text/markdown", f.last_response.headers["Content-Type"] assert_equal 200, f.last_response.status end test "default headers" do |f| f.get("/headers") @@ -319,17 +341,31 @@ end test "custom request and response class" do |f| params = JSON.generate(foo: "foo") - f.post("/json", params) + f.post("/custom", params) assert_equal params, f.last_response.body end -test "set content type if body is set" do |f| +test "don't set content type by default" do |f| f.get("/private") assert_equal 401, f.last_response.status assert_equal "Unauthorized", f.last_response.body + assert_equal nil, f.last_response.headers["Content-Type"] +end + +test "content type" do |f| + f.get("/write") + assert_equal nil, f.last_response.headers["Content-Type"] + + f.get("/text") + assert_equal "text/plain", f.last_response.headers["Content-Type"] + + f.get("/html") assert_equal "text/html", f.last_response.headers["Content-Type"] + + f.get("/json") + assert_equal "application/json", f.last_response.headers["Content-Type"] end