diff --git a/CHANGELOG.md b/CHANGELOG.md index c3949fe..5b0f83c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ -# 1.0.0 (??-03-2021) +# 1.1.0 (02-09-2021) + +- You can now set your own application name for startup message [#606](https://github.com/kemalcr/kemal/pull/606). Thanks @aravindavk :pray: +- Add array of paths support for before/after filters [#605](https://github.com/kemalcr/kemal/pull/605). Thanks @sdogruyol :pray: +- Fixed executing filters when before and after is defined at the same time [#612](https://github.com/kemalcr/kemal/pull/612). Thanks @mamantoha :pray: +- Set content type to text/html for 500 exceptions [#616](https://github.com/kemalcr/kemal/pull/616). Thanks @sdogruyol :pray: + +# 1.0.0 (22-03-2021) - Crystal 1.0.0 support :tada: - Update Radix to use latest 0.4.0 [#596](https://github.com/kemalcr/kemal/pull/596). Thanks @luislavena :pray: diff --git a/README.md b/README.md index 9f9ff64..aba67b4 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ See also [Getting Started](http://kemalcr.com/guide/). - Middleware support - Built-in JSON support - Built-in static file serving -- Built-in view templating via [Kilt](https://github.com/jeromegn/kilt) +- Built-in view templating via [ECR](https://crystal-lang.org/api/ECR.html) # Documentation diff --git a/shard.yml b/shard.yml index e37bdde..72011fd 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: kemal -version: 1.0.0 +version: 1.1.0 authors: - Serdar Dogruyol @@ -8,9 +8,6 @@ dependencies: radix: github: luislavena/radix version: ~> 0.4.0 - kilt: - github: jeromegn/kilt - version: ~> 0.6.0 exception_page: github: crystal-loot/exception_page version: ~> 0.2.0 diff --git a/spec/exception_handler_spec.cr b/spec/exception_handler_spec.cr index b9519e9..7064e84 100644 --- a/spec/exception_handler_spec.cr +++ b/spec/exception_handler_spec.cr @@ -59,6 +59,30 @@ describe "Kemal::ExceptionHandler" do response.body.should eq "Something happened" end + it "overrides the content type for filters" do + before_get do |env| + env.response.content_type = "application/json" + end + error 500 do |_, err| + err.message + end + get "/" do |env| + env.response.status_code = 500 + end + request = HTTP::Request.new("GET", "/") + io = IO::Memory.new + response = HTTP::Server::Response.new(io) + context = HTTP::Server::Context.new(request, response) + Kemal::ExceptionHandler::INSTANCE.next = Kemal::RouteHandler::INSTANCE + Kemal::ExceptionHandler::INSTANCE.call(context) + response.close + io.rewind + response = HTTP::Client::Response.from_io(io, decompress: false) + response.status_code.should eq 500 + response.headers["Content-Type"].should eq "text/html" + response.body.should eq "Rendered error with 500" + end + it "keeps the specified error Content-Type" do error 500 do "Something happened" diff --git a/spec/middleware/filters_spec.cr b/spec/middleware/filters_spec.cr index 9bc2564..5c3b477 100644 --- a/spec/middleware/filters_spec.cr +++ b/spec/middleware/filters_spec.cr @@ -183,6 +183,30 @@ describe "Kemal::FilterHandler" do client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) client_response.body.should eq("true") end + + it "executes code before and after request" do + before_filter = FilterTest.new + before_filter.modified = "false" + + after_filter = FilterTest.new + after_filter.modified = "false" + + filter_middleware = Kemal::FilterHandler.new + filter_middleware._add_route_filter("ALL", "*", :before) { before_filter.modified = "true" } + filter_middleware._add_route_filter("ALL", "*", :after) { after_filter.modified = "true" } + + kemal = Kemal::RouteHandler::INSTANCE + kemal.add_route "GET", "/" { "#{before_filter.modified}-#{after_filter.modified}" } + + before_filter.modified.should eq("false") + after_filter.modified.should eq("false") + + request = HTTP::Request.new("GET", "/") + create_request_and_return_io_and_context(filter_middleware, request) + io_with_context = create_request_and_return_io_and_context(kemal, request)[0] + client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response.body.should eq("true-true") + end end class FilterTest diff --git a/src/kemal/filter_handler.cr b/src/kemal/filter_handler.cr index 6d28680..298ce26 100644 --- a/src/kemal/filter_handler.cr +++ b/src/kemal/filter_handler.cr @@ -71,7 +71,7 @@ module Kemal end private def radix_path(verb : String?, path : String, type : Symbol) - "#{type}/#{verb}/#{path}" + "/#{type}/#{verb}/#{path}" end # :nodoc: diff --git a/src/kemal/helpers/macros.cr b/src/kemal/helpers/macros.cr index 4b5e309..41c8793 100644 --- a/src/kemal/helpers/macros.cr +++ b/src/kemal/helpers/macros.cr @@ -1,5 +1,3 @@ -require "kilt" - CONTENT_FOR_BLOCKS = Hash(String, Tuple(String, Proc(String))).new # `content_for` is a set of helpers that allows you to capture @@ -37,9 +35,9 @@ CONTENT_FOR_BLOCKS = Hash(String, Tuple(String, Proc(String))).new # setting the appropriate set of tags that should be added to the layout. macro content_for(key, file = __FILE__) %proc = ->() { - __kilt_io__ = IO::Memory.new + __view_io__ = IO::Memory.new {{ yield }} - __kilt_io__.to_s + __view_io__.to_s } CONTENT_FOR_BLOCKS[{{key}}] = Tuple.new {{file}}, %proc @@ -62,13 +60,15 @@ end # ``` macro render(filename, layout) __content_filename__ = {{filename}} - content = render {{filename}} - render {{layout}} + io = IO::Memory.new + content = ECR.embed {{filename}}, io + ECR.embed {{layout}}, io + io.to_s end # Render view with the given filename. macro render(filename) - Kilt.render({{filename}}) + ECR.render({{filename}}) end # Halt execution with the current context. diff --git a/src/kemal/helpers/templates.cr b/src/kemal/helpers/templates.cr index b343fc8..0468d38 100644 --- a/src/kemal/helpers/templates.cr +++ b/src/kemal/helpers/templates.cr @@ -22,6 +22,7 @@ def render_404 end def render_500(context, exception, verbosity) + context.response.content_type = "text/html" context.response.status_code = 500 template = if verbosity