diff --git a/spec/context_spec.cr b/spec/context_spec.cr index 23fdb62..e1b5b08 100644 --- a/spec/context_spec.cr +++ b/spec/context_spec.cr @@ -14,7 +14,7 @@ describe "Context" do it "sets content type" do kemal = Kemal::Handler.new kemal.add_route "GET", "/" do |env| - env.set_content_type "application/json" + env.content_type = "application/json" end request = HTTP::Request.new("GET", "/") response = kemal.call(request) @@ -37,7 +37,7 @@ describe "Context" do it "sets response headers" do kemal = Kemal::Handler.new kemal.add_route "GET", "/" do |env| - env.set_header "Accept-Language", "tr" + env.add_header "Accept-Language", "tr" end request = HTTP::Request.new("GET", "/") response = kemal.call(request) diff --git a/src/kemal/context.cr b/src/kemal/context.cr index b3e981d..173bd71 100644 --- a/src/kemal/context.cr +++ b/src/kemal/context.cr @@ -10,15 +10,11 @@ class Kemal::Context @response = Kemal::Response.new end - def headers - @request.headers - end - def response_headers @response.headers end - def set_header(name, value) + def add_header(name, value) @response.headers.add name, value end @@ -26,9 +22,8 @@ class Kemal::Context @response.content_type end - def set_content_type(content_type) - @response.content_type = content_type - end - + delegate headers, @request delegate status_code, @response + delegate :"status_code=", @response + delegate :"content_type=", @response end diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 9241e16..fc8ba05 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -26,7 +26,7 @@ class Kemal::Handler < HTTP::Handler context = Context.new(request, params.not_nil!) begin body = route.handler.call(context).to_s - return HTTP::Response.new(200, body, context.response_headers) + return HTTP::Response.new(context.status_code, body, context.response_headers) rescue ex return HTTP::Response.error("text/plain", ex.to_s) end diff --git a/src/kemal/response.cr b/src/kemal/response.cr index 3630c03..406bfa1 100644 --- a/src/kemal/response.cr +++ b/src/kemal/response.cr @@ -4,7 +4,7 @@ class Kemal::Response property content_type def initialize - @status_code :: String + @status_code = 200 @content_type = "text/html" @headers = HTTP::Headers{"Content-Type": @content_type} end