grape-swagger/spec/swagger_v2/default_api_spec.rb

176 lines
5.0 KiB
Ruby
Raw Normal View History

2013-06-18 21:56:15 +08:00
require 'spec_helper'
2015-08-27 04:54:03 +08:00
# require 'grape_version'
2013-06-18 21:56:15 +08:00
2014-07-14 21:59:11 +08:00
describe 'Default API' do
context 'with no additional options' do
2014-07-22 20:46:19 +08:00
def app
Class.new(Grape::API) do
format :json
desc 'This gets something.'
get '/something' do
{ bla: 'something' }
end
add_swagger_documentation
2013-06-18 21:56:15 +08:00
end
end
2014-07-22 20:46:19 +08:00
subject do
get '/swagger_doc'
JSON.parse(last_response.body)
2014-07-14 21:59:11 +08:00
end
2014-07-22 20:46:19 +08:00
it 'documents api' do
2015-10-05 02:05:22 +08:00
expect(subject).to eq(
2016-05-07 03:00:36 +08:00
'info' => { 'title' => 'API title', 'version' => '0.0.1' },
'swagger' => '2.0',
'produces' => ['application/json'],
'host' => 'example.org',
'tags' => [{ 'name' => 'something', 'description' => 'Operations about somethings' }],
'paths' => {
'/something' => {
'get' => {
'summary' => 'This gets something.',
2016-05-07 03:00:36 +08:00
'description' => 'This gets something.',
'produces' => ['application/json'],
'tags' => ['something'],
'operationId' => 'getSomething',
2016-05-11 04:01:13 +08:00
'responses' => { '200' => { 'description' => 'This gets something.' } }
}
}
}
2015-10-05 02:05:22 +08:00
)
2013-06-18 21:56:15 +08:00
end
2014-05-29 02:51:28 +08:00
2014-07-14 21:59:11 +08:00
context 'path inside the apis array' do
2014-07-22 20:46:19 +08:00
it 'starts with a forward slash' do
2015-08-27 04:54:03 +08:00
subject['paths'].each do |path|
2015-10-05 02:05:22 +08:00
expect(path.first).to start_with '/'
end
end
end
end
2015-10-05 02:05:22 +08:00
2015-08-20 00:18:27 +08:00
context 'with additional option block given to desc', if: GrapeVersion.satisfy?('>= 0.12.0') do
def app
Class.new(Grape::API) do
format :json
2015-12-19 03:29:48 +08:00
desc 'This gets something.'
get '/something' do
{ bla: 'something' }
end
add_swagger_documentation
end
end
subject do
get '/swagger_doc/something'
JSON.parse(last_response.body)
end
it 'documents endpoint' do
2016-05-07 03:00:36 +08:00
expect(subject).to eq('info' => { 'title' => 'API title', 'version' => '0.0.1' },
'swagger' => '2.0',
'produces' => ['application/json'],
'host' => 'example.org',
'tags' => [{ 'name' => 'something', 'description' => 'Operations about somethings' }],
'paths' => {
'/something' => {
'get' => {
'summary' => 'This gets something.',
2016-05-07 03:00:36 +08:00
'description' => 'This gets something.',
'produces' => ['application/json'],
'tags' => ['something'],
'operationId' => 'getSomething',
2016-05-11 04:01:13 +08:00
'responses' => { '200' => { 'description' => 'This gets something.' } }
}
}
})
end
2013-06-18 21:56:15 +08:00
end
2014-07-22 20:46:19 +08:00
context 'with additional info' do
def app
Class.new(Grape::API) do
format :json
add_swagger_documentation info: {
title: 'My API Title',
description: 'A description of my API',
license: 'Apache 2',
license_url: 'http://test.com',
terms_of_service_url: 'http://terms.com',
contact_email: 'support@test.com',
x: {
logo: 'http://logo.com/img.png'
}
}
2013-12-06 08:50:26 +08:00
end
2014-07-14 21:59:11 +08:00
end
subject do
2014-07-22 20:46:19 +08:00
get '/swagger_doc'
JSON.parse(last_response.body)['info']
end
2014-07-22 20:46:19 +08:00
it 'documents API title' do
expect(subject['title']).to eql('My API Title')
end
2014-07-22 20:46:19 +08:00
it 'documents API description' do
expect(subject['description']).to eql('A description of my API')
end
it 'should document the license' do
2015-10-05 02:05:22 +08:00
expect(subject['license']['name']).to eql('Apache 2')
end
2014-07-22 20:46:19 +08:00
it 'documents the license url' do
2015-10-05 02:05:22 +08:00
expect(subject['license']['url']).to eql('http://test.com')
end
2014-07-22 20:46:19 +08:00
it 'documents the terms of service url' do
expect(subject['termsOfServiceUrl']).to eql('http://terms.com')
end
2014-07-22 20:46:19 +08:00
it 'documents the contact email' do
2016-03-16 19:30:52 +08:00
expect(subject['contact']['email']).to eql('support@test.com')
2013-12-06 08:50:26 +08:00
end
it 'documents the extension field' do
expect(subject['x-logo']).to eql('http://logo.com/img.png')
end
2013-12-06 08:50:26 +08:00
end
context 'with tags' do
def app
Class.new(Grape::API) do
format :json
desc 'This gets something.'
get '/something' do
{ bla: 'something' }
end
get '/somethingelse' do
{ bla: 'somethingelse' }
end
add_swagger_documentation tags: [
{ name: 'something', description: 'customized description' }
]
end
end
subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end
it 'documents the customized tag' do
expect(subject['tags']).to eql(
[
{ 'name' => 'somethingelse', 'description' => 'Operations about somethingelses' },
{ 'name' => 'something', 'description' => 'customized description' }
]
)
end
end
2013-06-18 21:56:15 +08:00
end