From 30bdcc9759e03f7b4f1157a05ffb0d1d133fc975 Mon Sep 17 00:00:00 2001 From: Cris Ward Date: Mon, 2 Oct 2017 21:47:43 +0100 Subject: [PATCH] only return strings from routes fixes #374 (#408) --- spec/route_handler_spec.cr | 10 ++++++++++ src/kemal/route.cr | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) 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