-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow multiple base URLs to be configured
Closes: #392
- Loading branch information
Showing
7 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,52 @@ | ||
module Rack | ||
module PactBroker | ||
class SetBaseUrl | ||
def initialize app, base_url | ||
X_FORWARDED_PATTERN = /_X_FORWARDED_/.freeze | ||
|
||
def initialize app, base_urls | ||
@app = app | ||
@base_url = base_url | ||
@base_urls = base_urls | ||
end | ||
|
||
def call env | ||
def call(env) | ||
if env["pactbroker.base_url"] | ||
app.call(env) | ||
else | ||
app.call(env.merge("pactbroker.base_url" => base_url)) | ||
app.call(env.merge("pactbroker.base_url" => select_matching_base_url(env))) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :app, :base_url | ||
attr_reader :app, :base_urls | ||
|
||
def select_matching_base_url(env) | ||
if base_urls.size > 1 | ||
return matching_base_url_considering_x_forwarded_headers(env) || | ||
matching_base_url_not_considering_x_forwarded_headers(env) || | ||
default_base_url | ||
end | ||
default_base_url | ||
end | ||
|
||
def default_base_url | ||
base_urls.first | ||
end | ||
|
||
def matching_base_url_considering_x_forwarded_headers(env) | ||
matching_base_url(env) | ||
end | ||
|
||
def matching_base_url_not_considering_x_forwarded_headers(env) | ||
matching_base_url(env.reject{ |k, _| k =~ X_FORWARDED_PATTERN} ) | ||
end | ||
|
||
def matching_base_url(env) | ||
request_base_url = Rack::Request.new(env).base_url | ||
if base_urls.include?(request_base_url) | ||
request_base_url | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'rack/pact_broker/set_base_url' | ||
|
||
module Rack | ||
module PactBroker | ||
describe SetBaseUrl do | ||
let(:base_urls) { ["http://pact-broker"] } | ||
let(:rack_env) { {} } | ||
let(:target_app) { double('app', call: [200, {}, []]) } | ||
let(:app) { SetBaseUrl.new(target_app, base_urls) } | ||
|
||
subject { get("/", {}, rack_env) } | ||
|
||
describe "#call" do | ||
context "when the base_url is already set" do | ||
let(:rack_env) { { "pactbroker.base_url" => "http://foo"} } | ||
|
||
it "does not overwrite it" do | ||
expect(target_app).to receive(:call).with(hash_including("pactbroker.base_url" => "http://foo")) | ||
subject | ||
end | ||
end | ||
|
||
context "when there is one base URL" do | ||
it "sets that base_url" do | ||
expect(target_app).to receive(:call).with(hash_including("pactbroker.base_url" => "http://pact-broker")) | ||
subject | ||
end | ||
end | ||
|
||
context "when there are no base URLs" do | ||
let(:base_urls) { [] } | ||
|
||
it "sets the base URL to nil" do | ||
expect(target_app).to receive(:call).with(hash_including("pactbroker.base_url" => nil)) | ||
subject | ||
end | ||
end | ||
|
||
context "when there are multiple base URLs" do | ||
let(:base_urls) { ["https://foo", "https://pact-broker-external", "http://pact-broker-internal"] } | ||
|
||
let(:host) { "pact-broker-internal" } | ||
let(:scheme) { "http" } | ||
let(:forwarded_host) { "pact-broker-external" } | ||
let(:forwarded_scheme) { "https" } | ||
let(:rack_env) do | ||
{ | ||
Rack::HTTP_HOST => host, | ||
Rack::RACK_URL_SCHEME => scheme, | ||
"HTTP_X_FORWARDED_HOST" => forwarded_host, | ||
"HTTP_X_FORWARDED_SCHEME" => forwarded_scheme | ||
} | ||
end | ||
|
||
context "when the base URL created taking any X-Forwarded headers into account matches one of the base URLs" do | ||
it "uses that base URL" do | ||
expect(target_app).to receive(:call).with(hash_including("pactbroker.base_url" => "https://pact-broker-external")) | ||
subject | ||
end | ||
end | ||
|
||
context "when the base URL created NOT taking the X-Forwarded headers into account matches one of the base URLs (potential cache poisoning)" do | ||
let(:forwarded_host) { "pact-broker-external-wrong" } | ||
|
||
it "uses that base URL" do | ||
expect(target_app).to receive(:call).with(hash_including("pactbroker.base_url" => "http://pact-broker-internal")) | ||
subject | ||
end | ||
end | ||
|
||
context "when neither base URL matches the base URLs (potential cache poisoning)" do | ||
before do | ||
rack_env["HTTP_HOST"] = "silly-buggers-1" | ||
rack_env["HTTP_X_FORWARDED_HOST"] = "silly-buggers-1" | ||
end | ||
|
||
it "uses the first base URL" do | ||
expect(target_app).to receive(:call).with(hash_including("pactbroker.base_url" => "https://foo")) | ||
subject | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |