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
|
2013-11-25 22:35:04 +08:00
|
|
|
context 'with no additional options' do
|
2014-07-22 20:46:19 +08:00
|
|
|
def app
|
|
|
|
Class.new(Grape::API) do
|
2013-11-25 22:35:04 +08:00
|
|
|
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
|
2013-11-25 22:35:04 +08:00
|
|
|
|
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' => {
|
|
|
|
'description' => 'This gets something.',
|
|
|
|
'produces' => ['application/json'],
|
|
|
|
'tags' => ['something'],
|
|
|
|
'operationId' => 'getSomething',
|
|
|
|
'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 '/'
|
2013-12-06 09:31:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-08-18 22:36:31 +08:00
|
|
|
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
|
2015-08-18 22:36:31 +08:00
|
|
|
def app
|
|
|
|
Class.new(Grape::API) do
|
|
|
|
format :json
|
2015-12-19 03:29:48 +08:00
|
|
|
desc 'This gets something.'
|
2015-08-18 22:36:31 +08:00
|
|
|
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' => {
|
|
|
|
'description' => 'This gets something.',
|
|
|
|
'produces' => ['application/json'],
|
|
|
|
'tags' => ['something'],
|
|
|
|
'operationId' => 'getSomething',
|
|
|
|
'responses' => { '200' => { 'description' => 'This gets something.' } } } } })
|
2015-08-18 22:36:31 +08:00
|
|
|
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
|
2013-11-25 22:35:04 +08:00
|
|
|
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',
|
2015-10-05 02:05:22 +08:00
|
|
|
contact_email: 'support@test.com'
|
2013-11-25 22:35:04 +08:00
|
|
|
}
|
2013-12-06 08:50:26 +08:00
|
|
|
end
|
2014-07-14 21:59:11 +08:00
|
|
|
end
|
2013-11-25 22:35:04 +08:00
|
|
|
|
|
|
|
subject do
|
2014-07-22 20:46:19 +08:00
|
|
|
get '/swagger_doc'
|
2013-11-25 22:35:04 +08:00
|
|
|
JSON.parse(last_response.body)['info']
|
|
|
|
end
|
|
|
|
|
2014-07-22 20:46:19 +08:00
|
|
|
it 'documents API title' do
|
2013-11-25 22:35:04 +08:00
|
|
|
expect(subject['title']).to eql('My API Title')
|
|
|
|
end
|
|
|
|
|
2014-07-22 20:46:19 +08:00
|
|
|
it 'documents API description' do
|
2013-11-25 22:35:04 +08:00
|
|
|
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')
|
2013-11-25 22:35:04 +08:00
|
|
|
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')
|
2013-11-25 22:35:04 +08:00
|
|
|
end
|
|
|
|
|
2014-07-22 20:46:19 +08:00
|
|
|
it 'documents the terms of service url' do
|
2013-11-25 22:35:04 +08:00
|
|
|
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
|
|
|
|
end
|
2013-06-18 21:56:15 +08:00
|
|
|
end
|