From 0f948de5fe65e18cabc40586c39c379bbe6566e4 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 21 Jan 2021 13:10:41 +1100 Subject: [PATCH] feat(verification results): redirect requests for verification results to the HAL browser if requested in a web browser --- lib/pact_broker/api/paths.rb | 5 ++++ lib/pact_broker/app.rb | 3 +++ .../pact_broker/hal_browser_redirect_spec.rb | 27 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 spec/lib/rack/pact_broker/hal_browser_redirect_spec.rb diff --git a/lib/pact_broker/api/paths.rb b/lib/pact_broker/api/paths.rb index af048ebba..c8bb0b482 100644 --- a/lib/pact_broker/api/paths.rb +++ b/lib/pact_broker/api/paths.rb @@ -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) diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 34b256875..2566deef6 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -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' @@ -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 diff --git a/spec/lib/rack/pact_broker/hal_browser_redirect_spec.rb b/spec/lib/rack/pact_broker/hal_browser_redirect_spec.rb new file mode 100644 index 000000000..a62bfd2b8 --- /dev/null +++ b/spec/lib/rack/pact_broker/hal_browser_redirect_spec.rb @@ -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