diff --git a/lib/pact_broker/api/resources/default_base_resource.rb b/lib/pact_broker/api/resources/default_base_resource.rb index a9c562959..93625d0f5 100644 --- a/lib/pact_broker/api/resources/default_base_resource.rb +++ b/lib/pact_broker/api/resources/default_base_resource.rb @@ -68,12 +68,12 @@ def identifier_from_path alias_method :path_info, :identifier_from_path def base_url - request.env["pactbroker.base_url"] || PactBroker.configuration.base_url || request.base_uri.to_s.chomp('/') + request.env["pactbroker.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 - request.env["pactbroker.base_url"] || PactBroker.configuration.base_url || '' + request.env["pactbroker.base_url"] || '' end def charsets_provided diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index b7a57d03c..34b256875 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -5,6 +5,7 @@ require 'pact_broker/policies' require 'rack-protection' require 'rack/hal_browser' +require 'rack/pact_broker/set_base_url' require 'rack/pact_broker/add_pact_broker_version_header' require 'rack/pact_broker/convert_file_extension_to_accept_header' require 'rack/pact_broker/database_transaction' @@ -189,6 +190,7 @@ def configure_middleware @app_builder.use Rack::Static, :urls => ["/stylesheets", "/css", "/fonts", "/js", "/javascripts", "/images"], :root => PactBroker.project_root.join("public") @app_builder.use Rack::Static, :urls => ["/favicon.ico"], :root => PactBroker.project_root.join("public/images"), header_rules: [[:all, {'Content-Type' => 'image/x-icon'}]] @app_builder.use Rack::PactBroker::ConvertFileExtensionToAcceptHeader + @app_builder.use Rack::PactBroker::SetBaseUrl, configuration.base_url if configuration.use_hal_browser logger.info "Mounting HAL browser" diff --git a/lib/pact_broker/doc/controllers/app.rb b/lib/pact_broker/doc/controllers/app.rb index 99c741987..1e91476ff 100644 --- a/lib/pact_broker/doc/controllers/app.rb +++ b/lib/pact_broker/doc/controllers/app.rb @@ -51,7 +51,7 @@ 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. - env["pactbroker.base_url"] || PactBroker.configuration.base_url || '' + env["pactbroker.base_url"] || '' end end end diff --git a/lib/rack/pact_broker/set_base_url.rb b/lib/rack/pact_broker/set_base_url.rb new file mode 100644 index 000000000..92ba2c8ee --- /dev/null +++ b/lib/rack/pact_broker/set_base_url.rb @@ -0,0 +1,22 @@ +module Rack + module PactBroker + class SetBaseUrl + def initialize app, base_url + @app = app + @base_url = base_url + end + + def call env + if env["pactbroker.base_url"] + app.call(env) + else + app.call(env.merge("pactbroker.base_url" => base_url)) + end + end + + private + + attr_reader :app, :base_url + end + end +end diff --git a/spec/lib/pact_broker/api/resources/default_base_resource_spec.rb b/spec/lib/pact_broker/api/resources/default_base_resource_spec.rb index 9ff0c69b3..76d911ec0 100644 --- a/spec/lib/pact_broker/api/resources/default_base_resource_spec.rb +++ b/spec/lib/pact_broker/api/resources/default_base_resource_spec.rb @@ -90,7 +90,6 @@ module Resources 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 @@ -99,20 +98,10 @@ module Resources end end - context "when PactBroker.configuration.base_url is not nil" do + context "when the rack env value is not set" do before do 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 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 it "returns the base URL from the request" do expect(subject.base_url).to eq "http://example.org" diff --git a/spec/lib/pact_broker/doc/controllers/app_spec.rb b/spec/lib/pact_broker/doc/controllers/app_spec.rb index 29798c8f8..97e6768ae 100644 --- a/spec/lib/pact_broker/doc/controllers/app_spec.rb +++ b/spec/lib/pact_broker/doc/controllers/app_spec.rb @@ -4,13 +4,13 @@ module PactBroker module Doc module Controllers describe App do - describe "GET relation" do let(:app) { PactBroker::Doc::Controllers::App } + let(:rack_env) { {} } context "when the resource exists" do - subject { get "/webhooks" } + subject { get "/webhooks", nil, rack_env } it "returns a 200 status" do subject @@ -34,9 +34,7 @@ module Controllers end context "with the base_url set" do - before do - allow(PactBroker.configuration).to receive(:base_url).and_return('http://example.org/pact-broker') - end + let(:rack_env) { { "pactbroker.base_url" => "http://example.org/pact-broker"} } it "returns absolute links" do expect(subject.body).to include "href='http://example.org/pact-broker/css"