Skip to content

Commit

Permalink
feat(verification results): redirect requests for verification result…
Browse files Browse the repository at this point in the history
…s to the HAL browser if requested in a web browser
  • Loading branch information
bethesque committed Jan 21, 2021
1 parent 10b49fb commit 0f948de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/pact_broker/api/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ module Paths
PACT_BADGE_PATH = %r{^/pacts/provider/[^/]+/consumer/.*/badge(?:\.[A-Za-z]+)?$}.freeze
MATRIX_BADGE_PATH = %r{^/matrix/provider/[^/]+/latest/[^/]+/consumer/[^/]+/latest/[^/]+/badge(?:\.[A-Za-z]+)?$}.freeze
CAN_I_DEPLOY_BADGE_PATH = %r{^/pacticipants/[^/]+/latest-version/[^/]+/can-i-deploy/to/[^/]+/badge(?:\.[A-Za-z]+)?$}.freeze
VERIFICATION_RESULTS = %r{^/pacts/provider/[^/]+/consumer/[^/]+/pact-version/[^/]+/verification-results/[^/]+}

extend self

def is_verification_results_path?(path)
path.start_with?('/pacts') && (path =~ VERIFICATION_RESULTS)
end

def is_badge_path?(path)
# Optimise by checking include? first - regexp slow
path.include?('/badge') && (path =~ PACT_BADGE_PATH || path =~ MATRIX_BADGE_PATH || path =~ CAN_I_DEPLOY_BADGE_PATH)
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'pact_broker/policies'
require 'rack-protection'
require 'rack/hal_browser'
require 'rack/pact_broker/hal_browser_redirect'
require 'rack/pact_broker/set_base_url'
require 'rack/pact_broker/add_pact_broker_version_header'
require 'rack/pact_broker/convert_file_extension_to_accept_header'
Expand Down Expand Up @@ -190,11 +191,13 @@ def configure_middleware
@app_builder.use Rack::Static, :urls => ["/stylesheets", "/css", "/fonts", "/js", "/javascripts", "/images"], :root => PactBroker.project_root.join("public")
@app_builder.use Rack::Static, :urls => ["/favicon.ico"], :root => PactBroker.project_root.join("public/images"), header_rules: [[:all, {'Content-Type' => 'image/x-icon'}]]
@app_builder.use Rack::PactBroker::ConvertFileExtensionToAcceptHeader
# Rack::PactBroker::SetBaseUrl needs to be before the Rack::PactBroker::HalBrowserRedirect
@app_builder.use Rack::PactBroker::SetBaseUrl, configuration.base_url

if configuration.use_hal_browser
logger.info "Mounting HAL browser"
@app_builder.use Rack::HalBrowser::Redirect
@app_builder.use Rack::PactBroker::HalBrowserRedirect
else
logger.info "Not mounting HAL browser"
end
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/rack/pact_broker/hal_browser_redirect_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rack/pact_broker/hal_browser_redirect'

module Rack
module PactBroker
describe HalBrowserRedirect do
let(:target_app) { ->(env){ [200, {}, []] } }
let(:app) { HalBrowserRedirect.new(target_app) }
let(:rack_env) do
{
"pactbroker.base_url" => "http://base/foo",
"HTTP_ACCEPT" => "text/html"
}
end

subject { get(path, nil, rack_env) }

context "when requesting verification results" do
let(:path) { "/pacts/provider/Bar/consumer/Foo/pact-version/a2456ade40d0e148e23fb3310ec56831fef6ce8e/verification-results/106" }

it "redirects to the HAL browser" do
expect(subject.status).to eq 303
expect(subject.headers["Location"]).to eq "http://base/foo/hal-browser/browser.html#http://base/foo/pacts/provider/Bar/consumer/Foo/pact-version/a2456ade40d0e148e23fb3310ec56831fef6ce8e/verification-results/106"
end
end
end
end
end

0 comments on commit 0f948de

Please sign in to comment.