Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace request.env['HTTP_HOST'] with request.host_with_port #389

Merged
merged 1 commit into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#### Fixes

* [#389](https://github.com/ruby-grape/grape-swagger/pull/389): respect X-Forwarded-Host - [@edvakf](https://github.com/edvakf).

### 0.20.1 / 2016-04-17

#### Features
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def swagger_object(target_class, request, options)
swagger: '2.0',
produces: content_types_for(target_class),
authorizations: options[:authorizations],
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request.env['HTTP_HOST']),
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request.host_with_port),
basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request.env['SCRIPT_NAME']),
tags: GrapeSwagger::DocMethods::TagNameDescription.build(options),
schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes]
Expand Down
43 changes: 43 additions & 0 deletions spec/swagger_v2/host.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

describe 'host in the swagger_doc' do
include_context "the api entities"

before :all do
module TheApi
class EmptyApi < Grape::API
format :json

add_swagger_documentation
end
end
end

def app
TheApi::EmptyApi
end

describe 'host should include port' do
subject do
get 'http://example.com:8080/swagger_doc'
JSON.parse(last_response.body)
end

specify do
expect(subject['host']).to eq 'example.com:8080'
end
end

describe 'respect X-Forwarded-Host over Host header' do
subject do
header 'Host', 'dummy.example.com'
header 'X-Forwarded-Host', 'real.example.com'
get '/swagger_doc'
JSON.parse(last_response.body)
end

specify do
expect(subject['host']).to eq 'real.example.com'
end
end
end