grape-swagger/spec/swagger_v2/default_api_spec.rb

127 lines
3.2 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(
{
"info"=>{"title"=>"API title", "version"=>"v1"},
"swagger"=>"2.0",
"produces"=>["application/json"],
"host"=>"example.org",
2015-12-02 18:23:22 +08:00
"schemes" => ["https", "http"],
2015-10-05 02:05:22 +08:00
"paths"=>{
"/something"=>{
"get"=>{
"produces"=>["application/json"],
"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
desc 'This gets something.' do
detail 'more details about the endpoint'
end
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
2015-09-25 01:08:24 +08:00
expect(subject).to eq({
2015-10-05 02:05:22 +08:00
"info"=>{"title"=>"API title", "version"=>"v1"},
"swagger"=>"2.0",
"produces"=>["application/json"],
"host"=>"example.org",
2015-12-02 18:23:22 +08:00
"schemes" => ["https", "http"],
2015-10-05 02:05:22 +08:00
"paths"=>{
"/something"=>{
"get"=>{
"produces"=>["application/json"],
"responses"=>{"200"=>{"description"=>"This gets something."}}}}}
2015-09-25 01:08:24 +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
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-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
2015-10-05 02:05:22 +08:00
expect(subject['contact']['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