diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 56441d4..cc0cbb4 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -26,6 +26,6 @@ describe "Config" do it "adds a custom handler" do config = Kemal.config config.add_handler CustomTestHandler.new - config.handlers.size.should eq(1) + config.handlers.size.should eq(5) end end diff --git a/spec/context_spec.cr b/spec/context_spec.cr index 63ba9ab..8ffd7cd 100644 --- a/spec/context_spec.cr +++ b/spec/context_spec.cr @@ -2,50 +2,42 @@ require "./spec_helper" describe "Context" do it "has a default content type" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| "Hello" 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 = call_request_on_app(request) client_response.headers["Content-Type"].should eq("text/html") end it "sets content type" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| env.response.content_type = "application/json" "Hello" 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 = call_request_on_app(request) client_response.headers["Content-Type"].should eq("application/json") end it "parses headers" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| name = env.request.headers["name"] "Hello #{name}" end headers = HTTP::Headers.new headers["name"] = "kemal" request = HTTP::Request.new("GET", "/", headers) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq "Hello kemal" end it "sets response headers" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| env.response.headers.add "Accept-Language", "tr" 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 = call_request_on_app(request) client_response.headers["Accept-Language"].should eq "tr" end end diff --git a/spec/helpers_spec.cr b/spec/helpers_spec.cr index 5b79983..2df116e 100644 --- a/spec/helpers_spec.cr +++ b/spec/helpers_spec.cr @@ -4,7 +4,7 @@ describe "Macros" do describe "#basic_auth" do it "adds HTTPBasicAuthHandler" do basic_auth "serdar", "123" - Kemal.config.handlers.size.should eq 1 + Kemal.config.handlers.size.should eq 5 end end @@ -18,7 +18,7 @@ describe "Macros" do describe "#add_handler" do it "adds a custom handler" do add_handler CustomTestHandler.new - Kemal.config.handlers.size.should eq 1 + Kemal.config.handlers.size.should eq 5 end end @@ -30,7 +30,7 @@ describe "Macros" do it "sets a custom logger" do config = Kemal::Config::INSTANCE logger CustomLogHandler.new("production") - config.handlers.first.should be_a(CustomLogHandler) + config.handlers.last.should be_a(CustomLogHandler) config.logger.should be_a(CustomLogHandler) end end diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index 85c182e..e713934 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -2,52 +2,43 @@ require "./spec_helper" describe "Kemal::RouteHandler" do it "routes" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do + get "/" do "hello" 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 = call_request_on_app(request) client_response.body.should eq("hello") end it "routes request with query string" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| "hello #{env.params.query["message"]}" end request = HTTP::Request.new("GET", "/?message=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 = call_request_on_app(request) client_response.body.should eq("hello world") end it "routes request with multiple query strings" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| "hello #{env.params.query["message"]} time #{env.params.query["time"]}" end request = HTTP::Request.new("GET", "/?message=world&time=now") - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("hello world time now") end it "route parameter has more precedence than query string arguments" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/:message" do |env| + get "/:message" do |env| "hello #{env.params.url["message"]}" end request = HTTP::Request.new("GET", "/world?message=coco") - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("hello world") end it "parses simple JSON body" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "POST", "/" do |env| + post "/" do |env| name = env.params.json["name"] age = env.params.json["age"] "Hello #{name} Age #{age}" @@ -60,14 +51,12 @@ describe "Kemal::RouteHandler" do body: json_payload.to_json, headers: HTTP::Headers{"Content-Type": "application/json"}, ) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Hello Serdar Age 26") end it "parses JSON with string array" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "POST", "/" do |env| + post "/" do |env| skills = env.params.json["skills"] as Array "Skills #{skills.each.join(',')}" end @@ -79,14 +68,12 @@ describe "Kemal::RouteHandler" do body: json_payload.to_json, headers: HTTP::Headers{"Content-Type": "application/json"}, ) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Skills ruby,crystal") end it "parses JSON with json object array" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "POST", "/" do |env| + post "/" do |env| skills = env.params.json["skills"] as Array skills_from_languages = skills.map do |skill| skill = skill as Hash @@ -103,14 +90,12 @@ describe "Kemal::RouteHandler" do headers: HTTP::Headers{"Content-Type": "application/json"}, ) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Skills ruby,crystal") end it "checks for _method param in POST request to simulate PUT" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "PUT", "/" do |env| + put "/" do |env| "Hello World from PUT" end request = HTTP::Request.new( @@ -119,14 +104,12 @@ describe "Kemal::RouteHandler" do body: "_method=PUT", headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"} ) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Hello World from PUT") end it "checks for _method param in POST request to simulate PATCH" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "PATCH", "/" do |env| + patch "/" do |env| "Hello World from PATCH" end request = HTTP::Request.new( @@ -135,14 +118,12 @@ describe "Kemal::RouteHandler" do body: "_method=PATCH", headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} ) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Hello World from PATCH") end it "checks for _method param in POST request to simulate DELETE" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "DELETE", "/" do |env| + delete "/" do |env| "Hello World from DELETE" end json_payload = {"_method": "DELETE"} @@ -152,39 +133,25 @@ describe "Kemal::RouteHandler" do body: "_method=DELETE", headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} ) - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Hello World from DELETE") end it "can process HTTP HEAD requests for defined GET routes" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + get "/" do |env| "Hello World from GET" end request = HTTP::Request.new("HEAD", "/") - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.status_code.should eq(200) end - # Removed until there is a way to test multiple middlewares - # it "can't process HTTP HEAD requests for undefined GET routes" do - # kemal = Kemal::RouteHandler::INSTANCE - # request = HTTP::Request.new("HEAD", "/") - # 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(404) - # end - it "redirects user to provided url" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/" do |env| + 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 = call_request_on_app(request) client_response.status_code.should eq(302) client_response.headers.has_key?("Location").should eq(true) end diff --git a/spec/route_spec.cr b/spec/route_spec.cr index 95c6b26..b69f020 100644 --- a/spec/route_spec.cr +++ b/spec/route_spec.cr @@ -3,16 +3,14 @@ require "./spec_helper" describe "Route" do describe "match?" do it "matches the correct route" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/route1" do |env| + get "/route1" do |env| "Route 1" end - kemal.add_route "GET", "/route2" do |env| + get "/route2" do |env| "Route 2" end request = HTTP::Request.new("GET", "/route2") - io_with_context = create_request_and_return_io(kemal, request) - client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response = call_request_on_app(request) client_response.body.should eq("Route 2") end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index be05359..4f829b1 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,6 +1,5 @@ require "spec" -require "../src/kemal/*" -require "../src/kemal/middleware/*" +require "../src/*" include Kemal @@ -35,10 +34,24 @@ def create_ws_request_and_return_io(handler, request) io end +def call_request_on_app(request) + io = MemoryIO.new + response = HTTP::Server::Response.new(io) + context = HTTP::Server::Context.new(request, response) + Kemal::RouteHandler::INSTANCE.call(context) + response.close + io.rewind + HTTP::Client::Response.from_io(io, decompress: false) +end + Spec.before_each do config = Kemal.config config.env = "development" - config.setup_logging - config.handlers.clear + config.setup + config.add_handler Kemal::RouteHandler::INSTANCE +end + +Spec.after_each do + Kemal.config.handlers.clear Kemal::RouteHandler::INSTANCE.tree = Radix::Tree.new end diff --git a/spec/view_spec.cr b/spec/view_spec.cr index 5906e3f..ab17b60 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -6,38 +6,32 @@ end describe "Views" do it "renders file" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/view/:name" do |env| + get "/view/:name" do |env| name = env.params.url["name"] 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 = call_request_on_app(request) client_response.body.should contain("Hello world") end it "renders file with dynamic variables" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/view/:name" do |env| + get "/view/:name" do |env| name = env.params.url["name"] 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 = call_request_on_app(request) client_response.body.should contain("Hello world") end it "renders layout" do - kemal = Kemal::RouteHandler::INSTANCE - kemal.add_route "GET", "/view/:name" do |env| + get "/view/:name" do |env| name = env.params.url["name"] 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 = call_request_on_app(request) client_response.body.should contain("Hello world") end end