Skip to content

Commit

Permalink
feat: allow the base URL of the application to be set for the API
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 30, 2020
1 parent 89ea1a5 commit 73bd4c4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 1 addition & 5 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
24 changes: 23 additions & 1 deletion spec/lib/pact_broker/api/resources/base_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }

Expand All @@ -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 }
Expand Down

0 comments on commit 73bd4c4

Please sign in to comment.