kemal/spec/spec_helper.cr

58 lines
1.3 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-01-05 12:20:40 +00:00
class CustomTestHandler < HTTP::Handler
def call(request)
call_next request
end
end
2016-02-12 12:11:21 +00:00
class CustomLogHandler < Kemal::BaseLogHandler; end
2016-01-24 10:22:25 +00:00
def create_request_and_return_io(handler, request)
io = MemoryIO.new
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 = MemoryIO.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
begin
handler.call context
rescue IO::Error
# Raises because the MemoryIO is empty
end
response.close
io
end
2016-03-21 16:29:48 +00:00
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
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
config.add_handler Kemal::RouteHandler::INSTANCE
end
Spec.after_each do
Kemal.config.handlers.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