2015-10-05 02:05:22 +08:00
require 'spec_helper'
describe 'a simple mounted api' do
before :all do
class CustomType ; end
class SimpleMountedApi < Grape :: API
desc 'Document root'
get do
end
desc 'This gets something.' ,
2015-12-02 18:23:22 +08:00
notes : '_test_'
2015-10-05 02:05:22 +08:00
get '/simple' do
{ bla : 'something' }
end
desc 'This gets something for URL using - separator.' ,
2015-12-02 18:23:22 +08:00
notes : '_test_'
2015-10-05 02:05:22 +08:00
get '/simple-test' do
{ bla : 'something' }
end
2016-05-07 00:59:45 +08:00
head '/simple-head-test' do
status 200
end
2015-10-05 02:05:22 +08:00
desc 'this gets something else' ,
2015-12-02 18:23:22 +08:00
headers : {
'XAuthToken' = > { description : 'A required header.' , required : true } ,
'XOtherHeader' = > { description : 'An optional header.' , required : false }
} ,
http_codes : [
{ code : 403 , message : 'invalid pony' } ,
{ code : 405 , message : 'no ponies left!' }
]
2015-10-05 02:05:22 +08:00
get '/simple_with_headers' do
{ bla : 'something_else' }
end
desc 'this takes an array of parameters' ,
2015-12-02 18:23:22 +08:00
params : {
2015-12-19 01:40:53 +08:00
'items[]' = > { description : 'array of items' , is_array : true }
2015-12-02 18:23:22 +08:00
}
2015-10-05 02:05:22 +08:00
post '/items' do
{ }
end
desc 'this uses a custom parameter' ,
2015-12-02 18:23:22 +08:00
params : {
2015-12-19 01:40:53 +08:00
'custom' = > { type : CustomType , description : 'array of items' , is_array : true }
2015-12-02 18:23:22 +08:00
}
2015-10-05 02:05:22 +08:00
get '/custom' do
{ }
end
end
class SimpleApi < Grape :: API
mount SimpleMountedApi
add_swagger_documentation
end
end
def app
SimpleApi
end
2015-12-19 01:40:53 +08:00
describe " retrieves swagger-documentation on /swagger_doc " do
subject do
get '/swagger_doc.json'
JSON . parse ( last_response . body )
end
specify do
expect ( subject ) . to eq ( {
2016-04-30 17:27:05 +08:00
" info " = > { " title " = > " API title " , " version " = > " 0.0.1 " } ,
2015-10-05 02:05:22 +08:00
" swagger " = > " 2.0 " ,
" produces " = > [ " application/xml " , " application/json " , " application/octet-stream " , " text/plain " ] ,
" host " = > " example.org " ,
2016-05-07 00:59:45 +08:00
" tags " = > [ { " name " = > " simple " , " description " = > " Operations about simples " } , { " name " = > " simple-test " , " description " = > " Operations about simple-tests " } , { " name " = > " simple-head-test " , " description " = > " Operations about simple-head-tests " } , { " name " = > " simple_with_headers " , " description " = > " Operations about simple_with_headers " } , { " name " = > " items " , " description " = > " Operations about items " } , { " name " = > " custom " , " description " = > " Operations about customs " } ] ,
2015-12-19 01:40:53 +08:00
" paths " = > {
2016-03-16 00:53:03 +08:00
" /simple " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " This gets something. " ,
2016-03-16 00:53:03 +08:00
" produces " = > [ " application/json " ] ,
" tags " = > [ " simple " ] ,
" operationId " = > " getSimple " ,
" responses " = > { " 200 " = > { " description " = > " This gets something. " } } } } ,
" /simple-test " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " This gets something for URL using - separator. " ,
2016-03-16 00:53:03 +08:00
" produces " = > [ " application/json " ] ,
" tags " = > [ " simple-test " ] ,
" operationId " = > " getSimpleTest " ,
" responses " = > { " 200 " = > { " description " = > " This gets something for URL using - separator. " } } } } ,
2016-05-07 00:59:45 +08:00
" /simple-head-test " = > {
" head " = > {
" produces " = > [ " application/json " ] ,
" responses " = > { " 200 " = > { " description " = > " head SimpleHeadTest " } } ,
" tags " = > [ " simple-head-test " ] ,
" operationId " = > " headSimpleHeadTest " } } ,
2015-12-19 01:40:53 +08:00
" /simple_with_headers " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " this gets something else " ,
2015-12-19 01:40:53 +08:00
" produces " = > [ " application/json " ] ,
2016-04-22 08:22:46 +08:00
" parameters " = > [
{ " in " = > " header " , " name " = > " XAuthToken " , " description " = > " A required header. " , " type " = > " string " , " required " = > true } ,
{ " in " = > " header " , " name " = > " XOtherHeader " , " description " = > " An optional header. " , " type " = > " string " , " required " = > false } ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " simple_with_headers " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " getSimpleWithHeaders " ,
2015-12-19 01:40:53 +08:00
" responses " = > {
" 200 " = > { " description " = > " this gets something else " } ,
" 403 " = > { " description " = > " invalid pony " } ,
" 405 " = > { " description " = > " no ponies left! " } }
} } ,
" /items " = > {
" post " = > {
2016-04-08 09:05:35 +08:00
" description " = > " this takes an array of parameters " ,
2015-12-19 01:40:53 +08:00
" produces " = > [ " application/json " ] ,
2016-03-16 08:18:07 +08:00
" consumes " = > [ " application/json " ] ,
2016-03-30 06:43:33 +08:00
" parameters " = > [ { " in " = > " formData " , " name " = > " items[] " , " description " = > " array of items " , " required " = > false , " type " = > " array " , " items " = > { " type " = > " string " } } ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " items " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " postItems " ,
2016-05-01 18:31:44 +08:00
" responses " = > { " 201 " = > { " description " = > " this takes an array of parameters " } }
2015-12-19 01:40:53 +08:00
} } ,
" /custom " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " this uses a custom parameter " ,
2015-12-19 01:40:53 +08:00
" produces " = > [ " application/json " ] ,
2016-03-30 06:43:33 +08:00
" parameters " = > [ { " in " = > " formData " , " name " = > " custom " , " description " = > " array of items " , " required " = > false , " type " = > " array " , " items " = > { " type " = > " CustomType " } } ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " custom " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " getCustom " ,
2016-05-01 18:31:44 +08:00
" responses " = > { " 200 " = > { " description " = > " this uses a custom parameter " } } }
} } } )
2015-12-19 01:40:53 +08:00
end
2015-10-05 02:05:22 +08:00
end
2015-12-19 01:40:53 +08:00
describe 'retrieves the documentation for mounted-api' do
subject do
get '/swagger_doc/simple.json'
JSON . parse ( last_response . body )
end
2015-10-05 02:05:22 +08:00
2015-12-19 01:40:53 +08:00
specify do
expect ( subject ) . to eq ( {
2016-04-30 17:27:05 +08:00
" info " = > { " title " = > " API title " , " version " = > " 0.0.1 " } ,
2015-10-05 02:05:22 +08:00
" swagger " = > " 2.0 " ,
" produces " = > [ " application/xml " , " application/json " , " application/octet-stream " , " text/plain " ] ,
" host " = > " example.org " ,
2016-05-07 00:59:45 +08:00
" tags " = > [ { " name " = > " simple " , " description " = > " Operations about simples " } , { " name " = > " simple-test " , " description " = > " Operations about simple-tests " } , { " name " = > " simple-head-test " , " description " = > " Operations about simple-head-tests " } , { " name " = > " simple_with_headers " , " description " = > " Operations about simple_with_headers " } , { " name " = > " items " , " description " = > " Operations about items " } , { " name " = > " custom " , " description " = > " Operations about customs " } ] ,
2015-10-05 02:05:22 +08:00
" paths " = > {
2015-12-19 01:40:53 +08:00
" /simple " = > {
2016-03-16 00:53:03 +08:00
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " This gets something. " ,
2016-03-16 00:53:03 +08:00
" produces " = > [ " application/json " ] ,
" tags " = > [ " simple " ] ,
" operationId " = > " getSimple " ,
" responses " = > { " 200 " = > { " description " = > " This gets something. " } } } }
2015-12-19 01:40:53 +08:00
} } )
2015-10-05 02:05:22 +08:00
end
2015-12-19 01:40:53 +08:00
end
describe 'retrieves the documentation for mounted-api that' do
describe " contains '-' in URL " do
subject do
get '/swagger_doc/simple-test.json'
JSON . parse ( last_response . body )
end
2015-10-05 02:05:22 +08:00
2015-12-19 01:40:53 +08:00
specify do
expect ( subject ) . to eq ( {
2016-04-30 17:27:05 +08:00
" info " = > { " title " = > " API title " , " version " = > " 0.0.1 " } ,
2015-12-19 01:40:53 +08:00
" swagger " = > " 2.0 " ,
" produces " = > [ " application/xml " , " application/json " , " application/octet-stream " , " text/plain " ] ,
" host " = > " example.org " ,
2016-05-07 00:59:45 +08:00
" tags " = > [ { " name " = > " simple " , " description " = > " Operations about simples " } , { " name " = > " simple-test " , " description " = > " Operations about simple-tests " } , { " name " = > " simple-head-test " , " description " = > " Operations about simple-head-tests " } , { " name " = > " simple_with_headers " , " description " = > " Operations about simple_with_headers " } , { " name " = > " items " , " description " = > " Operations about items " } , { " name " = > " custom " , " description " = > " Operations about customs " } ] ,
2015-12-19 01:40:53 +08:00
" paths " = > {
" /simple-test " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " This gets something for URL using - separator. " ,
2015-12-19 01:40:53 +08:00
" produces " = > [ " application/json " ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " simple-test " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " getSimpleTest " ,
2015-12-19 01:40:53 +08:00
" responses " = > { " 200 " = > { " description " = > " This gets something for URL using - separator. " } } } }
} } )
end
end
describe 'includes headers' do
subject do
get '/swagger_doc/simple_with_headers.json'
JSON . parse ( last_response . body )
end
specify do
expect ( subject [ 'paths' ] ) . to eq ( {
" /simple_with_headers " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " this gets something else " ,
2015-12-19 01:40:53 +08:00
" produces " = > [ " application/json " ] ,
2016-04-22 08:22:46 +08:00
" parameters " = > [
{ " in " = > " header " , " name " = > " XAuthToken " , " description " = > " A required header. " , " type " = > " string " , " required " = > true } ,
{ " in " = > " header " , " name " = > " XOtherHeader " , " description " = > " An optional header. " , " type " = > " string " , " required " = > false } ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " simple_with_headers " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " getSimpleWithHeaders " ,
2015-12-19 01:40:53 +08:00
" responses " = > {
" 200 " = > { " description " = > " this gets something else " } ,
" 403 " = > { " description " = > " invalid pony " } ,
" 405 " = > { " description " = > " no ponies left! " } } }
} } )
end
2015-10-05 02:05:22 +08:00
end
2015-12-19 01:40:53 +08:00
describe 'supports array params' do
subject do
get '/swagger_doc/items.json'
JSON . parse ( last_response . body )
end
specify do
expect ( subject [ 'paths' ] ) . to eq ( {
2015-10-05 02:05:22 +08:00
" /items " = > {
" post " = > {
2016-04-08 09:05:35 +08:00
" description " = > " this takes an array of parameters " ,
2015-10-05 02:05:22 +08:00
" produces " = > [ " application/json " ] ,
2016-03-16 08:18:07 +08:00
" consumes " = > [ " application/json " ] ,
2016-03-30 06:43:33 +08:00
" parameters " = > [ { " in " = > " formData " , " name " = > " items[] " , " description " = > " array of items " , " required " = > false , " type " = > " array " , " items " = > { " type " = > " string " } } ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " items " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " postItems " ,
2016-05-01 18:31:44 +08:00
" responses " = > { " 201 " = > { " description " = > " this takes an array of parameters " } } }
2015-12-19 01:40:53 +08:00
} } )
end
2015-10-05 02:05:22 +08:00
end
2015-12-19 01:40:53 +08:00
describe 'supports custom params types' do
subject do
get '/swagger_doc/custom.json'
JSON . parse ( last_response . body )
end
specify do
expect ( subject [ 'paths' ] ) . to eq ( {
2015-10-05 02:05:22 +08:00
" /custom " = > {
" get " = > {
2016-04-08 09:05:35 +08:00
" description " = > " this uses a custom parameter " ,
2015-10-05 02:05:22 +08:00
" produces " = > [ " application/json " ] ,
2016-03-30 06:43:33 +08:00
" parameters " = > [ { " in " = > " formData " , " name " = > " custom " , " description " = > " array of items " , " required " = > false , " type " = > " array " , " items " = > { " type " = > " CustomType " } } ] ,
2016-03-09 23:37:03 +08:00
" tags " = > [ " custom " ] ,
2016-03-16 00:53:03 +08:00
" operationId " = > " getCustom " ,
2016-05-01 18:31:44 +08:00
" responses " = > { " 200 " = > { " description " = > " this uses a custom parameter " } } }
2015-12-19 01:40:53 +08:00
} } )
end
2015-10-05 02:05:22 +08:00
end
end
end