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

Allow Host header setting #570

Merged
merged 2 commits into from
Nov 4, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add tooling for measuring test coverage so that changes are safer (https://github.com/rswag/rswag/pull/551)
- Add CSP compatible with rswag in case the Rails one is not compatible (https://github.com/rswag/rswag/pull/263)
- Add ADDITIONAL_RSPEC_OPTS env variable (https://github.com/rswag/rswag/pull/556)
- Add option to set Host header (https://github.com/rswag/rswag/pull/184)

### Changed

Expand Down
16 changes: 12 additions & 4 deletions rswag-specs/lib/rswag/specs/request_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,22 @@ def add_headers(request, metadata, swagger_doc, parameters, example)
tuples << ['Content-Type', content_type]
end

# Host header
host = metadata[:operation][:host] || swagger_doc[:host]
if host.present?
host = example.respond_to?(:'Host') ? example.send(:'Host') : host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtannas I was wondering if you might have meant: example.respond_to?(:'host') ? example.send(:'host') here? (lower case h.) My tests broke after this change, where RSpec started trying to use my live production API host instead of the test host. example.host returns the correct test value for me, but I don't have a example.Host method.

tuples << ['Host', host]
end

# Rails test infrastructure requires rack-formatted headers
rack_formatted_tuples = tuples.map do |pair|
[
case pair[0]
when 'Accept' then 'HTTP_ACCEPT'
when 'Content-Type' then 'CONTENT_TYPE'
when 'Authorization' then 'HTTP_AUTHORIZATION'
else pair[0]
when 'Accept' then 'HTTP_ACCEPT'
when 'Content-Type' then 'CONTENT_TYPE'
when 'Authorization' then 'HTTP_AUTHORIZATION'
when 'Host' then 'HTTP_HOST'
else pair[0]
end,
pair[1]
]
Expand Down
22 changes: 22 additions & 0 deletions rswag-specs/spec/rswag/specs/request_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ module Specs
end
end

context 'host header' do
context "explicit 'Host' value provided" do
before do
metadata[:operation][:host] = 'swagger.io'
end

it "sets 'Host' header" do
expect(request[:headers]).to eq('HTTP_HOST' => 'swagger.io')
end
end

context "no 'Host' value provided" do
before do
metadata[:operation][:host] = nil
end

it "does not set 'Host' header" do
expect(request[:headers]).to eq({})
end
end
end

context 'basic auth' do
context 'swagger 2.0' do
before do
Expand Down