Skip to content

Commit 23695b0

Browse files
committed
feat: allow a custom UI to be configured
1 parent e91b21d commit 23695b0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/pact_broker/app.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def initialize &block
3232
yield configuration
3333
post_configure
3434
prepare_database
35-
prepare_app
3635
end
3736

3837
# Allows middleware to be inserted at the bottom of the shared middlware stack
@@ -54,12 +53,18 @@ def use_ui_auth middleware
5453
@make_it_later_ui_auth.make_it_later(middleware)
5554
end
5655

56+
def use_custom_ui custom_ui
57+
@custom_ui = custom_ui
58+
end
59+
5760
def call env
5861
running_app.call env
5962
end
6063

6164
private
6265

66+
attr_reader :custom_ui
67+
6368
def post_configure
6469
configure_logger
6570
SuckerPunch.logger = configuration.custom_logger || SemanticLogger['SuckerPunch']
@@ -101,6 +106,7 @@ def prepare_app
101106
configure_middleware
102107

103108
# need this first so UI login logic is performed before API login logic
109+
@cascade_apps << build_custom_ui if custom_ui
104110
@cascade_apps << build_ui
105111

106112
if configuration.enable_diagnostic_endpoints
@@ -139,6 +145,14 @@ def build_ui
139145
builder
140146
end
141147

148+
def build_custom_ui
149+
logger.info "Mounting Custom UI"
150+
builder = ::Rack::Builder.new
151+
builder.use Rack::PactBroker::AcceptsHtmlFilter
152+
builder.run @custom_ui
153+
builder
154+
end
155+
142156
def build_api
143157
logger.info "Mounting PactBroker::API"
144158
require 'pact_broker/api'
@@ -174,6 +188,7 @@ def configure_logger
174188

175189
def running_app
176190
@running_app ||= begin
191+
prepare_app
177192
apps = @cascade_apps
178193
@app_builder.map "/" do
179194
run Rack::Cascade.new(apps)

spec/lib/pact_broker/app_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ def self.calls
6565
end
6666
end
6767

68+
describe "use_custom_ui" do
69+
context "when the UI returns a non 404 response" do
70+
let(:custom_ui) { double('ui', call: [200, {}, ["hello"]]) }
71+
72+
it "returns the given page" do
73+
app.use_custom_ui(custom_ui)
74+
75+
get "/", nil, { "HTTP_ACCEPT" => "text/html" }
76+
expect(last_response.body).to eq "hello"
77+
end
78+
end
79+
80+
context "when the UI returns a 404 response" do
81+
let(:custom_ui) { double('ui', call: [404, {}, []]) }
82+
83+
it "passes on the call to the rest of the app" do
84+
get "/", nil, { "HTTP_ACCEPT" => "text/html" }
85+
expect(last_response.status).to eq 200
86+
end
87+
end
88+
end
89+
6890
describe "use_xxx_auth" do
6991
class TestAuth
7092
def initialize app, *args, &block

0 commit comments

Comments
 (0)