Skip to content

Commit fe94751

Browse files
authored
Merge pull request #25 from drewish/other-env
Allow specs to pass values to request env
2 parents 7b91920 + 92fb1f1 commit fe94751

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/rspec/rails/swagger/helpers.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,14 @@ def response status_code, attributes = {}, &block
204204
method = builder.method
205205
path = [builder.path, builder.query].join
206206
headers = builder.headers
207+
env = builder.env
207208
body = builder.body
208209

209210
# Run the request
210211
if ::Rails::VERSION::MAJOR >= 5
211-
self.send(method, path, {params: body, headers: headers})
212+
self.send(method, path, {params: body, headers: headers, env: env})
212213
else
213-
self.send(method, path, body, headers)
214+
self.send(method, path, body, headers.merge(env))
214215
end
215216

216217
if example.metadata[:capture_examples]

lib/rspec/rails/swagger/request_builder.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def consumes
3636

3737
##
3838
# Returns parameters defined in the operation and path item. Providing
39-
# a +location+ will limit the parameters by their `in` value.
39+
# a +location+ will filter the parameters to those with a matching +in+
40+
# value.
4041
def parameters location = nil
4142
path_item = metadata[:swagger_path_item] || {}
4243
operation = metadata[:swagger_operation] || {}
@@ -70,6 +71,15 @@ def headers
7071
headers
7172
end
7273

74+
##
75+
# If +instance+ defines an +env+ method this will return those values
76+
# for inclusion in the Rack env hash.
77+
def env
78+
return {} unless instance.respond_to? :env
79+
80+
instance.env
81+
end
82+
7383
def path
7484
base_path = document[:basePath] || ''
7585
# Find params in the path and replace them with values defined in

spec/rspec/rails/swagger/request_builder_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@
170170
end
171171
end
172172

173+
describe '#env' do
174+
subject { described_class.new(double('metadata'), instance) }
175+
let(:instance) { double('instance') }
176+
177+
context 'with no env method on the instance' do
178+
it 'returns empty hash' do
179+
expect(subject.env).to eq({})
180+
end
181+
end
182+
183+
context 'with env method on the instance' do
184+
it 'returns the results' do
185+
allow(instance).to receive(:env) { { foo: :bar } }
186+
expect(subject.env).to eq({foo: :bar})
187+
end
188+
end
189+
end
190+
173191
describe '#headers' do
174192
subject { described_class.new(double('metadata'), instance) }
175193
let(:instance) { double('instance') }

0 commit comments

Comments
 (0)