From b7506f6fab005a6e8b3329110c60d3ad5b9af765 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Sat, 4 Apr 2020 21:25:35 +0300 Subject: [PATCH] allow to set optional response body on redirects (#561) --- spec/route_handler_spec.cr | 12 ++++++++++++ src/kemal/ext/context.cr | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index a6db5e1..4ec111b 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -118,6 +118,18 @@ describe "Kemal::RouteHandler" do request = HTTP::Request.new("GET", "/") client_response = call_request_on_app(request) client_response.status_code.should eq(302) + client_response.body.should eq("") + client_response.headers.has_key?("Location").should eq(true) + end + + it "redirects with body" do + get "/" do |env| + env.redirect "/login", body: "Redirecting to /login" + end + request = HTTP::Request.new("GET", "/") + client_response = call_request_on_app(request) + client_response.status_code.should eq(302) + client_response.body.should eq("Redirecting to /login") client_response.headers.has_key?("Location").should eq(true) end end diff --git a/src/kemal/ext/context.cr b/src/kemal/ext/context.cr index f9a12ca..c2e51c6 100644 --- a/src/kemal/ext/context.cr +++ b/src/kemal/ext/context.cr @@ -17,9 +17,10 @@ class HTTP::Server @params ||= Kemal::ParamParser.new(@request, route_lookup.params) end - def redirect(url : String, status_code : Int32 = 302) + def redirect(url : String, status_code : Int32 = 302, *, body : String? = nil) @response.headers.add "Location", url @response.status_code = status_code + @response.print(body) if body end def route