Commit 27841825 by andrew morton

Set headers for parameters in: :header

parent c4b5175f
...@@ -26,10 +26,25 @@ module RSpec ...@@ -26,10 +26,25 @@ module RSpec
metadata[:swagger_operation][:consumes] || document[:consumes] metadata[:swagger_operation][:consumes] || document[:consumes]
end end
def parameters def parameters location = nil
path_item = metadata[:swagger_path_item] || {} path_item = metadata[:swagger_path_item] || {}
operation = metadata[:swagger_operation] || {} operation = metadata[:swagger_operation] || {}
path_item.fetch(:parameters, {}).merge(operation.fetch(:parameters, {})) params = path_item.fetch(:parameters, {}).merge(operation.fetch(:parameters, {}))
if location.present?
params.select{ |k, _| k.starts_with? "#{location}&" }
else
params
end
end
def parameter_values location
# Don't bother looking at the full parameter bodies since all we need
# are location and name which are in the key.
values = parameters(location)
.keys
.map{ |k| k.split('&').last }
.map{ |name| [name, instance.send(name)] }
Hash[values]
end end
def headers def headers
...@@ -39,7 +54,10 @@ module RSpec ...@@ -39,7 +54,10 @@ module RSpec
headers['HTTP_ACCEPT'] = produces.join(';') if produces.present? headers['HTTP_ACCEPT'] = produces.join(';') if produces.present?
headers['CONTENT_TYPE'] = consumes.first if consumes.present? headers['CONTENT_TYPE'] = consumes.first if consumes.present?
# TODO needs to pull in parameters with in: :header set. # TODO: do we need to do some capitalization to match the rack
# conventions?
parameter_values(:header).each { |k, v| headers[k] = v }
headers headers
end end
...@@ -55,19 +73,14 @@ module RSpec ...@@ -55,19 +73,14 @@ module RSpec
end end
def query def query
# Don't bother looking at the full parameter bodies since all we need query_params = parameter_values(:query).to_query
# are location and name which are the key. "?#{query_params}" unless query_params.blank?
query_params = parameters.keys.map{ |k| k.split('&')}
.select{ |location, name| location == 'query' }
.map{ |location, name| [name, instance.send(name)] }
'?' + Hash[query_params].to_query unless query_params.empty?
end end
def body def body
# And here all we need is the first half of the key to find the body # And here all we need is the first half of the key to find the body
# parameter and its name to fetch a value. # parameter and its name to fetch a value.
if key = parameters.keys.find{ |k| k.starts_with? 'body&' } if key = parameters(:body).keys.first
instance.send(key.split('&').last).to_json instance.send(key.split('&').last).to_json
end end
end end
......
...@@ -47,7 +47,6 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do ...@@ -47,7 +47,6 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
subject { klass.new } subject { klass.new }
describe "#operation" do describe "#operation" do
it "requires a verb" do it "requires a verb" do
expect(subject).to receive(:describe).with('get', { expect(subject).to receive(:describe).with('get', {
swagger_object: :operation, swagger_object: :operation,
......
...@@ -101,12 +101,14 @@ RSpec.describe RSpec::Swagger::RequestBuilder do ...@@ -101,12 +101,14 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
end end
describe '#headers' do describe '#headers' do
subject { described_class.new(double('metadata'), double('instance')) } subject { described_class.new(double('metadata'), instance) }
let(:instance) { double('instance') }
let(:produces) { } let(:produces) { }
let(:consumes) { } let(:consumes) { }
before do before do
allow(subject).to receive(:produces) { produces } allow(subject).to receive(:produces) { produces }
allow(subject).to receive(:consumes) { consumes } allow(subject).to receive(:consumes) { consumes }
allow(subject).to receive(:parameters).with(:header) { {} }
end end
context 'when produces is present' do context 'when produces is present' do
...@@ -135,7 +137,15 @@ RSpec.describe RSpec::Swagger::RequestBuilder do ...@@ -135,7 +137,15 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
end end
end end
xit "includes parameters with in: :header" do context 'with header params' do
it 'returns them in a string' do
expect(subject).to receive(:parameters).with(:header) { {
'header&X-Magic' => { same: :here }
} }
expect(instance).to receive('X-Magic'.to_sym) { :pickles }
expect(subject.headers).to include('X-Magic' => :pickles)
end
end end
end end
...@@ -172,10 +182,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do ...@@ -172,10 +182,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with no query params' do context 'with no query params' do
it 'returns nil' do it 'returns nil' do
expect(subject).to receive(:parameters) { { expect(subject).to receive(:parameters).with(:query) { {} }
'path&petId' => { this_is: :ignored },
'body&site' => { same: :here }
} }
expect(subject.query).to be_nil expect(subject.query).to be_nil
end end
...@@ -183,8 +190,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do ...@@ -183,8 +190,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with query params' do context 'with query params' do
it 'returns them in a string' do it 'returns them in a string' do
expect(subject).to receive(:parameters) { { expect(subject).to receive(:parameters).with(:query) { {
'path&petId' => { this_is: :ignored },
'query&site' => { same: :here } 'query&site' => { same: :here }
} } } }
expect(instance).to receive(:site) { :pickles } expect(instance).to receive(:site) { :pickles }
...@@ -200,10 +206,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do ...@@ -200,10 +206,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with no body param' do context 'with no body param' do
it 'returns nil' do it 'returns nil' do
expect(subject).to receive(:parameters) { { expect(subject).to receive(:parameters).with(:body) { {} }
'path&petId' => { this_is: :ignored },
'query&site' => { same: :here }
} }
expect(subject.body).to be_nil expect(subject.body).to be_nil
end end
...@@ -211,8 +214,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do ...@@ -211,8 +214,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with a body param' do context 'with a body param' do
it 'returns a serialized JSON string' do it 'returns a serialized JSON string' do
expect(subject).to receive(:parameters) { { expect(subject).to receive(:parameters).with(:body) { {
'path&petId' => { this_is: :ignored },
'body&site' => { same: :here } 'body&site' => { same: :here }
} } } }
expect(instance).to receive(:site) { { name: :pickles, team: :cowboys } } expect(instance).to receive(:site) { { name: :pickles, team: :cowboys } }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment