Skip to content

Commit

Permalink
feat: allow a custom API to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 6, 2019
1 parent c544721 commit 1c37116
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
32 changes: 28 additions & 4 deletions spec/lib/pact_broker/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1c37116

Please sign in to comment.