Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions lib/rspec/hanami/request_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ module RSpec
module Hanami
module RequestHelpers
class Request
def initialize(method, path, options)
def initialize(method, path, opts)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you rename this?

@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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion spec/rspec/hanami/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down