From 17f944ef9de493db5882e5ce4089669df57716ae Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Sun, 24 Jan 2016 12:52:41 +0200 Subject: [PATCH] Move redirect macro to context def --- spec/kemal_handler_spec.cr | 22 +++++------ spec/view_spec.cr | 80 +++++++++++++++++++------------------- src/kemal/context.cr | 5 +++ src/kemal/macros.cr | 5 --- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/spec/kemal_handler_spec.cr b/spec/kemal_handler_spec.cr index 91f2f32..c18f8ac 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/kemal_handler_spec.cr @@ -196,15 +196,15 @@ describe "Kemal::Handler" do client_response.status_code.should eq(404) end - # it "redirects user to provided url" do - # kemal = Kemal::Handler.new - # kemal.add_route "GET", "/" do |env| - # redirect "/login" - # end - # request = HTTP::Request.new("GET", "/") - # io_with_context = create_request_and_return_io(kemal, request) - # client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) - # client_response.status_code.should eq(301) - # client_response.headers.has_key?("Location").should eq(true) - # end + it "redirects user to provided url" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/" do |env| + env.redirect "/login" + end + request = HTTP::Request.new("GET", "/") + io_with_context = create_request_and_return_io(kemal, request) + client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response.status_code.should eq(301) + client_response.headers.has_key?("Location").should eq(true) + end end diff --git a/spec/view_spec.cr b/spec/view_spec.cr index 0794df9..9cef43d 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -1,40 +1,40 @@ -# require "./spec_helper" -# -# macro render_with_base_and_layout(filename) -# render "spec/asset/#{{{filename}}}", "spec/asset/layout.ecr" -# end -# -# describe "Views" do -# it "renders file" do -# kemal = Kemal::Handler.new -# kemal.add_route "GET", "/view/:name" do |env| -# render "spec/asset/hello.ecr" -# end -# request = HTTP::Request.new("GET", "/view/world") -# io_with_context = create_request_and_return_io(kemal, request) -# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) -# client_response.body.should contain("Hello world") -# end -# -# it "renders file with dynamic variables" do -# kemal = Kemal::Handler.new -# kemal.add_route "GET", "/view/:name" do |env| -# render_with_base_and_layout "hello.ecr" -# end -# request = HTTP::Request.new("GET", "/view/world") -# io_with_context = create_request_and_return_io(kemal, request) -# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) -# client_response.body.should contain("Hello world") -# end -# -# it "renders layout" do -# kemal = Kemal::Handler.new -# kemal.add_route "GET", "/view/:name" do |env| -# render "spec/asset/hello.ecr", "spec/asset/layout.ecr" -# end -# request = HTTP::Request.new("GET", "/view/world") -# io_with_context = create_request_and_return_io(kemal, request) -# client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) -# client_response.body.should contain("Hello world") -# end -# end +require "./spec_helper" + +macro render_with_base_and_layout(filename) + render "spec/asset/#{{{filename}}}", "spec/asset/layout.ecr" +end + +describe "Views" do + # it "renders file" do + # kemal = Kemal::Handler.new + # kemal.add_route "GET", "/view/:name" do |env| + # render "spec/asset/hello.ecr" + # end + # request = HTTP::Request.new("GET", "/view/world") + # io_with_context = create_request_and_return_io(kemal, request) + # client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + # client_response.body.should contain("Hello world") + # end + + # it "renders file with dynamic variables" do + # kemal = Kemal::Handler.new + # kemal.add_route "GET", "/view/:name" do |env| + # render_with_base_and_layout "hello.ecr" + # end + # request = HTTP::Request.new("GET", "/view/world") + # io_with_context = create_request_and_return_io(kemal, request) + # client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + # client_response.body.should contain("Hello world") + # end + # + # it "renders layout" do + # kemal = Kemal::Handler.new + # kemal.add_route "GET", "/view/:name" do |env| + # render "spec/asset/hello.ecr", "spec/asset/layout.ecr" + # end + # request = HTTP::Request.new("GET", "/view/world") + # io_with_context = create_request_and_return_io(kemal, request) + # client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + # client_response.body.should contain("Hello world") + # end +end diff --git a/src/kemal/context.cr b/src/kemal/context.cr index ba8d664..3903177 100644 --- a/src/kemal/context.cr +++ b/src/kemal/context.cr @@ -7,5 +7,10 @@ class HTTP::Server def params Kemal::ParamParser.new(@route, @request).parse end + + def redirect(url) + @response.headers.add "Location", url + @response.status_code = 301 + end end end diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 6434e81..1175633 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -16,11 +16,6 @@ macro render(filename, layout) render {{layout}} end -macro redirect(url) - env.response.headers.add "Location", {{url}} - env.response.status_code = 301 -end - macro add_handler(handler) Kemal.config.add_handler {{handler}} end