diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index c8831ff..8be01c0 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -10,6 +10,15 @@ describe "Kemal::RouteHandler" do client_response.body.should eq("hello") end + it "routes should only return strings" do + get "/" do + 100 + end + request = HTTP::Request.new("GET", "/") + client_response = call_request_on_app(request) + client_response.body.should eq("") + end + it "routes request with query string" do get "/" do |env| "hello #{env.params.query["message"]}" @@ -155,4 +164,5 @@ describe "Kemal::RouteHandler" do client_response.status_code.should eq(302) client_response.headers.has_key?("Location").should eq(true) end + end diff --git a/src/kemal/route.cr b/src/kemal/route.cr index c982b76..4dfb877 100644 --- a/src/kemal/route.cr +++ b/src/kemal/route.cr @@ -8,7 +8,8 @@ module Kemal def initialize(@method : String, @path : String, &handler : HTTP::Server::Context -> _) @handler = ->(context : HTTP::Server::Context) do - handler.call(context).to_s + output = handler.call(context) + output.is_a?(String) ? output : "" end end end