Sha256: 3be9e8f15cc21307fe643c40486e022348e5d3c6df2d833392dd354bf36aca30

Contents?: true

Size: 1.38 KB

Versions: 44

Compression:

Stored size: 1.38 KB

Contents

module Rack
  module PactBroker
    # If the HTML and the CSV group resources are both requested by the browser,
    # Chrome gets confused by the content types, and when you click back, it tries to load the CSV
    # instead of the HTML page. So we have to give the CSV resource a different URL (.csv)
    # Update: this should be fixed by lib/rack/pact_broker/add_vary_header.rb, but may as well
    # leave it now!

    class ConvertFileExtensionToAcceptHeader

      EXTENSION_REGEXP = /\.\w+$/.freeze
      EXTENSIONS = {
        ".csv" => "text/csv",
        ".svg" => "image/svg+xml",
        ".json" => "application/hal+json",
        ".yaml" => "application/yaml"
      }

      def initialize app
        @app = app
      end

      def call env
        file_extension = extension(env)
        if convert_to_accept_header? file_extension
          @app.call(set_accept_header_and_path_info(env, file_extension))
        else
          @app.call(env)
        end
      end

      def convert_to_accept_header? file_extension
        EXTENSIONS[file_extension]
      end

      def extension env
        env["PATH_INFO"] =~ EXTENSION_REGEXP && $~[0]
      end

      def set_accept_header_and_path_info env, file_extension
        env.merge(
          "PATH_INFO" => env["PATH_INFO"].gsub(EXTENSION_REGEXP, ""),
          "HTTP_ACCEPT" => EXTENSIONS[file_extension]
        )
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.111.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.109.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.109.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.108.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.107.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.107.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.107.0.beta.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.106.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.105.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.104.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.103.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.102.2 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.102.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.102.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.101.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.100.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.99.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.98.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.97.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb