Skip to content

Commit 1c37116

Browse files
committed
feat: allow a custom API to be configured
1 parent c544721 commit 1c37116

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

lib/pact_broker/app.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def use_custom_ui custom_ui
6060
@custom_ui = custom_ui
6161
end
6262

63+
def use_custom_api custom_api
64+
@custom_api = custom_api
65+
end
66+
6367
def call env
6468
running_app.call env
6569
end
@@ -182,11 +186,13 @@ def build_custom_ui
182186
def build_api
183187
logger.info "Mounting PactBroker::API"
184188
require 'pact_broker/api'
189+
api_apps = [PactBroker::API]
190+
api_apps.unshift(@custom_api) if @custom_api
185191
builder = ::Rack::Builder.new
186192
builder.use @make_it_later_api_auth
187193
builder.use Rack::PactBroker::Convert404ToHal
188194
builder.use Rack::PactBroker::DatabaseTransaction, configuration.database_connection
189-
builder.run PactBroker::API
195+
builder.run Rack::Cascade.new(api_apps)
190196
builder
191197
end
192198

spec/lib/pact_broker/app_spec.rb

+28-4
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ def self.calls
6666
end
6767

6868
describe "use_custom_ui" do
69+
before do
70+
app.use_custom_ui(custom_ui)
71+
get "/", nil, { "HTTP_ACCEPT" => "text/html" }
72+
end
73+
6974
context "when the UI returns a non 404 response" do
7075
let(:custom_ui) { double('ui', call: [200, {}, ["hello"]]) }
7176

7277
it "returns the given page" do
73-
app.use_custom_ui(custom_ui)
74-
75-
get "/", nil, { "HTTP_ACCEPT" => "text/html" }
7678
expect(last_response.body).to eq "hello"
7779
end
7880
end
@@ -81,12 +83,34 @@ def self.calls
8183
let(:custom_ui) { double('ui', call: [404, {}, []]) }
8284

8385
it "passes on the call to the rest of the app" do
84-
get "/", nil, { "HTTP_ACCEPT" => "text/html" }
8586
expect(last_response.status).to eq 200
8687
end
8788
end
8889
end
8990

91+
describe "use_custom_api" do
92+
before do
93+
app.use_custom_api(custom_api)
94+
get "/", nil, { "HTTP_ACCEPT" => "application/hal+json" }
95+
end
96+
97+
context "when the API returns a non 404 response" do
98+
let(:custom_api) { double('api', call: [200, {}, ["hello"]]) }
99+
100+
it "returns the given resource" do
101+
expect(last_response.body).to eq "hello"
102+
end
103+
end
104+
105+
context "when the custom API returns a 404 response" do
106+
let(:custom_api) { double('api', call: [404, {}, []]) }
107+
108+
it "passes on the call to the rest of the app" do
109+
expect(last_response.body).to_not eq "hello"
110+
end
111+
end
112+
end
113+
90114
describe "use_xxx_auth" do
91115
class TestAuth
92116
def initialize app, *args, &block

0 commit comments

Comments
 (0)