shard-kemal/spec/spec_helper.cr

92 lines
2 KiB
Crystal
Raw Normal View History

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
def call(env)
call_next env
2016-07-28 18:12:55 +00:00
end
def write(message)
end
end
2016-02-12 12:11:21 +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)
def create_request_and_return_io_and_context(handler, request)
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
{io, context}
2016-01-24 10:22:25 +00:00
end
def create_ws_request_and_return_io_and_context(handler, request)
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
# Raises because the IO::Memory is empty
2016-01-24 10:22:25 +00:00
end
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
response.upgrade_handler.try &.call(io)
{% end %}
io.rewind
{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)
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)
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
def build_main_handler
Kemal.config.setup
main_handler = Kemal.config.handlers.first
current_handler = main_handler
Kemal.config.handlers.each do |handler|
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
Kemal.config.clear
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