Skip to content

Commit

Permalink
chore: allow base URL to be set on a per request level in rack middle…
Browse files Browse the repository at this point in the history
…ware

For PF.
  • Loading branch information
bethesque committed Jan 12, 2021
1 parent b57aad3 commit 2c958af
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/pact_broker/api/resources/default_base_resource.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'webmachine'
require 'pact_broker/services'
require 'pact_broker/api/decorators'
Expand Down Expand Up @@ -67,16 +68,16 @@ def identifier_from_path
alias_method :path_info, :identifier_from_path

def base_url
PactBroker.configuration.base_url || request.base_uri.to_s.chomp('/')
request.env["pactbroker.base_url"] || PactBroker.configuration.base_url || request.base_uri.to_s.chomp('/')
end

# See comments for base_url in lib/pact_broker/doc/controllers/app.rb
def ui_base_url
PactBroker.configuration.base_url || ''
request.env["pactbroker.base_url"] || PactBroker.configuration.base_url || ''
end

def charsets_provided
[['utf-8', :encode]]
[["utf-8", :encode]]
end

# We only use utf-8 so leave encoding as it is
Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/doc/controllers/app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'padrino-core'
require 'redcarpet'

Expand Down Expand Up @@ -50,9 +51,9 @@ def base_url
# https://www.acunetix.com/blog/articles/automated-detection-of-host-header-attacks/
# Either use the explicitly configured base url or an empty string,
# rather than request.base_url, which uses the X-Forwarded headers.
PactBroker.configuration.base_url || ''
env["pactbroker.base_url"] || PactBroker.configuration.base_url || ''
end
end
end
end
end
end
20 changes: 17 additions & 3 deletions spec/lib/pact_broker/api/resources/default_base_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module PactBroker
module Api
module Resources
describe DefaultBaseResource do
before do
allow(env).to receive(:[]).with("pactbroker.base_url").and_return(nil)
end
let(:request) { double('request', body: body, uri: uri, base_uri: URI("http://example.org/"), env: env, path_info: path_info).as_null_object }
let(:path_info) { { application_context: application_context, key1: "foo%20bar", key2: :value2, key3: 1.2 }}
let(:application_context) { PactBroker::ApplicationContext.default_application_context }
Expand Down Expand Up @@ -85,18 +88,29 @@ module Resources
end

describe "base_url" do
before do
allow(env).to receive(:[]).with("pactbroker.base_url").and_return("http://rack")
allow(PactBroker.configuration).to receive(:base_url).and_return("http://foo")
end

context "when pactbroker.base_url is set on the env" do
it "uses that" do
expect(subject.base_url).to eq "http://rack"
end
end

context "when PactBroker.configuration.base_url is not nil" do
before do
allow(PactBroker.configuration).to receive(:base_url).and_return("http://foo")
allow(env).to receive(:[]).with("pactbroker.base_url").and_return(nil)
end

it "returns the configured base URL" do
expect(subject.base_url).to eq "http://foo"
end
end

context "when PactBroker.configuration.base_url is nil" do
context "when PactBroker.configuration.base_url is nil and the rack env value is not set" do
before do
allow(env).to receive(:[]).with("pactbroker.base_url").and_return(nil)
allow(PactBroker.configuration).to receive(:base_url).and_return(nil)
end

Expand Down

0 comments on commit 2c958af

Please sign in to comment.