Sha256: 00e2546fabe700ab3d9391b1a2f3b11eb0a4ba8520d92439f68d873ade666590

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true
require "pact_broker/api/paths"

module Rack
  module PactBroker
    module RequestTarget
      extend self

      WEB_ASSET_EXTENSIONS = %w[.js .woff .woff2 .css .png .html .map .ttf .ico].freeze
      API_CONTENT_TYPES = %w[application/hal+json application/problem+json application/json text/csv application/yaml text/plain].freeze

      def request_for_ui?(env)
        !(request_for_api?(env))
      end

      def request_for_api?(env)
        explicit_request_for_api(env) || no_accept_header(env) || is_badge_request?(env) || (accept_all(env) && !is_web_extension(env))
      end

      private

      def body_is_json(env)
        env["CONTENT_TYPE"]&.include?("json")
      end

      def explicit_request_for_api(env)
        accepts_api_content_type(env) || body_is_api_content_type(env)
      end

      def accepts_api_content_type(env)
        is_api_content_type((env["HTTP_ACCEPT"]&.downcase) || "")
      end

      def body_is_api_content_type(env)
        is_api_content_type((env["CONTENT_TYPE"]&.downcase) || "")
      end

      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)
        env["HTTP_ACCEPT"] == "*/*"
      end

      # No browser ever makes a request without an accept header, so it must be an API
      # request if there is no Accept header
      def no_accept_header(env)
        env["HTTP_ACCEPT"] == nil || env["HTTP_ACCEPT"] == ""
      end

      def is_web_extension(env)
        env["PATH_INFO"].end_with?(*WEB_ASSET_EXTENSIONS)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/rack/pact_broker/request_target.rb
pact_broker-2.111.0 lib/rack/pact_broker/request_target.rb
pact_broker-2.109.1 lib/rack/pact_broker/request_target.rb
pact_broker-2.109.0 lib/rack/pact_broker/request_target.rb
pact_broker-2.108.0 lib/rack/pact_broker/request_target.rb
pact_broker-2.107.1 lib/rack/pact_broker/request_target.rb
pact_broker-2.107.0 lib/rack/pact_broker/request_target.rb
pact_broker-2.107.0.beta.1 lib/rack/pact_broker/request_target.rb
pact_broker-2.106.0 lib/rack/pact_broker/request_target.rb