Skip to content

Commit 597bd3f

Browse files
author
Nathan Van der Auwera
committed
Added test for overruling the defaults (and fixed it).
1 parent 7a8508b commit 597bd3f

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

lib/grape-swagger.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def self.setup(options)
4141
@@target_class = options[:target_class]
4242
@@mount_path = options[:mount_path]
4343
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
44-
@@api_version = options[:api_version]
45-
@@base_path = options[:base_path]
44+
api_version = options[:api_version]
45+
base_path = options[:base_path]
4646

4747
desc 'Swagger compatible API description'
4848
get @@mount_path do
@@ -54,9 +54,9 @@ def self.setup(options)
5454
{ :path => "#{@@mount_path}/#{route}.{format}" }
5555
end
5656
{
57-
apiVersion: @@api_version,
57+
apiVersion: api_version,
5858
swaggerVersion: "1.1",
59-
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
59+
basePath: base_path || "http://#{env['HTTP_HOST']}",
6060
operations:[],
6161
apis: routes_array
6262
}
@@ -84,9 +84,9 @@ def self.setup(options)
8484
end
8585

8686
{
87-
apiVersion: @@api_version,
87+
apiVersion: api_version,
8888
swaggerVersion: "1.1",
89-
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
89+
basePath: base_path || "http://#{env['HTTP_HOST']}",
9090
resourcePath: "",
9191
apis: routes_array
9292
}

spec/non_default_api_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'spec_helper'
2+
3+
describe "overruling the defaults for the api documentation generation" do
4+
5+
class MountedApi < Grape::API
6+
desc 'this gets something'
7+
get '/something' do
8+
{:bla => 'something'}
9+
end
10+
end
11+
12+
13+
context "overruling the basepath" do
14+
class SimpleApiWithBasePath < Grape::API
15+
NON_DEFAULT_BASE_PATH= "http://www.breakcoregivesmewood.com"
16+
17+
mount MountedApi
18+
add_swagger_documentation :base_path => NON_DEFAULT_BASE_PATH
19+
end
20+
21+
subject { SimpleApiWithBasePath.new }
22+
def app; subject end
23+
24+
it "retrieves the given base-path on /swagger_doc" do
25+
get '/swagger_doc'
26+
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}\"}]}"
27+
end
28+
29+
it "retrieves the same given base-path for mounted-api" do
30+
Random.stub(:rand) { 0 }
31+
get '/swagger_doc/mountedapi'
32+
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=>[]}]}]}"
33+
end
34+
end
35+
36+
context "overruling the basepath" do
37+
class SimpleApiWithAPiVersion < Grape::API
38+
API_VERSION = "101"
39+
40+
mount MountedApi
41+
add_swagger_documentation :api_version => API_VERSION
42+
end
43+
44+
subject { SimpleApiWithAPiVersion.new }
45+
def app; subject end
46+
47+
it "retrieves the given base-path on /swagger_doc" do
48+
get '/swagger_doc'
49+
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}\"}]}"
50+
end
51+
52+
it "retrieves the same given base-path for mounted-api" do
53+
Random.stub(:rand) { 0 }
54+
get '/swagger_doc/mountedapi'
55+
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=>[]}]}]}"
56+
end
57+
end
58+
59+
context "overruling the mount-path" do
60+
class SimpleApiWithDifferentMount < Grape::API
61+
MOUNT_PATH = "api_doc"
62+
63+
mount MountedApi
64+
add_swagger_documentation :mount_path => MOUNT_PATH
65+
end
66+
67+
subject { SimpleApiWithDifferentMount.new }
68+
def app; subject end
69+
70+
it "retrieves the given base-path on /swagger_doc" do
71+
get '/api_doc'
72+
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}\"}]}"
73+
end
74+
75+
it "retrieves the same given base-path for mounted-api" do
76+
Random.stub(:rand) { 0 }
77+
get '/api_doc/mountedapi'
78+
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=>[]}]}]}"
79+
end
80+
81+
it "does not respond to swagger_doc" do
82+
get '/swagger_doc'
83+
last_response.status.should be == 404
84+
end
85+
end
86+
87+
88+
end

0 commit comments

Comments
 (0)