Added test for overruling the defaults (and fixed it).

This commit is contained in:
Nathan Van der Auwera 2012-07-25 16:47:36 +02:00
parent 7a8508bb12
commit 597bd3f840
2 changed files with 94 additions and 6 deletions

View File

@ -41,8 +41,8 @@ module Grape
@@target_class = options[:target_class]
@@mount_path = options[:mount_path]
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
@@api_version = options[:api_version]
@@base_path = options[:base_path]
api_version = options[:api_version]
base_path = options[:base_path]
desc 'Swagger compatible API description'
get @@mount_path do
@ -54,9 +54,9 @@ module Grape
{ :path => "#{@@mount_path}/#{route}.{format}" }
end
{
apiVersion: @@api_version,
apiVersion: api_version,
swaggerVersion: "1.1",
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
basePath: base_path || "http://#{env['HTTP_HOST']}",
operations:[],
apis: routes_array
}
@ -84,9 +84,9 @@ module Grape
end
{
apiVersion: @@api_version,
apiVersion: api_version,
swaggerVersion: "1.1",
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
basePath: base_path || "http://#{env['HTTP_HOST']}",
resourcePath: "",
apis: routes_array
}

View File

@ -0,0 +1,88 @@
require 'spec_helper'
describe "overruling the defaults for the api documentation generation" do
class MountedApi < Grape::API
desc 'this gets something'
get '/something' do
{:bla => 'something'}
end
end
context "overruling the basepath" do
class SimpleApiWithBasePath < Grape::API
NON_DEFAULT_BASE_PATH= "http://www.breakcoregivesmewood.com"
mount MountedApi
add_swagger_documentation :base_path => NON_DEFAULT_BASE_PATH
end
subject { SimpleApiWithBasePath.new }
def app; subject end
it "retrieves the given base-path on /swagger_doc" do
get '/swagger_doc'
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"#{SimpleApiWithBasePath::NON_DEFAULT_BASE_PATH}\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
end
it "retrieves the same given base-path for mounted-api" do
Random.stub(:rand) { 0 }
get '/swagger_doc/mountedapi'
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"#{SimpleApiWithBasePath::NON_DEFAULT_BASE_PATH}\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
end
end
context "overruling the basepath" do
class SimpleApiWithAPiVersion < Grape::API
API_VERSION = "101"
mount MountedApi
add_swagger_documentation :api_version => API_VERSION
end
subject { SimpleApiWithAPiVersion.new }
def app; subject end
it "retrieves the given base-path on /swagger_doc" do
get '/swagger_doc'
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithAPiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
end
it "retrieves the same given base-path for mounted-api" do
Random.stub(:rand) { 0 }
get '/swagger_doc/mountedapi'
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithAPiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
end
end
context "overruling the mount-path" do
class SimpleApiWithDifferentMount < Grape::API
MOUNT_PATH = "api_doc"
mount MountedApi
add_swagger_documentation :mount_path => MOUNT_PATH
end
subject { SimpleApiWithDifferentMount.new }
def app; subject end
it "retrieves the given base-path on /swagger_doc" do
get '/api_doc'
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
end
it "retrieves the same given base-path for mounted-api" do
Random.stub(:rand) { 0 }
get '/api_doc/mountedapi'
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
end
it "does not respond to swagger_doc" do
get '/swagger_doc'
last_response.status.should be == 404
end
end
end