From 5376eb8bb24494afb733d2bc68e89e32d5223bdc Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Wed, 28 Oct 2015 20:52:34 +0200 Subject: [PATCH] Added context specs --- spec/context_spec.cr | 23 +++++++++++++++++++++++ spec/kemal_handler_spec.cr | 10 ---------- spec/param_parser_spec.cr | 18 ++++++++---------- src/kemal/context.cr | 10 ++++------ src/kemal/handler.cr | 2 +- 5 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 spec/context_spec.cr diff --git a/spec/context_spec.cr b/spec/context_spec.cr new file mode 100644 index 0000000..e141e24 --- /dev/null +++ b/spec/context_spec.cr @@ -0,0 +1,23 @@ +require "./spec_helper" + +describe "Context" do + it "has a default content type" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/" do |env| + "Hello" + end + request = HTTP::Request.new("GET", "/") + response = kemal.call(request) + response.headers["Content-Type"].should eq("text/plain") + end + + it "sets content type" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/" do |env| + env.set_content_type "application/json" + end + request = HTTP::Request.new("GET", "/") + response = kemal.call(request) + response.headers["Content-Type"].should eq("application/json") + end +end diff --git a/spec/kemal_handler_spec.cr b/spec/kemal_handler_spec.cr index b338e8e..a43cdec 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/kemal_handler_spec.cr @@ -40,14 +40,4 @@ describe "Kemal::Handler" do response = kemal.call(request) response.body.should eq("hello world") end - - it "sets content type" do - kemal = Kemal::Handler.new - kemal.add_route "GET", "/" do |env| - env.response.content_type = "application/json" - end - request = HTTP::Request.new("GET", "/") - response = kemal.call(request) - response.headers["Content-Type"].should eq("application/json") - end end diff --git a/spec/param_parser_spec.cr b/spec/param_parser_spec.cr index 06bac0e..2f65984 100644 --- a/spec/param_parser_spec.cr +++ b/spec/param_parser_spec.cr @@ -1,12 +1,11 @@ require "./spec_helper" describe "ParamParser" do - it "parses query params" do route = Route.new "POST", "/" do |env| - hasan = env.params["hasan"] - "Hello #{hasan}" - end + hasan = env.params["hasan"] + "Hello #{hasan}" + end request = HTTP::Request.new("POST", "/?hasan=cemal") params = Kemal::ParamParser.new(route, request).parse params["hasan"].should eq "cemal" @@ -14,14 +13,13 @@ describe "ParamParser" do it "parses request body" do route = Route.new "POST", "/" do |env| - name = env.params["name"] - age = env.params["age"] - hasan = env.params["hasan"] - "Hello #{name} #{hasan} #{age}" - end + name = env.params["name"] + age = env.params["age"] + hasan = env.params["hasan"] + "Hello #{name} #{hasan} #{age}" + end request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=serdar&age=99") params = Kemal::ParamParser.new(route, request).parse params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) end - end diff --git a/src/kemal/context.cr b/src/kemal/context.cr index 3713bc6..3b5582a 100644 --- a/src/kemal/context.cr +++ b/src/kemal/context.cr @@ -1,15 +1,13 @@ class Kemal::Context getter request getter params + getter content_type def initialize(@request, @params) + @content_type = "text/plain" end - def response - @response ||= Response.new - end - - def response? - @response + def set_content_type(content_type) + @content_type = content_type end end diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 719f207..f5e69aa 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 - content_type = context.response?.try(&.content_type) || "text/plain" + content_type = context.content_type return HTTP::Response.ok(content_type, body) rescue ex return HTTP::Response.error("text/plain", ex.to_s)