Skip to content

Commit

Permalink
feat: allow a custom UI to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 30, 2019
1 parent e91b21d commit 23695b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def initialize &block
yield configuration
post_configure
prepare_database
prepare_app
end

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

def use_custom_ui custom_ui
@custom_ui = custom_ui
end

def call env
running_app.call env
end

private

attr_reader :custom_ui

def post_configure
configure_logger
SuckerPunch.logger = configuration.custom_logger || SemanticLogger['SuckerPunch']
Expand Down Expand Up @@ -101,6 +106,7 @@ def prepare_app
configure_middleware

# need this first so UI login logic is performed before API login logic
@cascade_apps << build_custom_ui if custom_ui
@cascade_apps << build_ui

if configuration.enable_diagnostic_endpoints
Expand Down Expand Up @@ -139,6 +145,14 @@ def build_ui
builder
end

def build_custom_ui
logger.info "Mounting Custom UI"
builder = ::Rack::Builder.new
builder.use Rack::PactBroker::AcceptsHtmlFilter
builder.run @custom_ui
builder
end

def build_api
logger.info "Mounting PactBroker::API"
require 'pact_broker/api'
Expand Down Expand Up @@ -174,6 +188,7 @@ def configure_logger

def running_app
@running_app ||= begin
prepare_app
apps = @cascade_apps
@app_builder.map "/" do
run Rack::Cascade.new(apps)
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/pact_broker/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ def self.calls
end
end

describe "use_custom_ui" do
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

context "when the UI returns a 404 response" do
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_xxx_auth" do
class TestAuth
def initialize app, *args, &block
Expand Down

0 comments on commit 23695b0

Please sign in to comment.