Skip to content

Commit 857a1e8

Browse files
Merge pull request #21 from cutalion/https_base_path
Use Rack::Request base_url (to make proper urls for https)
2 parents 23454cf + fdfab14 commit 857a1e8

File tree

3 files changed

+90
-64
lines changed

3 files changed

+90
-64
lines changed

lib/grape-swagger.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def self.setup(options)
7070
{
7171
apiVersion: api_version,
7272
swaggerVersion: "1.1",
73-
basePath: base_path || "http://#{env['HTTP_HOST']}",
73+
basePath: base_path || request.base_url,
7474
operations:[],
7575
apis: routes_array
7676
}
@@ -102,7 +102,7 @@ def self.setup(options)
102102
{
103103
apiVersion: api_version,
104104
swaggerVersion: "1.1",
105-
basePath: base_path || "http://#{env['HTTP_HOST']}",
105+
basePath: base_path || request.base_url,
106106
resourcePath: "",
107107
apis: routes_array
108108
}

spec/grape-swagger-helper_spec.rb

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,87 @@
22

33
describe "helpers" do
44

5-
before(:all) do
6-
class HelperTestAPI < Grape::API
7-
add_swagger_documentation
8-
end
5+
before(:all) do
6+
class HelperTestAPI < Grape::API
7+
add_swagger_documentation
8+
end
99

10-
@api = Object.new
11-
# after injecting grape-swagger into the Test API we get the helper methods
12-
# back from the first endpoint's class (the API mounted by grape-swagger
13-
# to serve the swagger_doc
14-
@api.extend HelperTestAPI.endpoints.first.options[:app].helpers
10+
@api = Object.new
11+
# after injecting grape-swagger into the Test API we get the helper methods
12+
# back from the first endpoint's class (the API mounted by grape-swagger
13+
# to serve the swagger_doc
14+
@api.extend HelperTestAPI.endpoints.first.options[:app].helpers
1515

16-
end
16+
end
1717

18-
context "parsing parameters" do
19-
it "should parse params as query strings for a GET" do
20-
params = {
21-
name: {type: 'String', :desc => "A name", required: true },
22-
level: 'max'
23-
}
24-
path = "/coolness"
25-
method = "GET"
26-
@api.parse_params(params, path, method).should ==
27-
[
28-
{paramType: "query", name: :name, description:"A name", dataType: "String", required: true},
29-
{paramType: "query", name: :level, description:"", dataType: "String", required: false}
30-
]
31-
end
18+
context "parsing parameters" do
19+
it "should parse params as query strings for a GET" do
20+
params = {
21+
name: {type: 'String', :desc => "A name", required: true },
22+
level: 'max'
23+
}
24+
path = "/coolness"
25+
method = "GET"
26+
@api.parse_params(params, path, method).should ==
27+
[
28+
{paramType: "query", name: :name, description:"A name", dataType: "String", required: true},
29+
{paramType: "query", name: :level, description:"", dataType: "String", required: false}
30+
]
31+
end
3232

33-
it "should parse params as body for a POST" do
34-
params = {
35-
name: {type: 'String', :desc =>"A name", required: true },
36-
level: 'max'
37-
}
38-
path = "/coolness"
39-
method = "POST"
40-
@api.parse_params(params, path, method).should ==
41-
[
42-
{paramType: "body", name: :name, description:"A name", dataType: "String", required: true},
43-
{paramType: "body", name: :level, description:"", dataType: "String", required: false}
44-
]
45-
end
46-
end
33+
it "should parse params as body for a POST" do
34+
params = {
35+
name: {type: 'String', :desc =>"A name", required: true },
36+
level: 'max'
37+
}
38+
path = "/coolness"
39+
method = "POST"
40+
@api.parse_params(params, path, method).should ==
41+
[
42+
{paramType: "body", name: :name, description:"A name", dataType: "String", required: true},
43+
{paramType: "body", name: :level, description:"", dataType: "String", required: false}
44+
]
45+
end
46+
end
4747

48-
context "parsing the path" do
49-
it "should parse the path" do
50-
path = ":abc/def(.:format)"
51-
@api.parse_path(path, nil).should == "{abc}/def.{format}"
52-
end
48+
context "parsing the path" do
49+
it "should parse the path" do
50+
path = ":abc/def(.:format)"
51+
@api.parse_path(path, nil).should == "{abc}/def.{format}"
52+
end
5353

5454
it "should parse a path that has vars with underscores in the name" do
5555
path = "abc/:def_g(.:format)"
56-
@api.parse_path(path, nil).should == "abc/{def_g}.{format}"
57-
56+
@api.parse_path(path, nil).should == "abc/{def_g}.{format}"
57+
5858
end
5959

6060
it "should parse a path that has vars with numbers in the name" do
6161
path = "abc/:sha1(.:format)"
62-
@api.parse_path(path, nil).should == "abc/{sha1}.{format}"
62+
@api.parse_path(path, nil).should == "abc/{sha1}.{format}"
6363
end
6464

6565
it "should parse a path that has multiple variables" do
6666
path1 = "abc/:def/:geh(.:format)"
6767
path2 = "abc/:def:geh(.:format)"
68-
@api.parse_path(path1, nil).should == "abc/{def}/{geh}.{format}"
69-
@api.parse_path(path2, nil).should == "abc/{def}{geh}.{format}"
68+
@api.parse_path(path1, nil).should == "abc/{def}/{geh}.{format}"
69+
@api.parse_path(path2, nil).should == "abc/{def}{geh}.{format}"
7070
end
7171

72-
it "should parse the path with a specified version" do
73-
path = ":abc/{version}/def(.:format)"
74-
@api.parse_path(path, 'v1').should == "{abc}/v1/def.{format}"
75-
end
76-
end
72+
it "should parse the path with a specified version" do
73+
path = ":abc/{version}/def(.:format)"
74+
@api.parse_path(path, 'v1').should == "{abc}/v1/def.{format}"
75+
end
76+
end
7777

78-
context "parsing header parameters" do
79-
it "should parse params for the header" do
80-
params = {"XAuthToken" => { description: "A required header.", required: true}}
81-
@api.parse_header_params(params).should ==
82-
[
83-
{paramType: "header", name: "XAuthToken", description:"A required header.", dataType: "String", required: true}
84-
]
85-
end
86-
end
78+
context "parsing header parameters" do
79+
it "should parse params for the header" do
80+
params = {"XAuthToken" => { description: "A required header.", required: true}}
81+
@api.parse_header_params(params).should ==
82+
[
83+
{paramType: "header", name: "XAuthToken", description:"A required header.", dataType: "String", required: true}
84+
]
85+
end
86+
end
8787

8888
end

spec/non_default_api_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,31 @@ def app; SimpleApiWithVersion end
202202
end
203203
end
204204

205+
context "protected API" do
206+
before(:all) do
207+
class ProtectedApi < Grape::API
208+
desc 'this gets something'
209+
get '/something' do
210+
{:bla => 'something'}
211+
end
212+
end
213+
214+
class SimpleApiWithProtection < Grape::API
215+
mount ProtectedApi
216+
add_swagger_documentation
217+
end
218+
end
219+
220+
def app; SimpleApiWithProtection; end
221+
222+
it "should use https schema in mount point" do
223+
get '/swagger_doc', {}, 'rack.url_scheme' => 'https'
224+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"https://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/something.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
225+
end
205226

227+
it "should use https schema in endpoint doc" do
228+
get '/swagger_doc/something', {}, 'rack.url_scheme' => 'https'
229+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"https://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>\"GET-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
230+
end
231+
end
206232
end

0 commit comments

Comments
 (0)