diff --git a/lib/rack/pact_broker/database_transaction.rb b/lib/rack/pact_broker/database_transaction.rb index 25d3acf73..7b808de4c 100644 --- a/lib/rack/pact_broker/database_transaction.rb +++ b/lib/rack/pact_broker/database_transaction.rb @@ -22,14 +22,18 @@ def initialize app, database_connection def call env set_database_connector if use_transaction? env - call_with_transaction env + call_with_transaction(add_database_connector(env)) else - call_without_transaction env + call_without_transaction(add_database_connector(env)) end ensure clear_database_connector end + def add_database_connector(env) + env.merge("pactbroker.database_connector" => @default_database_connector) + end + def use_transaction? env TRANS_METHODS.include? env[REQUEST_METHOD] end diff --git a/spec/lib/rack/pact_broker/database_transaction_spec.rb b/spec/lib/rack/pact_broker/database_transaction_spec.rb index dfff5ec4a..8856181f2 100644 --- a/spec/lib/rack/pact_broker/database_transaction_spec.rb +++ b/spec/lib/rack/pact_broker/database_transaction_spec.rb @@ -25,6 +25,16 @@ module PactBroker subject { self.send(http_method, "/") } + it "sets the pactbroker.database_connector on the env" do + actual_env = nil + allow(api).to receive(:call) do | env | + actual_env = env + [200, {}, {}] + end + subject + expect(actual_env).to have_key("pactbroker.database_connector") + end + context "for get requests" do let(:http_method) { :get }