kemal/spec/spec_helper.cr

86 lines
1.7 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)
2016-01-24 10:22:25 +00:00
def create_request_and_return_io(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
end
def create_ws_request_and_return_io(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
response.close
io
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
main_handler = Kemal.config.handlers.first
current_handler = main_handler
Kemal.config.handlers.each_with_index do |handler, index|
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"
2016-03-21 16:29:48 +00:00
config.setup
end
Spec.after_each do
Kemal.config.clear
2016-04-12 11:41:09 +00:00
Kemal::RouteHandler::INSTANCE.tree = Radix::Tree(Route).new
2015-11-25 21:35:04 +00:00
end