From 73bd4c44928632decf38c54aba331e6e16027665 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 30 Jan 2020 15:49:29 +1100 Subject: [PATCH] feat: allow the base URL of the application to be set for the API --- .../api/resources/base_resource.rb | 2 +- lib/pact_broker/app.rb | 2 -- lib/pact_broker/configuration.rb | 6 +---- .../api/resources/base_resource_spec.rb | 24 ++++++++++++++++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/pact_broker/api/resources/base_resource.rb b/lib/pact_broker/api/resources/base_resource.rb index a5e22ed9b..c6ae09a77 100644 --- a/lib/pact_broker/api/resources/base_resource.rb +++ b/lib/pact_broker/api/resources/base_resource.rb @@ -59,7 +59,7 @@ def identifier_from_path alias_method :path_info, :identifier_from_path def base_url - request.base_uri.to_s.chomp('/') + PactBroker.configuration.base_url || request.base_uri.to_s.chomp('/') end def charsets_provided diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index e8c8f86d0..1e83affb2 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -4,7 +4,6 @@ require 'pact_broker/logging/default_formatter' require 'rack-protection' require 'rack/hal_browser' -require 'rack/pact_broker/store_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' @@ -159,7 +158,6 @@ def configure_middleware end @app_builder.use Rack::PactBroker::InvalidUriProtection @app_builder.use Rack::PactBroker::ResetThreadData - @app_builder.use Rack::PactBroker::StoreBaseURL @app_builder.use Rack::PactBroker::AddPactBrokerVersionHeader @app_builder.use Rack::PactBroker::AddVaryHeader @app_builder.use Rack::Static, :urls => ["/stylesheets", "/css", "/fonts", "/js", "/javascripts", "/images"], :root => PactBroker.project_root.join("public") diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index cf39c4cd2..da0342110 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -33,7 +33,7 @@ class Configuration :seed_example_data ] - attr_accessor :log_dir, :database_connection, :auto_migrate_db, :auto_migrate_db_data, :example_data_seeder, :seed_example_data, :use_hal_browser, :html_pact_renderer, :use_rack_protection + attr_accessor :base_url, :log_dir, :database_connection, :auto_migrate_db, :auto_migrate_db_data, :example_data_seeder, :seed_example_data, :use_hal_browser, :html_pact_renderer, :use_rack_protection attr_accessor :validate_database_connection_config, :enable_diagnostic_endpoints, :version_parser, :sha_generator attr_accessor :use_case_sensitive_resource_names, :order_versions_by_date attr_accessor :check_for_potential_duplicate_pacticipant_names @@ -176,10 +176,6 @@ def enable_badge_resources= enable_badge_resources self.enable_public_badge_access = enable_badge_resources end - def base_url - ENV['PACT_BROKER_BASE_URL'] - end - def save_to_database # Can't require a Sequel::Model class before the connection has been set require 'pact_broker/config/save' diff --git a/spec/lib/pact_broker/api/resources/base_resource_spec.rb b/spec/lib/pact_broker/api/resources/base_resource_spec.rb index 6297e6c09..ca49c427b 100644 --- a/spec/lib/pact_broker/api/resources/base_resource_spec.rb +++ b/spec/lib/pact_broker/api/resources/base_resource_spec.rb @@ -4,7 +4,7 @@ module PactBroker module Api module Resources describe BaseResource do - let(:request) { double('request', uri: uri).as_null_object } + let(:request) { double('request', uri: uri, base_uri: URI("http://example.org/")).as_null_object } let(:response) { double('response') } let(:uri) { URI('http://example.org/path?query') } @@ -19,6 +19,28 @@ module Resources expect(subject.headers['Access-Control-Allow-Methods']).to eq "GET, OPTIONS" end end + + describe "base_url" do + context "when PactBroker.configuration.base_url is not nil" do + before do + allow(PactBroker.configuration).to receive(:base_url).and_return("http://foo") + 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 + before do + 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" + end + end + end end ALL_RESOURCES = ObjectSpace.each_object(::Class).select {|klass| klass < BaseResource }