spec-kemal/src/spec-kemal.cr

49 lines
1.1 KiB
Crystal
Raw Normal View History

2016-04-09 14:37:46 +00:00
require "spec"
require "kemal"
2016-04-16 21:13:12 +00:00
Kemal.config.logging = false
2016-04-09 14:37:46 +00:00
2016-09-06 03:21:24 +00:00
class Global
@@response : HTTP::Client::Response?
def self.response=(@@response)
end
def self.response
@@response
end
end
2016-04-18 13:51:50 +00:00
2016-10-26 13:53:36 +00:00
{% for method in %w(get post put head delete patch) %}
def {{method.id}}(path, headers : HTTP::Headers? = nil, body : String? = nil)
2016-10-26 15:00:56 +00:00
request = HTTP::Request.new("{{method.id}}".upcase, path, headers, body )
2016-10-26 13:53:36 +00:00
Global.response = process_request request
2016-04-09 14:37:46 +00:00
end
2016-10-26 13:53:36 +00:00
{% end %}
2016-04-09 14:37:46 +00:00
2016-10-26 13:53:36 +00:00
def process_request(request)
2016-12-10 16:55:26 +00:00
io = IO::Memory.new
2016-10-26 13:53:36 +00:00
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
main_handler = build_main_handler
main_handler.call context
response.close
io.rewind
client_response = HTTP::Client::Response.from_io(io, decompress: false)
Global.response = client_response
2016-04-09 14:37:46 +00:00
end
2016-10-26 13:53:36 +00:00
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
2016-10-26 13:53:36 +00:00
main_handler
end
2016-04-18 13:51:50 +00:00
def response
2016-09-06 03:21:24 +00:00
Global.response.not_nil!
2016-04-18 13:51:50 +00:00
end