Skip to content

Instantly share code, notes, and snippets.

@beauby
Created November 23, 2016 03:36
Show Gist options
  • Save beauby/fe02ecd48f68bcaa11033e08445af36f to your computer and use it in GitHub Desktop.
Save beauby/fe02ecd48f68bcaa11033e08445af36f to your computer and use it in GitHub Desktop.
module RSpec
module Hanami
class Request
def initialize(method, path, options)
@path, @query_string = path.split('?', 2)
@method = method
@params = options[:params]
@headers = options[:headers] || {}
end
def env
default_env.tap do |env|
env.merge!(
'REQUEST_METHOD' => @method,
'PATH_INFO' => @path,
'QUERY_STRING' => "?#{@query_string}"
)
env['rack.input'] = StringIO.new(@params.to_json) if @params
@headers.each do |k, v|
rack_name = k.upcase.tr('-', '_')
env["HTTP_#{rack_name}"] = v
end
end
end
def default_env
{
'SCRIPT_NAME' => '',
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => '800613',
'rack.version' => [1, 3],
'rack.url_scheme' => 'http',
'rack.input' => StringIO.new,
'rack.errors' => StringIO.new,
'rack.multithread' => false,
'rack.multiprocess' => false,
'rack.run_once' => false,
'rack.hijack?' => false
}
end
end
def self.included(klass)
klass.class_eval do
attr_reader :response
end
end
def request(request)
@response = ::Hanami.app.call(request.env)
end
def get(path, options = {})
request(Request.new('GET', path, options))
end
def post(path, options = {})
request(Request.new('POST', path, options))
end
def patch(path, options = {})
request(Request.new('PATCH', path, options))
end
def put(path, options = {})
request(Request.new('PUT', path, options))
end
def delete(path, options = {})
request(Request.new('DELETE', path, options))
end
end
end
include RSpec::Hanami
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment