grape-swagger/spec/swagger_v2/simple_mounted_api_spec.rb

168 lines
5.5 KiB
Ruby
Raw Normal View History

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
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: {
'items[]' => { description: 'array of items' }
}
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: {
'custom' => { type: CustomType, description: 'array of items' }
}
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
it 'retrieves swagger-documentation on /swagger_doc' do
get '/swagger_doc.json'
expect(JSON.parse(last_response.body)).to eq(
{
"info"=>{"title"=>"API title", "version"=>"v1"},
"swagger"=>"2.0",
"produces"=>["application/xml", "application/json", "application/octet-stream", "text/plain"],
"host"=>"example.org",
2015-12-02 18:23:22 +08:00
"schemes" => ["https", "http"],
2015-10-05 02:05:22 +08:00
"paths"=>
{"/simple"=>{"get"=>{"produces"=>["application/json"], "responses"=>{"200"=>{"description"=>"This gets something."}}}},
"/simple-test"=>{"get"=>{"produces"=>["application/json"], "responses"=>{"200"=>{"description"=>"This gets something for URL using - separator."}}}},
2015-10-05 02:05:22 +08:00
"/simple_with_headers"=>
{"get"=>
{"produces"=>["application/json"],
"responses"=>
{"200"=>{"description"=>"this gets something else"},
"403"=>{"description"=>"invalid pony"},
"405"=>{"description"=>"no ponies left!"}},
2015-12-02 18:23:22 +08:00
"headers"=>{
'XAuthToken' => {"description"=>'A required header.', "required"=>true },
'XOtherHeader' => {"description"=>'An optional header.', "required"=>false }
}}},
"/items"=>{"post"=>{"produces"=>["application/json"], "responses"=>{"201"=>{"description"=>"this takes an array of parameters"}}}},
"/custom"=>{"get"=>{"produces"=>["application/json"], "responses"=>{"200"=>{"description"=>"this uses a custom parameter"}}}}}}
2015-10-05 02:05:22 +08:00
)
end
it 'retrieves the documentation for mounted-api' do
get '/swagger_doc/simple.json'
expect(JSON.parse(last_response.body)).to eq({
"info"=>{"title"=>"API title", "version"=>"v1"},
"swagger"=>"2.0",
"produces"=>["application/xml", "application/json", "application/octet-stream", "text/plain"],
"host"=>"example.org",
2015-12-02 18:23:22 +08:00
"schemes" => ["https", "http"],
"paths"=>{"/simple"=>{"get"=>{"produces"=>["application/json"], "responses"=>{"200"=>{"description"=>"This gets something."}}}}}})
2015-10-05 02:05:22 +08:00
end
context 'retrieves the documentation for mounted-api that' do
it "contains '-' in URL" do
get '/swagger_doc/simple-test.json'
expect(JSON.parse(last_response.body)).to eq({
"info"=>{"title"=>"API title", "version"=>"v1"},
"swagger"=>"2.0",
"produces"=>["application/xml", "application/json", "application/octet-stream", "text/plain"],
"host"=>"example.org",
2015-12-02 18:23:22 +08:00
"schemes" => ["https", "http"],
2015-10-05 02:05:22 +08:00
"paths"=>{
"/simple-test"=>{"get"=>{
"produces"=>["application/json"],
"responses"=>{
"200"=>{"description"=>"This gets something for URL using - separator."}}}}}}
2015-10-05 02:05:22 +08:00
)
end
it 'includes headers' do
get '/swagger_doc/simple_with_headers.json'
expect(JSON.parse(last_response.body)['paths']).to eq(
{"/simple_with_headers"=>
{"get"=>
{"produces"=>["application/json"],
2015-12-02 18:23:22 +08:00
"headers"=>{
'XAuthToken' => {"description"=>'A required header.', "required"=>true },
'XOtherHeader' => {"description"=>'An optional header.', "required"=>false }},
2015-10-05 02:05:22 +08:00
"responses"=>
{"200"=>{"description"=>"this gets something else"},
"403"=>{"description"=>"invalid pony"},
"405"=>{"description"=>"no ponies left!"}}}}}
2015-10-05 02:05:22 +08:00
)
end
it 'supports multiple parameters' do
get '/swagger_doc/items.json'
expect(JSON.parse(last_response.body)['paths']).to eq(
{
"/items"=>{
"post"=>{
"produces"=>["application/json"],
"responses"=>{
"201"=>{
"description"=>"this takes an array of parameters"}}}}}
2015-10-05 02:05:22 +08:00
)
end
it 'supports custom types' do
get '/swagger_doc/custom.json'
expect(JSON.parse(last_response.body)['paths']).to eq(
{
"/custom"=>{
"get"=>{
"produces"=>["application/json"],
"responses"=>{
"200"=>{
"description"=>"this uses a custom parameter"}}}}}
2015-10-05 02:05:22 +08:00
)
end
end
end