From 92fb1f1d4680a47713a284e3931f7050c41e59a0 Mon Sep 17 00:00:00 2001 From: andrew morton Date: Sun, 2 Oct 2016 22:12:57 -0600 Subject: [PATCH] Allow specs to pass values to request env Fixes #23 --- lib/rspec/rails/swagger/helpers.rb | 5 +++-- lib/rspec/rails/swagger/request_builder.rb | 12 +++++++++++- .../rails/swagger/request_builder_spec.rb | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/rspec/rails/swagger/helpers.rb b/lib/rspec/rails/swagger/helpers.rb index 81100b9..2e4068f 100644 --- a/lib/rspec/rails/swagger/helpers.rb +++ b/lib/rspec/rails/swagger/helpers.rb @@ -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] diff --git a/lib/rspec/rails/swagger/request_builder.rb b/lib/rspec/rails/swagger/request_builder.rb index a2d27a2..21d4606 100644 --- a/lib/rspec/rails/swagger/request_builder.rb +++ b/lib/rspec/rails/swagger/request_builder.rb @@ -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] || {} @@ -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 diff --git a/spec/rspec/rails/swagger/request_builder_spec.rb b/spec/rspec/rails/swagger/request_builder_spec.rb index 4393a36..0d4670a 100644 --- a/spec/rspec/rails/swagger/request_builder_spec.rb +++ b/spec/rspec/rails/swagger/request_builder_spec.rb @@ -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') }