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 to set extra request headers #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions lib/rack_reverse_proxy/roundtrip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def strip_headers
target_request_headers.delete(header)
end
end

def extra_headers
return unless options[:extra_headers]
options[:extra_headers].each do |k,v|
target_request_headers[k.to_s] = v
end
end

def host_header
return uri.host if uri.port == uri.default_port
Expand Down Expand Up @@ -187,6 +194,7 @@ def need_replace_location?
def setup_request
preserve_host
strip_headers
extra_headers
set_forwarded_headers
initialize_http_header
set_basic_auth
Expand Down
24 changes: 24 additions & 0 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,30 @@ def app
end
end
end

context "extra_headers option" do
subject do
stub_request(:any, "http://example.com/test")
get "/test", {}, "HTTP_ACCEPT_ENCODING" => "gzip, deflate""
end

describe "with extra_headers set" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy "/test", "http://example.com/", extra_headers: {"HTTP_FOO_BAR" => "baz"}
end
end

it "adds extra headers to the request headers" do
subject
expect(
a_request(:get, "http://example.com/test").with(
:headers => { "Accept-Encoding" => "gzip, deflate", "Foo-Bar" => "baz" }
)
).to have_been_made
end
end
end

describe "with x_forwarded_headers turned off" do
def app
Expand Down