diff --git a/lib/rspec/hanami/request_helpers.rb b/lib/rspec/hanami/request_helpers.rb index cf07a41..328cebc 100644 --- a/lib/rspec/hanami/request_helpers.rb +++ b/lib/rspec/hanami/request_helpers.rb @@ -2,17 +2,17 @@ module RSpec module Hanami module RequestHelpers class Request - def initialize(method, path, options) + def initialize(method, path, opts) @path, @query_string = path.split('?', 2) @method = method - @params = options[:params] - @headers = options[:headers] || {} + @params = opts[:params] + @headers = opts[:headers] || {} end def env default_env.tap do |env| - env['PATH_INFO'] = @path, - env['REQUEST_METHOD'] = @method, + env['PATH_INFO'] = @path + env['REQUEST_METHOD'] = @method env['QUERY_STRING'] = "?#{@query_string}" env['rack.input'] = StringIO.new(@params.to_json) if @params @@ -47,27 +47,33 @@ def self.included(klass) end def request(request) + puts request.env @response = ::Hanami.app.call(request.env) end - def get(path, options = {}) - request(Request.new('GET', path, options)) + def get(path, opts = {}) + request(Request.new('GET', path, opts)) end - def post(path, options = {}) - request(Request.new('POST', path, options)) + def post(path, opts = {}) + request(Request.new('POST', path, opts)) end - def patch(path, options = {}) - request(Request.new('PATCH', path, options)) + def patch(path, opts = {}) + request(Request.new('PATCH', path, opts)) end - def put(path, options = {}) - request(Request.new('PUT', path, options)) + def put(path, opts = {}) + request(Request.new('PUT', path, opts)) end - def delete(path, options = {}) - request(Request.new('DELETE', path, options)) + + def delete(path, opts = {}) + request(Request.new('DELETE', path, opts)) + end + + def options(path, opts = {}) + request(Request.new('OPTIONS', path, opts)) end end end diff --git a/spec/rspec/hanami/request_spec.rb b/spec/rspec/hanami/request_spec.rb index 449296c..9b373f7 100644 --- a/spec/rspec/hanami/request_spec.rb +++ b/spec/rspec/hanami/request_spec.rb @@ -5,13 +5,15 @@ describe '#env' do [['GET', '/', {}], ['POST', '/users', {}], + ['OPTIONS', '/users', {}], ['PATCH', '/posts', {}], ['PUT', '/comments', {}], ['DELETE', '/tags', {}]].each do |method, path, options| it "returns hash for #{path} #{method}" do request = RSpec::Hanami::RequestHelpers::Request.new(method, path, options).env expect(request["REQUEST_METHOD"]).to eq method - expect(request["PATH_INFO"]).to eq [path, method, "?"] + expect(request["PATH_INFO"]).to eq path + expect(request["QUERY_STRING"]).to eq "?" end end end