Skip to content

Commit

Permalink
chore: ensure requests for the badges get sent to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 15, 2020
1 parent 0b210e1 commit 22b8c1a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/pact_broker/api/paths.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion lib/rack/pact_broker/request_target.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'pact_broker/api/paths'

module Rack
module PactBroker
Expand All @@ -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
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/rack/pact_broker/request_target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22b8c1a

Please sign in to comment.