Commit a48ef4a2 by andrew morton Committed by GitHub

Merge pull request #19 from drewish/little-fixes

Little fixes
parents 315e948a fcd9706f
......@@ -62,7 +62,7 @@ module RSpec
when :path_item
["\n", metadata[:swagger_path_item][:path]]
when :operation
["\n ", metadata[:swagger_operation][:method].to_s, "\t"]
["\n ", "%-8s" % metadata[:swagger_operation][:method]]
end
end
......@@ -107,7 +107,7 @@ module RSpec
operation[:parameters] = prepare_parameters(swagger_operation[:parameters])
end
operation.merge!(swagger_operation.slice(
:summary, :description, :externalDocs, :operationId,
:tags, :summary, :description, :externalDocs, :operationId,
:consumes, :produces, :schemes, :deprecated, :security
))
end
......
......@@ -174,6 +174,10 @@ module RSpec
metadata[:swagger_operation][:produces] = mime_types
end
def tags *tags
metadata[:swagger_operation][:tags] = tags
end
def response status_code, attributes = {}, &block
attributes.symbolize_keys!
......
......@@ -20,11 +20,11 @@ module RSpec
end
def produces
metadata[:swagger_operation][:produces] || document[:produces]
Array(metadata[:swagger_operation][:produces]).presence || Array(document[:produces])
end
def consumes
metadata[:swagger_operation][:consumes] || document[:consumes]
Array(metadata[:swagger_operation][:consumes]).presence || Array(document[:consumes])
end
def parameters location = nil
......@@ -52,7 +52,7 @@ module RSpec
headers = {}
# Match the names that Rails uses internally
headers['HTTP_ACCEPT'] = produces.join(';') if produces.present?
headers['HTTP_ACCEPT'] = produces.first if produces.present?
headers['CONTENT_TYPE'] = consumes.first if consumes.present?
# TODO: do we need to do some capitalization to match the rack
......
......@@ -186,6 +186,36 @@ RSpec.describe RSpec::Rails::Swagger::Helpers::Operation do
end
subject { klass.new }
describe '#consumes' do
before { subject.metadata = {swagger_operation: {}} }
it 'accepts an array' do
subject.consumes('foo', 'bar')
expect(subject.metadata[:swagger_operation][:consumes]).to eq ['foo', 'bar']
end
end
describe '#produces' do
before { subject.metadata = {swagger_operation: {}} }
it 'accepts an array' do
subject.produces('foo', 'bar')
expect(subject.metadata[:swagger_operation][:produces]).to eq ['foo', 'bar']
end
end
describe '#tags' do
before { subject.metadata = {swagger_operation: {}} }
it 'accepts an array' do
subject.tags('foo', 'bar')
expect(subject.metadata[:swagger_operation][:tags]).to eq ['foo', 'bar']
end
end
describe '#response' do
before { subject.metadata = {swagger_object: :operation} }
......
......@@ -37,11 +37,19 @@ RSpec.describe RSpec::Rails::Swagger::RequestBuilder do
let(:document) { double }
before { allow(subject).to receive(:document) { document } }
context 'with value in operation' do
context 'with string in operation' do
let(:metadata) { { swagger_operation: {produces: 'something' } } }
it 'converts it to an array' do
expect(subject.produces).to eq ['something']
end
end
context 'with array in operation' do
let(:metadata) { { swagger_operation: {produces: 'something' } } }
it 'uses that value' do
expect(subject.produces).to eq 'something'
expect(subject.produces).to eq ['something']
end
end
......@@ -51,7 +59,7 @@ RSpec.describe RSpec::Rails::Swagger::RequestBuilder do
it 'uses the value from the document' do
expect(document).to receive(:[]).with(:produces) { 'or other' }
expect(subject.produces).to eq 'or other'
expect(subject.produces).to eq ['or other']
end
end
end
......@@ -61,11 +69,19 @@ RSpec.describe RSpec::Rails::Swagger::RequestBuilder do
let(:document) { double }
before { allow(subject).to receive(:document) { document } }
context 'with value in operation' do
context 'with string in operation' do
let(:metadata) { { swagger_operation: {consumes: 'something' } } }
it 'converts it to an array' do
expect(subject.consumes).to eq ['something']
end
end
context 'with array in operation' do
let(:metadata) { { swagger_operation: {consumes: ['something'] } } }
it 'uses that value' do
expect(subject.consumes).to eq 'something'
expect(subject.consumes).to eq ['something']
end
end
......@@ -75,7 +91,7 @@ RSpec.describe RSpec::Rails::Swagger::RequestBuilder do
it 'uses the value from the document' do
expect(document).to receive(:[]).with(:consumes) { 'or other' }
expect(subject.consumes).to eq 'or other'
expect(subject.consumes).to eq ['or other']
end
end
end
......@@ -111,26 +127,40 @@ RSpec.describe RSpec::Rails::Swagger::RequestBuilder do
allow(subject).to receive(:parameters).with(:header) { {} }
end
context 'when produces is present' do
context 'when produces has a single value' do
let(:produces) { ['foo/bar'] }
it 'sets the Accept header' do
expect(subject.headers).to include('HTTP_ACCEPT' => 'foo/bar')
end
end
context 'when produces has multiple values' do
let(:produces) { ['foo/bar', 'bar/baz'] }
it 'sets the Accept header to the first' do
expect(subject.headers).to include('HTTP_ACCEPT' => 'foo/bar')
end
end
context 'when produces is blank' do
it 'does not set the Accept header' do
expect(subject.headers.keys).not_to include('HTTP_ACCEPT')
end
end
context 'when consumes is present' do
context 'when consumes has a single value' do
let(:consumes) { ['bar/baz'] }
it 'sets the Content-Type header' do
expect(subject.headers).to include('CONTENT_TYPE' => 'bar/baz')
end
end
context 'when consumes has multiple values' do
let(:consumes) { ['bar/baz', 'flooz/flop'] }
it 'sets the Content-Type header to the first' do
expect(subject.headers).to include('CONTENT_TYPE' => 'bar/baz')
end
end
context 'when consumes is blank' do
it 'does not set the Content-Type header' do
expect(subject.headers.keys).not_to include('CONTENT_TYPE')
......
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