Skip to content

Commit b4c95fe

Browse files
blakenumbata
authored andcommitted
Fix host test
1 parent b3eaa88 commit b4c95fe

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

lib/grape-swagger/openapi_3/endpoint.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ def content_types_for(target_class)
2323
# openapi 3.0 related parts
2424
#
2525
# required keys for SwaggerObject
26-
def swagger_object(_target_class, _request, options)
26+
def swagger_object(_target_class, request, options)
27+
url = GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request)
28+
base_path = GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request)
29+
servers = options[:servers] || [{ url: "#{request.scheme}://#{url}#{base_path}" }]
30+
servers = servers.is_a?(Hash) ? [servers] : servers
31+
2732
object = {
2833
info: info_object(options[:info].merge(version: options[:doc_version])),
2934
openapi: '3.0.0',
3035
security: options[:security],
3136
authorizations: options[:authorizations],
32-
servers: options[:servers].is_a?(Hash) ? [options[:servers]] : options[:servers]
37+
servers: servers
3338
}
3439

3540
if options[:security_definitions] || options[:security]

spec/openapi_3/host_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'host in the swagger_doc' do
6+
before :all do
7+
module TheApi
8+
class EmptyApi < Grape::API
9+
format :json
10+
11+
desc 'This gets something.'
12+
get '/something' do
13+
{ bla: 'something' }
14+
end
15+
16+
add_swagger_documentation openapi_version: '3.0'
17+
end
18+
end
19+
end
20+
21+
def app
22+
TheApi::EmptyApi
23+
end
24+
25+
describe 'host should include port' do
26+
subject do
27+
get 'http://example.com:8080/swagger_doc'
28+
JSON.parse(last_response.body)
29+
end
30+
31+
specify do
32+
expect(subject['servers'].first['url']).to eq 'http://example.com:8080'
33+
end
34+
end
35+
36+
describe 'respect X-Forwarded-Host over Host header' do
37+
subject do
38+
header 'Host', 'dummy.example.com'
39+
header 'X-Forwarded-Host', 'real.example.com'
40+
get '/swagger_doc'
41+
JSON.parse(last_response.body)
42+
end
43+
44+
specify do
45+
expect(subject['servers'].first['url']).to eq 'http://real.example.com'
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)