From 1c371163a61ecb08f6880373815c5c43ff89613e Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 6 May 2019 12:32:09 +1000 Subject: [PATCH] feat: allow a custom API to be configured --- lib/pact_broker/app.rb | 8 +++++++- spec/lib/pact_broker/app_spec.rb | 32 ++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 4618c93ca..75d1b169b 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -60,6 +60,10 @@ def use_custom_ui custom_ui @custom_ui = custom_ui end + def use_custom_api custom_api + @custom_api = custom_api + end + def call env running_app.call env end @@ -182,11 +186,13 @@ def build_custom_ui def build_api logger.info "Mounting PactBroker::API" require 'pact_broker/api' + api_apps = [PactBroker::API] + api_apps.unshift(@custom_api) if @custom_api builder = ::Rack::Builder.new builder.use @make_it_later_api_auth builder.use Rack::PactBroker::Convert404ToHal builder.use Rack::PactBroker::DatabaseTransaction, configuration.database_connection - builder.run PactBroker::API + builder.run Rack::Cascade.new(api_apps) builder end diff --git a/spec/lib/pact_broker/app_spec.rb b/spec/lib/pact_broker/app_spec.rb index c5cc8349f..f66dd4b87 100644 --- a/spec/lib/pact_broker/app_spec.rb +++ b/spec/lib/pact_broker/app_spec.rb @@ -66,13 +66,15 @@ def self.calls end describe "use_custom_ui" do + before do + app.use_custom_ui(custom_ui) + get "/", nil, { "HTTP_ACCEPT" => "text/html" } + end + context "when the UI returns a non 404 response" do let(:custom_ui) { double('ui', call: [200, {}, ["hello"]]) } it "returns the given page" do - app.use_custom_ui(custom_ui) - - get "/", nil, { "HTTP_ACCEPT" => "text/html" } expect(last_response.body).to eq "hello" end end @@ -81,12 +83,34 @@ def self.calls let(:custom_ui) { double('ui', call: [404, {}, []]) } it "passes on the call to the rest of the app" do - get "/", nil, { "HTTP_ACCEPT" => "text/html" } expect(last_response.status).to eq 200 end end end + describe "use_custom_api" do + before do + app.use_custom_api(custom_api) + get "/", nil, { "HTTP_ACCEPT" => "application/hal+json" } + end + + context "when the API returns a non 404 response" do + let(:custom_api) { double('api', call: [200, {}, ["hello"]]) } + + it "returns the given resource" do + expect(last_response.body).to eq "hello" + end + end + + context "when the custom API returns a 404 response" do + let(:custom_api) { double('api', call: [404, {}, []]) } + + it "passes on the call to the rest of the app" do + expect(last_response.body).to_not eq "hello" + end + end + end + describe "use_xxx_auth" do class TestAuth def initialize app, *args, &block