2014-06-11 23:41:02 +00:00
|
|
|
require "spec"
|
2016-03-21 16:29:48 +00:00
|
|
|
require "../src/*"
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2015-10-23 18:33:26 +00:00
|
|
|
include Kemal
|
2015-11-25 21:35:04 +00:00
|
|
|
|
2016-07-28 18:12:55 +00:00
|
|
|
class CustomLogHandler < Kemal::BaseLogHandler
|
2022-06-29 10:31:28 +00:00
|
|
|
def call(context)
|
|
|
|
call_next(context)
|
2016-07-28 18:12:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def write(message)
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 12:11:21 +00:00
|
|
|
|
2017-04-06 18:43:41 +00:00
|
|
|
class TestContextStorageType
|
|
|
|
property id
|
|
|
|
@id = 1
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AnotherContextStorageType
|
|
|
|
property name
|
|
|
|
@name = "kemal-context"
|
|
|
|
end
|
|
|
|
|
|
|
|
add_context_storage_type(TestContextStorageType)
|
|
|
|
add_context_storage_type(AnotherContextStorageType)
|
|
|
|
|
2018-06-30 13:49:04 +00:00
|
|
|
def create_request_and_return_io_and_context(handler, request)
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-01-24 10:22:25 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
|
|
|
handler.call(context)
|
|
|
|
response.close
|
|
|
|
io.rewind
|
2018-06-30 13:49:04 +00:00
|
|
|
{io, context}
|
2016-01-24 10:22:25 +00:00
|
|
|
end
|
|
|
|
|
2018-06-30 13:49:04 +00:00
|
|
|
def create_ws_request_and_return_io_and_context(handler, request)
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-01-24 10:22:25 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
|
|
|
begin
|
|
|
|
handler.call context
|
|
|
|
rescue IO::Error
|
2016-11-22 20:29:10 +00:00
|
|
|
# Raises because the IO::Memory is empty
|
2016-01-24 10:22:25 +00:00
|
|
|
end
|
2020-06-10 18:11:43 +00:00
|
|
|
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
|
|
|
|
response.upgrade_handler.try &.call(io)
|
|
|
|
{% end %}
|
2017-09-10 11:41:07 +00:00
|
|
|
io.rewind
|
2018-06-30 13:49:04 +00:00
|
|
|
{io, context}
|
2016-01-24 10:22:25 +00:00
|
|
|
end
|
|
|
|
|
2016-03-21 16:29:48 +00:00
|
|
|
def call_request_on_app(request)
|
2016-11-22 20:29:10 +00:00
|
|
|
io = IO::Memory.new
|
2016-03-21 16:29:48 +00:00
|
|
|
response = HTTP::Server::Response.new(io)
|
|
|
|
context = HTTP::Server::Context.new(request, response)
|
2016-10-28 08:35:34 +00:00
|
|
|
main_handler = build_main_handler
|
|
|
|
main_handler.call context
|
2016-03-21 16:29:48 +00:00
|
|
|
response.close
|
|
|
|
io.rewind
|
|
|
|
HTTP::Client::Response.from_io(io, decompress: false)
|
|
|
|
end
|
|
|
|
|
2016-10-28 08:35:34 +00:00
|
|
|
def build_main_handler
|
2017-07-11 08:22:35 +00:00
|
|
|
Kemal.config.setup
|
2016-10-28 08:35:34 +00:00
|
|
|
main_handler = Kemal.config.handlers.first
|
|
|
|
current_handler = main_handler
|
2018-05-17 08:07:40 +00:00
|
|
|
Kemal.config.handlers.each do |handler|
|
2016-10-28 08:35:34 +00:00
|
|
|
current_handler.next = handler
|
|
|
|
current_handler = handler
|
|
|
|
end
|
|
|
|
main_handler
|
|
|
|
end
|
|
|
|
|
2015-11-25 21:35:04 +00:00
|
|
|
Spec.before_each do
|
2016-02-12 12:11:21 +00:00
|
|
|
config = Kemal.config
|
|
|
|
config.env = "development"
|
2017-08-24 15:52:10 +00:00
|
|
|
config.logging = false
|
2016-03-21 16:29:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Spec.after_each do
|
2016-10-28 08:35:34 +00:00
|
|
|
Kemal.config.clear
|
2022-09-15 08:52:28 +00:00
|
|
|
Kemal::FilterHandler::INSTANCE.tree = Radix::Tree(Array(Kemal::FilterHandler::FilterBlock)).new
|
2017-10-06 09:46:58 +00:00
|
|
|
Kemal::RouteHandler::INSTANCE.routes = Radix::Tree(Route).new
|
2018-09-16 10:17:51 +00:00
|
|
|
Kemal::RouteHandler::INSTANCE.cached_routes = Hash(String, Radix::Result(Route)).new
|
2017-10-06 09:46:58 +00:00
|
|
|
Kemal::WebSocketHandler::INSTANCE.routes = Radix::Tree(WebSocket).new
|
2015-11-25 21:35:04 +00:00
|
|
|
end
|