Skip to content

Commit

Permalink
Merge pull request #25 from drewish/other-env
Browse files Browse the repository at this point in the history
Allow specs to pass values to request env
  • Loading branch information
drewish authored Oct 14, 2016
2 parents 7b91920 + 92fb1f1 commit fe94751
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/rspec/rails/swagger/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ def response status_code, attributes = {}, &block
method = builder.method
path = [builder.path, builder.query].join
headers = builder.headers
env = builder.env
body = builder.body

# Run the request
if ::Rails::VERSION::MAJOR >= 5
self.send(method, path, {params: body, headers: headers})
self.send(method, path, {params: body, headers: headers, env: env})
else
self.send(method, path, body, headers)
self.send(method, path, body, headers.merge(env))
end

if example.metadata[:capture_examples]
Expand Down
12 changes: 11 additions & 1 deletion lib/rspec/rails/swagger/request_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def consumes

##
# Returns parameters defined in the operation and path item. Providing
# a +location+ will limit the parameters by their `in` value.
# a +location+ will filter the parameters to those with a matching +in+
# value.
def parameters location = nil
path_item = metadata[:swagger_path_item] || {}
operation = metadata[:swagger_operation] || {}
Expand Down Expand Up @@ -70,6 +71,15 @@ def headers
headers
end

##
# If +instance+ defines an +env+ method this will return those values
# for inclusion in the Rack env hash.
def env
return {} unless instance.respond_to? :env

instance.env
end

def path
base_path = document[:basePath] || ''
# Find params in the path and replace them with values defined in
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/rails/swagger/request_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@
end
end

describe '#env' do
subject { described_class.new(double('metadata'), instance) }
let(:instance) { double('instance') }

context 'with no env method on the instance' do
it 'returns empty hash' do
expect(subject.env).to eq({})
end
end

context 'with env method on the instance' do
it 'returns the results' do
allow(instance).to receive(:env) { { foo: :bar } }
expect(subject.env).to eq({foo: :bar})
end
end
end

describe '#headers' do
subject { described_class.new(double('metadata'), instance) }
let(:instance) { double('instance') }
Expand Down

0 comments on commit fe94751

Please sign in to comment.