From 22b8c1a54aacb1ae0ae4bc574396696750b14d5a Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 15 Jul 2020 19:01:50 +1000 Subject: [PATCH] chore: ensure requests for the badges get sent to the API --- lib/pact_broker/api/paths.rb | 15 +++++++++++++++ lib/rack/pact_broker/request_target.rb | 7 ++++++- spec/lib/rack/pact_broker/request_target_spec.rb | 7 +++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 lib/pact_broker/api/paths.rb diff --git a/lib/pact_broker/api/paths.rb b/lib/pact_broker/api/paths.rb new file mode 100644 index 000000000..533f7c4be --- /dev/null +++ b/lib/pact_broker/api/paths.rb @@ -0,0 +1,15 @@ +module PactBroker + module Api + 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 + + extend self + + def is_badge_path?(path) + # Optimise by checking include? first - regexp slow + path.include?('/badge') && (path =~ PACT_BADGE_PATH || path =~ MATRIX_BADGE_PATH) + end + end + end +end diff --git a/lib/rack/pact_broker/request_target.rb b/lib/rack/pact_broker/request_target.rb index d24d01f07..1d32b92b9 100644 --- a/lib/rack/pact_broker/request_target.rb +++ b/lib/rack/pact_broker/request_target.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'pact_broker/api/paths' module Rack module PactBroker @@ -13,7 +14,7 @@ def request_for_ui?(env) end def request_for_api?(env) - explicit_request_for_api(env) || no_accept_header(env) || (accept_all(env) && !is_web_extension(env)) + explicit_request_for_api(env) || no_accept_header(env) || is_badge_request?(env) || (accept_all(env) && !is_web_extension(env)) end private @@ -38,6 +39,10 @@ def is_api_content_type(header) API_CONTENT_TYPES.any?{ |content_type| header.include?(content_type) } end + def is_badge_request?(env) + env['HTTP_ACCEPT'].include?('svg') && ::PactBroker::Api::Paths.is_badge_path?(env['PATH_INFO']) + end + # default curl Accept header # Also used by browsers to request various web assets like woff files def accept_all(env) diff --git a/spec/lib/rack/pact_broker/request_target_spec.rb b/spec/lib/rack/pact_broker/request_target_spec.rb index 7ba62dfd5..375bbed85 100644 --- a/spec/lib/rack/pact_broker/request_target_spec.rb +++ b/spec/lib/rack/pact_broker/request_target_spec.rb @@ -56,6 +56,13 @@ module PactBroker it { is_expected.to be false } end + + context "when the request is for a badge resource with a svg content type" do + let(:accept) { "image/svg+xml;charset=utf-8" } + let(:path) { "/pacts/provider/foo/consumer/bar/latest/badge" } + + it { is_expected.to be false } + end end end end