Commit 27841825 by andrew morton

Set headers for parameters in: :header

parent c4b5175f
......@@ -26,10 +26,25 @@ module RSpec
metadata[:swagger_operation][:consumes] || document[:consumes]
end
def parameters
def parameters location = nil
path_item = metadata[:swagger_path_item] || {}
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
def headers
......@@ -39,7 +54,10 @@ module RSpec
headers['HTTP_ACCEPT'] = produces.join(';') if produces.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
end
......@@ -55,19 +73,14 @@ module RSpec
end
def query
# Don't bother looking at the full parameter bodies since all we need
# are location and name which are the key.
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?
query_params = parameter_values(:query).to_query
"?#{query_params}" unless query_params.blank?
end
def 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.
if key = parameters.keys.find{ |k| k.starts_with? 'body&' }
if key = parameters(:body).keys.first
instance.send(key.split('&').last).to_json
end
end
......
......@@ -47,7 +47,6 @@ RSpec.describe RSpec::Swagger::Helpers::PathItem do
subject { klass.new }
describe "#operation" do
it "requires a verb" do
expect(subject).to receive(:describe).with('get', {
swagger_object: :operation,
......
......@@ -101,12 +101,14 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
end
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(:consumes) { }
before do
allow(subject).to receive(:produces) { produces }
allow(subject).to receive(:consumes) { consumes }
allow(subject).to receive(:parameters).with(:header) { {} }
end
context 'when produces is present' do
......@@ -135,7 +137,15 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
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
......@@ -172,10 +182,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with no query params' do
it 'returns nil' do
expect(subject).to receive(:parameters) { {
'path&petId' => { this_is: :ignored },
'body&site' => { same: :here }
} }
expect(subject).to receive(:parameters).with(:query) { {} }
expect(subject.query).to be_nil
end
......@@ -183,8 +190,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with query params' do
it 'returns them in a string' do
expect(subject).to receive(:parameters) { {
'path&petId' => { this_is: :ignored },
expect(subject).to receive(:parameters).with(:query) { {
'query&site' => { same: :here }
} }
expect(instance).to receive(:site) { :pickles }
......@@ -200,10 +206,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with no body param' do
it 'returns nil' do
expect(subject).to receive(:parameters) { {
'path&petId' => { this_is: :ignored },
'query&site' => { same: :here }
} }
expect(subject).to receive(:parameters).with(:body) { {} }
expect(subject.body).to be_nil
end
......@@ -211,8 +214,7 @@ RSpec.describe RSpec::Swagger::RequestBuilder do
context 'with a body param' do
it 'returns a serialized JSON string' do
expect(subject).to receive(:parameters) { {
'path&petId' => { this_is: :ignored },
expect(subject).to receive(:parameters).with(:body) { {
'body&site' => { same: :here }
} }
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